原文引自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.