Dependency Injection in Web Forms MVC
David Hayden has a post about the issue that you face when you are trying to use dependency injection in Web Forms MVC. I talked about similar issues here.
He points out that this type of code is bad:
protected Page_PreInit(object sender, EventArgs e) { // Constructor Injection of Data Access Service and View ICustomerDAO dao = Container.Resolve<ICustomerDAO>(): _presenter = new AddCustomerPresenter(dao, this); // Property Injection of Logging ILoggingService logger = Container.Resolve<ILoggingService>(): _presenter.Logger = logger; }
This type of code a Worst Practice in my opinion. It means that the view is responsible for setting up the presenter, that is a big No! right there.
He gives the example of WCSF & Object Builder way of doing it, but I don't think that this is a good approach:
public partial class AddCustomer : Page, IAddCustomer { private AddCustomerPresenter _presenter; [CreateNew] public AddCustomerPresenter Presenter { set { this._presenter = value; this._presenter.View = this; } } // ... }
The problems that I have with this approach is that the view suddenly makes assumptions about the life cycle of the controller, which is not something that I want it to do. I may want a controller per conversation, for instance, and then where would I be? Another issue is that the view is responsible for injecting itself to presenter, which is not something that I would like to see there as well.
Here is how I do it with Rhino.Igloo:
public partial class AddCustomer : BasePage, IAddCustomer { private AddCustomerPresenter _presenter; public AddCustomerPresenter Presenter { set { this._presenter = value; } } // ... }
The BijectionFacility will notice that we have a settable property of type that inherit from BaseController, and will get it from the container and inject that in. I don't believe in explicit Controller->View communication, but assuming that I needed that, it would be very easy to inject that into the presenter. Very easy as in adding three lines of code to ComponentRepository's InjectControllers method:
PropertyInfo view = controller.GetType().GetProperty("View");
if(view!=null)
view.SetValue(controller, instance);

Comments
Comment preview