Autofac Webform Integration: Manual Property Injection
Consider this autofac registration module for a webform MVP style application, where I register a couple of components by providing some parameters dependency.
public class SampleModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<FooService>().As<IFooService>();
builder.Register<FooPresenter>((c, p) =>
new FooPresenter(p.Named<IFooView>("view"),
c.Resolve<开发者_如何学编程;IFooService>(p),
p.Named<string>("connectionString")))
.InstancePerHttpRequest();
base.Load(builder);
}
}
In the page I can resolve the type using some code like this:
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
var cpa = (IContainerProviderAccessor) HttpContext.Current.ApplicationInstance;
var cp = cpa.ContainerProvider;
// Resolve presenter
var presenter = cp.RequestLifetime.Resolve<FooPresenter>(
new NamedParameter[]
{
new NamedParameter("connectionString", "xyz"),
new NamedParameter("view", this)
});
}
But I would really like to use property injection and manage the dependency on the parameters at that level; Something like this for example:
public FooPresenter Presenter { get; set; }
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
var cpa = (IContainerProviderAccessor) HttpContext.Current.ApplicationInstance;
var cp = cpa.ContainerProvider;
// Parameters bindings?
cp.RequestLifetime.InjectProperties(this);
}
Is there something that I can leverage on Autofac? Thanks in advance.
Yes, there is ASP.NET integration which will inject page-level properties.
See the "Add Modules to Web.config" section here:
http://code.google.com/p/autofac/wiki/AspNetIntegration
Edit: Sorry for misunderstanding the question. I see now that you are not asking if property injection is available, but rather how to use it in conjunction with named parameters.
I am going to make an inference and say the reason you need named parameters is solely to pass the view to the presenter. The connection string shouldn't really be the page's responsibility; you can probably make that a property on SampleModule
and read it from there.
I have a preferred approach to MVP/Autofac which has been battle-tested for years. It is clean, provides some nice extension points, and keep pages relatively free of implementation details. You can find the overview in my answer to this question:
Injecting Lower Layer Dependency in Presenter in an ASP.NET MVP Application
This works and is automatic in Autofac
<Autofac.Integration.Web.Forms.InjectUnsetProperties()>
Public MustInherit Class ViewBasePage(Of TPresenter As BasePresenter(Of TView, TPresenter), TView As {IView, Class})
Inherits System.Web.UI.Page
Implements IView
Protected _presenter As TPresenter
Private _factory As Func(Of TView, TPresenter)
Public Property PresenterFactory As Func(Of TView, TPresenter)
Get
Return _factory
End Get
Set(value As Func(Of TView, TPresenter)
_factory = value
_presenter = _factory(DirectCast(DirectCast(Me, IView), TView))
End Set
End Property
精彩评论