Dependency Injection and Model View Presenter with WCSF Scenario
David Hayden ( Microsoft MVP C# ), Filed: Web Client Software Factory
When using Model-View-Presenter in ASP.NET, one often injects the presenter into the view using a service locator call to your dependency injection tool as such:
protected override void OnInit(EventArgs e)
{
_presenter = IoC.Resolve<MyPresenter>();
// ...
}
Early reports from Web Client Software Factory Team members suggest that the next version of the WCSF ( or perhaps one of the bundles ) will inject the presenter into the view like this:
protected override void OnInit(EventArgs e)
{
WebClientApplication.BuildItemWithCurrentContext(this);
base.OnInit(e);
}
where the presenter is injected as a property on the view with a [CreateNew] attribute:
[CreateNew]
public MyPresenter Presenter
{
set
{
this._presenter = value;
this._presenter.View = this;
}
}
I talked about this in more detail in the following post:
It is an interesting approach to dependency injection that allows you to fill dependencies on an object that has already been created.
I went ahead and added this feature to my Dependency Injection Application Block that I have been talking about the past week:
Now one can inject the presenter into the view using both methods. First the traditional way:
protected override void OnInit(EventArgs e)
{
_presenter = ObjectFactory.Create<MyPresenter>();
// ...
}
or using a version similar to the proposed Web Client Software Factory approach:
protected override void OnInit(EventArgs e)
{
ObjectFactory.AddDependencies(this);
// ...
}
with an [InjectionProperty] Attribute on the Presenter Property on the View:
[InjectionProperty]
public MyPresenter Presenter
{
set
{
this._presenter = value;
this._presenter.View = this;
}
}
Internally there is an ObjectDependencyProvider that uses a set of pluggable IDependencyProviders to provide dependencies on objects:

One of the providers, InjectionPropertyDependencyProvider, provides the dependencies to properties marked with the [InjectionProperty] Attribute.
I can't see myself using the AddDependencies method a lot, but I needed the feature internally anyway so I could do optional setter injection as part of the typical object creation scenario.
by David Hayden ( Microsoft MVP C# ), Filed: Web Client Software Factory