Using Ninject with Udi Dahan's Domain Events

a 夏天 提交于 2019-11-30 22:01:10

How does the "Container" get set in this static class?

You will have to set it during application startup:

DomainEvents.Container = kernel;

what would be the Ninject syntax to resolve all the event handlers:

You can do it like this, for instance:

Container.Get<IEnumerable<Handles<T>>>())

Udi's static DomainEvents class is an implementation of the Ambient Context anti-pattern (see DI PP&P chapter 5.3). In this case I would rather use dependency injection to inject an IDomainEvents abstraction into code that needs it, instead of letting code depend on a static instance.

The problem however is that your domain objects will need a dependency on the IDomainEvents and constructor injection is (probably) not possible. The trick is to use method injection in that case.

In other words, use constructor injection to inject the IDomainEvents into command handlers or services (or what ever you call your business logic that uses the methods on your domain objects) and pass that dependency into the domain object when calling a method that needs it (method injection).

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