Simple Injector: RegisterInitializer does not alwa

2019-08-02 02:18发布

I use the RegisterInitializer method to inject properties in base types as follows:

container.RegisterInitializer<BaseHandler>(handler =>
{
    handler.Context = container.GetInstance<HandlerContext>();
});

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?

1条回答
劫难
2楼-- · 2019-08-02 03:01

UPDATE

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).


Your observation is correct. The Simple Injector documentation describes it like this:

Note: The container will not be able to call an initializer delegate on a type that is manually constructed using the new operator. Use automatic constructor injection whenever possible.

The best way to overcome this problem is allowing Simple Injector to use automatic constructor injection.

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

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.

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.

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.

I hope this helps.

查看更多
登录 后发表回答