设计 - 使用Windsor时应注册对象 [英] Design - Where should objects be registered when using Windsor

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

问题描述

我的应用程序中将包含以下组件

I will have the following components in my application


  • DataAccess

  • DataAccess.Test

  • 业务

  • Business.Test

  • 申请

  • DataAccess
  • DataAccess.Test
  • Business
  • Business.Test
  • Application

我希望使用城堡温莎作为IoC将层叠在一起,但我对胶合的设计有些不确定。

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. 应用程序(或测试应用程序)注册所有依赖关系的对象。 / li>
  1. Each layer can register its own objects. To test the BL, the test bench could register mock classes for the DAL.
  2. Each layer can register the object of its dependencies, e.g. the business layer registers the components of the data access layer. To test the BL, the test bench would have to unload the "real" DAL object and register the mock objects.
  3. The application (or test app) registers all objects of the dependencies.

有人可以帮助我一些想法和利弊的不同路径?

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

  • 等。

  • In a desktop app, that would be in the Main method (or very close to it)
  • In an ASP.NET (including MVC) application, that would be in Global.asax
  • In WCF, that would be in a ServiceHostFactory
  • etc.

容器只是将模块组合成工作应用程序的引擎。原则上,您可以手动编写代码(这称为可怜人的DI ),但是使用像Windsor这样的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.

这样的组合根将理想地是应用程序根目录中的唯一代码,使应用程序成为所谓的可执行程序(Humble Executable)本身不需要单元测试的优秀 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.

另外在Windsor中,您应该在安装程序中封装组件注册逻辑(实现 IWindsorInstaller )请参见文档了解更多详情

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

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

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