原文引自http://www.dotnetcurry.com/ShowArticle.aspx?ID=786

1.传统三层结构,相邻两层之间交互;

2.如果使用EntityFramework则View层直接与Db层交互,如在Controller中定义DbContext操作数据库,属于紧耦合;

3.解决2中紧耦合的方法:1)定义IRepository(CRUD),此接口在对应数据层实现(DbContext);2)Controller中只使用IRepository实例出的相应Repository,进而达到松耦合的目的;

4.Controller默认使用无参构造函数,不能以传递参数的方式实例化Controller中的Repository;

5.创建ControllerFactory,实现DefaultControllerFactory接口,以工厂的形式产生Controller实例集合;下图展示了可以以传递参数的方法实例化Controller(即Constructor injection for MVC);

6.万事俱备,只欠东风的一步(All that is remaining is creation of the CompositionRoot and hooking it up with the application start).

CompositionRoot使用Public构造函数,调用Static方法实例化private的IControllerFactory对象;The CreateControllerFactory() method is where our dependency instantiation happens(真正的依赖just from here,add appSettings key into web.config);

7.用CompositionRoot的对象实例化ControllerFactory:

____________________________________________recap下_____________________________________________________

Recap

- Dependency Injection is a Design pattern that enables us to write loosely coupled code (Ref: Dependency Inject in .NET By Mark Seemann) and enforces, dependent object give up control of managing their dependencies and instead let a Composition Root inject the dependencies into them.

- Using a Composition Root in a manner described above is often referred to as ‘Poor man’s DI’ pattern. This is not so much of a bad thing as it is an indicator of lack of Inversion of Control Containers. IoC Containers help in taking away the pain of registering each new object and managing their instances. Some common IoC containers for .NET are Castle Windsor, Unity, Ninject. We will discuss IoC containers in one of our future article.

- DI + Repository + Programming against Interfaces overall help in separation of concerns and greatly improves the overall application maintainability.

- The above design has zero impact on change to View or Data layer.

For example, if we were to introduce a new Mobile view we could simply go and build it from scratch while using the exact same Domain and Data Access Layer. All we had to do was ensure the dependencies are created correctly at the Composition Root. Similarly to swap the Sql backend with an Azure backend there would be zero impact on the business layer or the View layer. All that would change was the configuration information providing the concrete type for the Azure repository implementation. In any other design the impact of such changes is much more pervasive and requires touching all layers of an application.

最新文章

  1. Android快乐贪吃蛇游戏实战项目开发教程-03虚拟方向键(二)绘制一个三角形
  2. 湘潭1247 Pair-Pair(树状数组)
  3. git merge 和 rebase 区别
  4. SVN上传代码
  5. Codeforces Round #277 (Div. 2)
  6. php防止ddos,dns,集群攻击的实现代码
  7. Javascript之获取屏幕宽高
  8. spoj 394
  9. javascript基础学习(一)
  10. 经常使用的DB2命令(2)
  11. CQRS(命令查询职责分离)和 EDA(事件驱动架构)
  12. dom03
  13. 使用Blender的UV映射制作一个地球
  14. 以pfile或者spfile启动时show parameter pfile的不同结果
  15. vue调试神器vue-devtools安装
  16. 创建的UIWindow为何不显示
  17. CISCO静态路由配置
  18. Python全栈开发-Day11-RabbitMQ/Redis
  19. mysql系统变量查询
  20. 【leetcode 简单】第二十一题 相同的树

热门文章

  1. KVM之virsh管理虚拟机内存
  2. C#使用OracleDataReader返回DataTable
  3. 雪花算法生成全局唯一ID
  4. Day 17 time,datetime,random,os,sys,json,pickle
  5. 【剑指Offer】3、从尾到头打印链表
  6. VUE常见问题解决
  7. 使用Linux自带的命令logrotate对Nginx日志进行切割
  8. 【ZOJ 4060】Flippy Sequence
  9. MariaDB 10.x 将包含多主复制功能
  10. [bzoj3680]吊打XXX_模拟退火