Using Ninject with Udi Dahan's Domain Events

前端 未结 1 1886
我在风中等你
我在风中等你 2021-01-03 03:15

I\'m using Ninject in an MVC project and am trying to implement Domain Events following Udi Dahan\'s pattern http://www.udidahan.com/2009/06/14/domain-events-salvation/

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-03 03:57

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

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

    0 讨论(0)
提交回复
热议问题