Dependency Injection in ASP.NET MVC
原文引自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的更多相关文章
- Dependency Injection in ASP.NET Web API 2 (在web api2 中使用依赖注入)
原文:http://www.asp.net/web-api/overview/advanced/dependency-injection 1 什么是依赖注入(Dependency Injection) ...
- Dependency Injection in ASP.NET Web API 2
What is Dependency Injection? A dependency is any object that another object requires. For example, ...
- 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, ...
- 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 ...
- [引]ASP.NET MVC 4 Content Map
本文转自:http://msdn.microsoft.com/en-us/library/gg416514(v=vs.108).aspx The Model-View-Controller (MVC) ...
- [转]Implementing User Authentication in ASP.NET MVC 6
本文转自:http://www.dotnetcurry.com/aspnet-mvc/1229/user-authentication-aspnet-mvc-6-identity In this ar ...
- ASP.NET MVC 6 一些不晓得的写法
今天在看 Scott Guthrie 的一篇博文<Introducing ASP.NET 5>,在 MVC 6 中,发现有些之前不晓得的写法,这边简单记录下,算是对自己知识的补充,有些我并 ...
- C# Ioc ASP.NET MVC Dependency Injection
ASP.NET MVC Dependency Injection 同志们,非常快速的Ioc注册接口和注入Mvc Controller,步骤如下: 安装Unity.Mvc NuGet Package 在 ...
- MVC Controller Dependency Injection for Beginners【翻译】
在codeproject看到一篇文章,群里的一个朋友要帮忙我翻译一下顺便贴出来,这篇文章适合新手,也算是对MEF的一个简单用法的介绍. Introduction In a simple stateme ...
随机推荐
- PAT_A1144#The Missing Number
Source: PAT A1144 The Missing Number (20 分) Description: Given N integers, you are supposed to find ...
- 整理Crontab 定时计划
一. 什么是crontab? crontab命令常见于Unix和类Unix的操作系统之中,用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和 ...
- C#--in逆变-out协变
MSDN上的解释 协变保留兼容性,逆变与之相反 in的使用 个人理解:就是表明泛型就是可以逆变的(逆变就是大变小) // Contravariant interface. interface ICon ...
- Linux中tty是什么(tty1~7)
tty:终端设备的统称. tty一词源于Teletypes,或者teletypewriters,原来指的是电传打字机,是通过串行线用打印机键盘通过阅读和发送信息的东西,后来这东西被键盘与显示器取代,所 ...
- eclipse快捷键深入理解
1.Eclipse 中的重构功能使其成为了一个现代的 Java 集成开发环境 (IDE),而不再是一个普通的文本编辑器.使用重构,您可以轻松更改您的代码,而不必担心对别处造成破坏. 2.Eclipse ...
- git merge和git rebase的区别和异同
1.git merge和git rebase作用差不多,都是将远程代码和本地代码合并 2.git merge和git rebase作用差不多,都是将远程代码和本地代码合并 3.git merge ...
- LDA 两种含义
关于LDA有两种含义,一种是线性判别分析(Linear Discriminant Analysis),一种是概率主题模型:隐含狄利克雷分布(Latent Dirichlet Allocation,简称 ...
- 教你高速高效接入SDK——Unity统一接入渠道SDK(Android篇)
U8SDK的设计之初,就是为了可以支持各种游戏引擎开发的游戏,而不不过Android的原生平台.眼下一大半的手游,都是採用Unity3D和Cocos2dx开发,那么这里,我们就先来一步步给大家演示,用 ...
- Notepad++ 设置执行 lua 和 python
Notepad++ 设置执行 lua 和 python 一.设置 run -> 设置 cmd /k lua "$(FULL_CURRENT_PATH)" & PAUS ...
- windows下mysql5.6.20使用mysqldumpslow.pl分析慢日志
要想执行mysqldumpslow.pl(这是perl程序),下载perl编译器. 下载地址:http://pan.baidu.com/s/1i3GLKAp 就是ActivePerl_5.16.2.3 ...