Integration of a DI container in a domain layer. Domain Events

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

 public static IContainer Container { get; set; } 

and then

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

Do I have to integrate the DI container inside the same assembly, am I storing domain objects or can I urgently / abstract from Container.ResolveAll<Handles<T>>() ? (In my previous experiences, I put all the DI-related things inside global.asax.cs ).

Technically, I am only familiar with the Ninject DI container, but I will probably understand this concept, so your tips / illustrations will be appreciated.

Thanks!

0
source share
1 answer

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

The only catch is registering event handlers. To do this, I use Bootstrapper to call instances of IUnityRegistration and configure UNITY. I started using CommonServiceLocator to reduce dependencies. And more recently, I switched to MEF to completely get rid of registration classes.

+2
source

Source: https://habr.com/ru/post/1441329/


All Articles