This project is read-only.

MediaTypeFormatters and dependency injection

Topics: Web Api
May 31, 2011 at 4:18 PM
Maybe I'm missing something totally obvious, but it seems formatters are gonna be hard to plug into a DI strategy with the current design.

Typically, the place where DI happens/is setup is in the resource factory (i.e. which sets a child/per-request container scope typically), but formatters are hooked in a totally different place, at the operation handler factory level.

Say I have a formatter that pulls an entity from an EF context before populating it with values sent in a request, and that the entity then reaches the service/resource implementation as a parameter. The service in this case will have a DI-injected dependency on the EF context to do the save/updates as needed to the DB, but if the entity was loaded in a different context, that won't work as nicely.

It looks like the formatter won't have a way to reach the same context as the service, unless I pass some out of band data (say, TLS).



Daniel Cazzulino | Developer Lead | MS MVP | Clarius Consulting | +1 425.329.3471