问题描述
我用的是 RegisterInitializer
方法注入的基本类型的属性如下:
I use the RegisterInitializer
method to inject properties in base types as follows:
container.RegisterInitializer<BaseHandler>(handler =>
{
handler.Context = container.GetInstance<HandlerContext>();
});
这个伟大的工程,但 RegisterInitializer
不被解雇的,从BaseHandler继承所有注册的类型。它似乎并不当我打电话运行新
自己:
This works great, but the RegisterInitializer
doesn't get fired on all registered types that inherit from BaseHandler. It doesn't seem to run when I call new
myself:
var url = ConfigurationManager.AppSettings["NotificationServiceUrl"];
container.Register<Handler<NotifyCustomerMessage>>(() =>
new NotifyCustomerHandler(url));
// many other Handler<T> lines here
这是为什么,我怎么能解决这个问题?
Why is that, and how can I solve this?
推荐答案
更新
此行为在改变的简单的注射器2 的:初始化器现在也火上 Func键&LT; T&GT;
注册。其原因是简单的喷油器现在有明确的生活方式的支持,现在实际上的行为像StructureMap(如下所述)。
This behavior has changed in Simple Injector 2: Initializers will now also fire on Func<T>
registrations. The reason is that Simple Injector now has explicit lifestyle support and now in fact behaves like StructureMap (as described below).
您的观察是正确的。简单注射器文件<一个href="http://simpleinjector.$c$cplex.com/wikipage?title=Using%20the%20Simple%20Injector&referringTitle=Home&ProjectName=simpleinjector#Configuring_Property_Injection"相对=nofollow>描述这样的:
Your observation is correct. The Simple Injector documentation describes it like this:
注:容器将不能够 叫上一个初始化委托 是手工构造类型 使用新
运营商。使用自动 构造函数注入时 可能的。
要克服这个问题的最好办法是让简单的注射用自动构造函数注入。
The best way to overcome this problem is allowing Simple Injector to use automatic constructor injection.
您 NotifyCustomerHandler
需要字符串
构造函数的参数,这使得它无法做到自动构造注入。你的 NotifyCustomerHandler
似乎有多重责任。摘要通知服务从处理功能,通过隐藏的 INotificationService
接口,这背后的服务,让处理程序取决于该接口上。您可以在配置值比注入到通知服务。
Your NotifyCustomerHandler
takes a string
constructor argument, which makes it impossible to do automatic constructor injection. Your NotifyCustomerHandler
seems to have multiple responsibilities. Abstract the notification service from the handler functionality, by hiding this service behind an INotificationService
interface and letting the handler depend on that interface. You can than inject that configuration value into the notification service.
为什么简单的喷油器的行为这样的一些背景信息的
Some background information on why Simple Injector behaves this way
虽然其他DI框架(如 StructureMap 其 OnCreationForAll
法)将调用委托即使你新的一个类型,简单的注射没有。它是由在其中的框架期望用户注册生活方式的差异造成的。
Although other DI frameworks (such as StructureMap with its OnCreationForAll
method) will invoke the delegate even when you new up a type, Simple Injector does not. It is caused by the difference in which the frameworks expect users to register lifestyle.
使用StructureMap,生活方式通过显式调用 LifecycleIs
的方法进行配置。通过简单的喷油器用户有望通过注册实现自己的生活方式的代表配置的生活方式。寻找例如在每线程的生活方式例如中的文档。与StructureMap它是控制寿命为你,而用简单的进样器,通常取决于用户的框架。
With StructureMap, lifestyles are configured by explicitly calling the LifecycleIs
method. With Simple Injector users are expected to configure lifestyles by registering delegates that implement lifestyle themselves. Look for instance at the Per Thread lifestyle example in the documentation. With StructureMap it is the framework that controls the lifetime for you, while with Simple Injector it is often up to the user.
在换句话说,StructureMap预计注册委托总是创建一个新的实例,而简单的喷油器不具有这种期望。由于StructureMap期待一个新的实例返回,就可以调用委托后安全初始化对象。实例缓存做过的工作。简单的注射器但是,将没有的调用初始化的对象,从这些代表回来,只因为这将有可能重新初始化同一个对象,一遍又一遍,这可能会导致应用程序意外行为和可能的性能问题。
In other words, StructureMap expects a registered delegate to always create a new instance, while Simple Injector does not have this expectation. Because StructureMap expects a new instance is returned, it can safely initialize that object after calling the delegate. Caching of instances is done elsewhere. Simple Injector however, will not call an initializer on objects returned from such delegate, simply because that would possibly reinitialize the same object over and over again, which could cause unexpected application behavior and possible performance problems.
我希望这有助于。
这篇关于简单的注射器:RegisterInitializer并不总是闪光的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!