DAL ->BLL <- GUI + 组合根.如何设置DI绑定? [英] DAL -> BLL <- GUI + composition root. How to setup DI-bindings?
问题描述
我已经制作了一个三层应用程序,参考在这个答案:
I have made a three-layer application with refrences going as described in this answer:
DAL with Repositories -> BLL with services and IRepository <- Asp.net mvc-app
为了通过依赖注入运行它,我看到了几个选项:
1. 从 Web 应用程序添加对 DAL 的引用,以便能够在应用程序启动时设置绑定.
2.使用带有xml-configuration的容器
(3.使用反射加载dal-assembly并查找类型)
To get this running with dependency injection I see a few options:
1. Add a reference to DAL from the web-app to be able to setup bindings on application start.
2. Use a container with xml-configuration
(3. Use reflection to load the dal-assembly and find types)
选项 1. 很简单,并且还可以将 DAL.dll 复制到 bin 中,但后来我突然重新引入了我努力摆脱的引用.现在可以直接访问存储库.选项 2 和 3 似乎不必要地复杂.
Option 1. is easy and also makes the DAL.dll be copied to bin but then I suddenly reintroduce the reference I worked so hard to get rid of. The repositories can now be accessed directly. Option 2 and 3 seems unnecessarily complex.
没有别的办法了吗?
推荐答案
Mark Seemann 的回答给了我这个变体的想法:
Mark Seemann's answer gave me the idea for this variant:
DAL with Repositories -> BLL with services and IRepository <- Asp.net mvc-app
^------------------------^--------- Composition Root <-------´
这是为了说明,不是让 Web 项目引用 DAL,而是引用一个单独的 Composition Root 项目,该项目同时引用 DAL 和 BLL.composition-root-project 有一个类,其中包含一个定义绑定的方法.它提供了这些额外的好处:
This is meant to illustrate that instead of letting the Web project reference the DAL it references a separate Composition Root-project that references both DAL and BLL. The composition-root-project has a single class with one method that define the bindings. It gives these additional benefits:
- 只有三层.四层在团队中很难卖.
- 确保松耦合.无法从视图代码访问 DAL.
- 更好的工具支持.控制器保留在标准位置,因此可以在上下文菜单上访问添加控制器",并且控制器代码中会突出显示缺少的视图.此外,无需配置或编写自定义控制器工厂.
我没有看到任何大的缺点.
I don't see any big drawbacks.
这篇关于DAL ->BLL <- GUI + 组合根.如何设置DI绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!