城堡温莎:在多个项目中注册组件解决方案 [英] Castle Windsor: Register components across multiple projects in solution

查看:167
本文介绍了城堡温莎:在多个项目中注册组件解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. Mvc

    我想使用Castle Windsor进行依赖注入,包括以下项目: strong> [ASP.NET MVC 3 Web应用程序]:表示层(取决于企业和模型

  2. 业务 [Class Library]:业务层(取决于数据访问和模型

  3. DataAccess [类库]:数据访问层(取决于模型)

  4. 模型 [类库]:模型层

在业务层有一个名为 PostService 的类实现 IPostService 管理博客帖子。 Mvc 项目的 PostsController 取决于 IPostService 。但是, PostService (相应的具体实现)本身取决于 IPostRepository



我在哪里配置Castle Windsor以返回 PostRepository 的实例来解析 IPostRepository Mvc 项目不了解DataAccess 项目。因此,我无法在 global.asax 或其他 Mvc 中的其他地方配置组件绑定。






[更新]依赖关系图



现在我已经找到一个解决方案Dimitrov!)我想和你分享当前的依赖关系图。



解决方案

您应该在MVC项目中配置DI容器。这就是一切都生活的地方。这里所有的程序集必须被引用,包括数据访问层当然(没有参考具体的数据访问,你的MVC应用程序根本无法工作)。所以MVC应用程序知道所有其他层。 Global.asax中的 Application_Start 是配置DI容器的好地方。


I would like to use Castle Windsor for dependency injection for my solution consisting of the following projects:

  1. Mvc [ASP.NET MVC 3 Web Application]: presentation layer (depends on Business and Models)
  2. Business [Class Library]: business layer (depends on DataAccess and Models)
  3. DataAccess [Class Library]: data access layer (depends on Models)
  4. Models [Class Library]: model layer

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.

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.


[Update] Dependency Diagram

Now that I've found a solution (thanks again, Darin Dimitrov!) I'd like to share the current dependency diagram with you.

解决方案

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屋!

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