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

Dependency Injection in ASP.NET MVC的更多相关文章

  1. Dependency Injection in ASP.NET Web API 2 (在web api2 中使用依赖注入)

    原文:http://www.asp.net/web-api/overview/advanced/dependency-injection 1 什么是依赖注入(Dependency Injection) ...

  2. Dependency Injection in ASP.NET Web API 2

    What is Dependency Injection? A dependency is any object that another object requires. For example, ...

  3. Dependency Injection in ASP.NET Web API 2 Using Unity

    What is Dependency Injection? A dependency is any object that another object requires. For example, ...

  4. Dependency Injection in ASP.NET Core

    Transient – A new instance of the service is created each time it is requested. It can be used for s ...

  5. [引]ASP.NET MVC 4 Content Map

    本文转自:http://msdn.microsoft.com/en-us/library/gg416514(v=vs.108).aspx The Model-View-Controller (MVC) ...

  6. [转]Implementing User Authentication in ASP.NET MVC 6

    本文转自:http://www.dotnetcurry.com/aspnet-mvc/1229/user-authentication-aspnet-mvc-6-identity In this ar ...

  7. ASP.NET MVC 6 一些不晓得的写法

    今天在看 Scott Guthrie 的一篇博文<Introducing ASP.NET 5>,在 MVC 6 中,发现有些之前不晓得的写法,这边简单记录下,算是对自己知识的补充,有些我并 ...

  8. C# Ioc ASP.NET MVC Dependency Injection

    ASP.NET MVC Dependency Injection 同志们,非常快速的Ioc注册接口和注入Mvc Controller,步骤如下: 安装Unity.Mvc NuGet Package 在 ...

  9. MVC Controller Dependency Injection for Beginners【翻译】

    在codeproject看到一篇文章,群里的一个朋友要帮忙我翻译一下顺便贴出来,这篇文章适合新手,也算是对MEF的一个简单用法的介绍. Introduction In a simple stateme ...

随机推荐

  1. PHP并发IO编程实践

    PHP相关扩展 Stream:PHP内核提供的socket封装 Sockets:对底层Socket API的封装 Libevent:对libevent库的封装 Event:基于Libevent更高级的 ...

  2. Node.js标准的回调函数

    Node.js标准的回调函数:第一个参数代表错误信息,第二个参数代表结果. function (err, data) 当正常读取时,err参数为null,data参数为读取到的String.当读取发生 ...

  3. win10下一分钟快速搭建rtmp推流服务器

    为了让大家少踩笔者踩过的坑,目前将工作中搭建rtmp推流服务器的步骤总结如下: 步骤1: 下载 nginx 1.7.11.3 Gryphon 下载链接: http://nginx-win.ecsds. ...

  4. 然而,该来的还是来了(Diary)

    2017-05-07     LG月赛成功炸掉...发现自己真的好多东西不会啊.对某些知识仅仅有最基础的一点理解啊!连线段树都理解不了怎么办?归并排序(including分治+贪心)全部炸掉啊.感觉自 ...

  5. win10、win7 使用centos配置网络,可以让Xshell进行连接,虚拟机进行上网;

    系统:window 10 虚拟机VMware® Workstation 15 Pro Linux版本:CentOS-6.3 前提:关闭防火墙 如果是win7 系统可以不用第八步,如果不行可以试一下第八 ...

  6. Codeforces 879A/B

    A. Borya's Diagnosis 传送门:http://codeforces.com/contest/879/problem/A 本题是一个模拟问题. 依次访问n个元素,第i个元素首次出现于s ...

  7. 【ZOJ 4067】Books

    [链接] 我是链接,点我呀:) [题意] [题解] 统计a中0的个数cnt0 然后m减去cnt0 因为这cnt0个0是一定会取到的. 如果m==0了 那么直接找到数组中的最小值mi 输出mi-1就好 ...

  8. TotoiseSVN使用教程

    TortoiseSVN百科 TortoiseSVN 是 Subversion 版本控制系统的一个免费开源客户端,可以超越时间的管理文件和目录.文件保存在中央版本库,除了能记住文件和目录的每次修改以外, ...

  9. 【函数式】Monads模式初探——Endofunctor

    自函子 自函子(Endofunctor)是一个将范畴映射到自身的函子(A functor that maps a category to itself). 函子是将一个范畴转换到另一个范畴.所以自函子 ...

  10. BNU 34986 Football on Table

    "Bored? Let's play table football!" The table football is played on a rectangular table, u ...