在域层中集成DI容器。域事件 [英] Integrating DI container within domain layer. Domain events

查看:83
本文介绍了在域层中集成DI容器。域事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在文章之后: http://www.udidahan.com / 2009/06/14 / domain-events-salvation /
我们可以看到 DomainEvents 实现使用DI容器

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; }

然后

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

我应该将DI容器集成到存储域对象的同一程序集中吗?外部化/提取 Container.ResolveAll< Handles< T>>>()? (根据我以前的经验,我将所有与 DI相关的放在 global.asax.cs 内)。

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

从技术上讲,我只熟悉Ninject DI容器,但可能会理解这个概念,因此可以赞赏您的建议/插图。

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

谢谢!

推荐答案

否,没有必要。我将DomainEvents及其方法设为非静态,然后使用容器创建它。一个体面的容器将创建并初始化Handles及其依赖关系,并允许您在不引用容器的情况下调用事件处理程序。

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.

唯一的陷阱是对事件处理程序。为此,我使用 Bootstrapper 调用IUnityRegistration实例并配置UNITY。我开始使用 CommonServiceLocator 来减少依赖性。甚至最近,我转而使用MEF摆脱了所有注册类。

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.

这篇关于在域层中集成DI容器。域事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆