views:

45

answers:

1

Is it possible to somehow leverage the dependency injection in Asp.net MVC 3 (using the Forms ViewEngine) to inject UserControls from another library? I am already using MEF to load some other stuff into my MvcApplication.

I need this because I want to build a system with an expandable type system. I want the type vendor to be able to inject custom controls for the provided type. I already have a custom MetadataProvider that knows how to handle the provided types (it sets the ModelMetadata.TemplateHint property).

The question is where can I plug in with MEF, so that the TemplateHint gets properly handled, and a custom control is dispalyed upon caling Html.EditorForModel

+1  A: 

I can't think of a way to do this using the current Service Locator infrastructure in MVC 3 beta. I can think of 2 alternative appraoches though:

  1. Have an editor template that instead of rendering the HTML delegates to your custom controls and returns their output.
  2. Write your own ViewEngine that can perform lookups for partial views (the paths will be of the form "EditorTemplates/YourTypeName" etc.) and return a view that knows how to talk to your custom controls.
marcind
Hi, I just implemented my own ViewEngine. I basically just needed to implement IViewEngine.FindPartialView, parse out the custom type id from the partialViewName argument and delegate the rendering back to the custom type. So your answer is kind of correct :)
m0sa
I'm pretty sure #2 is exactly what you described :)
marcind