Integrating DI container within domain layer. Domain events

情到浓时终转凉″ 提交于 2019-12-25 01:49:13

问题


Following the article: http://www.udidahan.com/2009/06/14/domain-events-salvation/ we can see that DomainEvents implemantation uses DI container

public static IContainer Container { get; set; }

and then

if(Container != null) {
    foreach(var handler in Container.ResolveAll<Handles<T>>()) handler.Handle(args);
}

Should I integrate DI container inside the same assembly I store domain objects or can I externalize/abstract away the Container.ResolveAll<Handles<T>>()? (In my previous experiences I put all DI-related stuff inside global.asax.cs).

Technically I'm familiar only with Ninject DI container but probably will understand the concept so your advices/illustrations are appreciated.

Thanks!


回答1:


No it is not necessary. I'd make DomainEvents and its methods non-static and use the container to create it. A decent container will create and initialize the Handles and their dependencies and allow you to call the event handlers without any reference to the container.

The only catch is the registration of the event handlers. For that I use Bootstrapper to call instances of IUnityRegistration and configure UNITY. I started to use CommonServiceLocator to reduce dependencies. And even more recently, I switched to MEF to get rid of the registration classes all together.



来源:https://stackoverflow.com/questions/10444646/integrating-di-container-within-domain-layer-domain-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!