Autofac Webform 集成:手动属性注入
考虑这个用于 Webform MVP 样式应用程序的 autofac 注册模块,我在其中通过提供一些参数依赖项来注册几个组件。
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);
}
}
在页面中,我可以使用如下代码解析类型:
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)
});
}
但我真的很想使用属性注入并管理对该级别参数的依赖关系;例如:
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);
}
我可以在 Autofac 上利用一些东西吗?提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,ASP.NET 集成将注入页面级属性。
请参阅此处的“将模块添加到 Web.config”部分:
http://code.google .com/p/autofac/wiki/AspNetIntegration
编辑: 抱歉误解了这个问题。我现在发现您不是在问属性注入是否可用,而是在问如何将其与命名参数结合使用。
我将做出一个推论,并说您需要命名参数的原因只是为了将视图传递给演示者。连接字符串实际上不应该是页面的责任;您可以将其设为 SampleModule 的属性并从那里读取它。
我有一个首选的 MVP/Autofac 方法,它已经经过多年的考验。它很干净,提供了一些很好的扩展点,并且使页面相对没有实现细节。您可以在我对此问题的回答中找到概述:
在 ASP.NET MVP 应用程序中的 Presenter 中注入较低层依赖项
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
这在 Autofac 中有效并且是自动的
This works and is automatic in Autofac