Search code examples
c#model-view-controllerstructuremapviewengine

How to make a structure map powered viewengine in asp.net mvc


My views extend a base view class I've made:

public class BaseView : ViewPage

At the moment I'm calling ObjectFactory.GetInstance inside this class's constructor to get some interface implementations but I'd like to use structuremap to inject them as constructor arguments.

I'm using a structuremapcontrollerfactory to create my controllers, but how can I do the same for views?

I know I can implement a custom ViewEngine, but using reflector to look at the mvc default viewengine and its dependencies, it seems to go on and on and I'd rather not have to re-implement stuff that's already there. How can I solve this?

I know I could make things easier with setter instead of constructor injection but I'd rather avoid that if possible.


Solution

  • Yes, if you are talking about the WebForms ViewEngine, you are correct in your assessment that it was not designed with inversion of control in mind. You can either lobby Microsoft to change it, or you can use setter injection. In FubuMVC, we use setter injection along with StructureMap's BuildUp method: