温莎城堡:注册横跨在溶液中多个项目的组件 [英] Castle Windsor: Register components across multiple projects in solution
问题描述
我想用温莎城堡依赖注入我的解决方案,包括以下项目:
I would like to use Castle Windsor for dependency injection for my solution consisting of the following projects:
- 的mvc [ASP.NET MVC 3 Web应用程序]:presentation层(取决于的商业的和的模式的)
- 商家 [类库]:业务层(取决于的数据访问的和的模式的)
- 数据访问 [类库]:数据访问层(取决于的模式的)
- 模式 [类库]:模型层
- Mvc [ASP.NET MVC 3 Web Application]: presentation layer (depends on Business and Models)
- Business [Class Library]: business layer (depends on DataAccess and Models)
- DataAccess [Class Library]: data access layer (depends on Models)
- Models [Class Library]: model layer
在业务层有一类称为 PostService
实施 IPostService
,管理博客文章。在的MVC PostsController
的项目依赖于 IPostService
。然而, PostService
(对应具体实现)本身依赖于 IPostRepository
。
In the business layer there is a class called PostService
implementing IPostService
that manages blog posts. The PostsController
of the Mvc project depends on IPostService
. However, PostService
(the corresponding concrete implementation) itself depends on IPostRepository
.
在哪里配置温莎城堡返回 PostRepository
的一个实例解析 IPostRepository
?在的mvc 的项目不知道有关的数据访问的项目。因此,我无法配置的组件绑定的的Global.asax 的或其他地方内的的mvc 的
Where do I configure Castle Windsor to return an instance of PostRepository
to resolve IPostRepository
? The Mvc project doesn't know about the DataAccess project. Thus, I can't configure the component bindings in global.asax or somewhere else within Mvc.
[更新]依赖关系图
现在,我已经找到了解决办法(再次感谢,达林季米特洛夫!)我想与大家分享当前的依赖关系图。
Now that I've found a solution (thanks again, Darin Dimitrov!) I'd like to share the current dependency diagram with you.
推荐答案
您应在MVC项目配置DI容器。这是一切进入现场。这其中所有的组件必须当然也包括数据访问层引用(没有引用具体的数据访问MVC应用程序根本无法工作)。所以MVC应用程序知道所有其他层。 的Application_Start
在Global.asax中是配置一个DI容器的好地方。
You should configure the DI container in the MVC project. This is where everything comes into live. This where all the assemblies must be referenced including the data access layer of course (without referencing a concrete data access your MVC application simply cannot work). So the MVC application knows all about the other layers. Application_Start
in Global.asax is a good place to configure a DI container.
这篇关于温莎城堡:注册横跨在溶液中多个项目的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!