设计 - 在哪里应该使用对象时,温莎注册 [英] Design - Where should objects be registered when using Windsor

查看:115
本文介绍了设计 - 在哪里应该使用对象时,温莎注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将有下列组件在我的应用

I will have the following components in my application


  • 数据访问

  • DataAccess.Test

  • 商业

  • Business.Test

  • 应用

我希望利用温莎城堡为国际奥委会的各层粘合在一起,但我对胶的设计有点不确定。

I was hoping to use Castle Windsor as IoC to glue the layers together but I am bit uncertain about the design of the gluing.

我的问题是谁应该负责的对象为登记温莎?
我有几个想法;

My question is who should be responsible for registering the objects into Windsor? I have a couple of ideas;


  1. 每层都可以注册自己的对象。为了测试BL,试验台能为DAL注册模拟类。

  2. 每层都可以注册其依赖关系,例如对象业务层寄存器数据访问层的组分。为了测试BL,试验台将不得不卸载真正的DAL对象并注册模拟对象。

  3. 应用程序(或测试程序)注册依赖的所有对象。

有人可以帮助我的一些想法和优点/缺点与不同的路径?链接利用温莎城堡这样的例子项目将是非常有益的。

Can someone help me with some ideas and pros/cons with the different paths? Links to example projects utilizing Castle Windsor in this way would be very helpful.

推荐答案

在一般情况下,在应用程序中的所有组件应尽可能晚组成,因为这确保了最大的模块化,并且模块以松散耦合尽可能

In general, all components in an application should be composed as late as possible, because that ensures maximum modularity, and that modules are as loosely coupled as possible.

在实践中,这意味着你应该在你的应用程序的根目录配置的容器。

In practice, this means that you should configure the container at the root of your application.


  • 在一个桌面应用程序,这将是在Main方法(或非常接近)

  • 在一个ASP.NET(包括MVC)的应用程序,这将是在Global.asax中

  • 在WCF,这将是一个ServiceHostFactory


容器很简单,组成模块集成到工作应用程序引擎。原则上,你可以手工编写code(这就是所谓的穷人的DI 的),但它仅仅是这么容易使用DI容器像温莎。

The container is simply the engine that composes modules into a working application. In principle, you could write the code by hand (this is called Poor Man's DI), but it is just so much easier to use a DI Container like Windsor.

这样的构成根的理想地将是code的在应用程序的根目录的唯一的一块,使得应用所谓的谦逊的可执行文件的(一个从长期优异的xUnit测试模式的),这并不需要在自身的单元测试

Such a Composition Root will ideally be the only piece of code in the application's root, making the application a so-called Humble Executable (a term from the excellent xUnit Test Patterns) that doesn't need unit testing in itself.

您的测试应该不需要容器在所有的,因为你的对象和模块应该是组合的,你可以直接从单元测试提供的测试双打的给他们。这是最好的,如果你能设计所有的模块是容器无关。

Your tests should not need the container at all, as your objects and modules should be composable, and you can directly supply Test Doubles to them from the unit tests. It is best if you can design all of your modules to be container-agnostic.

还特意在温莎,你应该安装在封装组件注册逻辑(实现类型 IWindsorInstaller )查看的有关详细信息,文档

Also specifically in Windsor you should encapsulate your component registration logic within installers (types implementing IWindsorInstaller) See the documentation for more details

这篇关于设计 - 在哪里应该使用对象时,温莎注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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