原文引自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. luoguP4719 【模板】动态 DP 线段树+树链剖分+矩阵乘法+动态DP

    题目描述 给定一棵n个点的树,点带点权. 有m次操作,每次操作给定x,y,表示修改点x的权值为y. 你需要在每次操作之后求出这棵树的最大权独立集的权值大小. 输入输出格式 输入格式: 第一行,n,m分 ...

  2. 在vue中写一个跟着鼠标跑的div,div里面动态显示数据

    1.div应该放在body里面,这是我放在body中的一个div里面的div <!-- 信息查看层 --> <div class="floatDiv" :styl ...

  3. Linux基础:seq命令总结

    本文只总结一些常用的用法,更详细的说明见man seq和 seq --help. seq命令 seq命令用于输出数字序列. 语法格式 Usage: seq [OPTION]... LAST or: s ...

  4. [模板]FFT

    郝神并没有令我明白这个. 但是巨神的题解太强了. #include <iostream> #include <complex> #include <cmath> # ...

  5. layer 使用教程

    http://layer.layui.com/ <!DOCTYPE html><html lang="en"><head> <meta c ...

  6. python 对图片做垂直投影

    Python 对图片做垂直投影 本文利用opencv对图片进行垂直投影,做出垂直投影图,大体思路:打开图片,灰度化,二值化,按列进行统计,新建一个大小和原图一样的图片,按列进行填充: cv2.cv.G ...

  7. Django(六)

    一.一大波model操作 1. 创建数据库表 # 单表 # app01_user ==> tb1 # users class User(models.Model): name = models. ...

  8. System v和posix的IPC对比

    之前有一篇关于共享内存的System V和Posix的对比: http://www.cnblogs.com/charlesblc/p/6261469.html POSIX(Portable Opera ...

  9. AIX下sort命令简介及使用

    AIX下sort命令简介及使用 sort -rn +3 , r倒排序,n按照数字排序:  +3按照第四列排序: 第一列是+0: 学习:http://blog.csdn.net/chen_linbo/a ...

  10. GNUPlot绘制曲线

    发现gnuplot在mac上编译安装相当方便,在线下为了測试java老堆和lucene索引大小,须要绘制两条线,可是直接点连的线很难看,所以后面使用贝塞尔曲线. 脚本例如以下: #! /usr/loc ...