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 ...
随机推荐
- BZOJ 4195: [Noi2015]程序自动分析 并查集 + 离散化 + 水题
TM 读错题了...... 我还以为是要动态询问呢,结果是统一处理完了再询问...... 幼儿园题,不解释. Code: #include<bits/stdc++.h> #define m ...
- 死磕itchat源码--config.py
itchat的配置文件,源码: import os, platform # 版本及微信的url,二维码等 VERSION = '1.3.10' BASE_URL = 'https://login.we ...
- PAT_A1125#Chain the Ropes
Source: PAT A1125 Chain the Ropes (25 分) Description: Given some segments of rope, you are supposed ...
- C#datetime判断日期输入是否正确
//7.输入年月日,看看格式是否正确.利用DateTime. //(1) //DateTime dt=DateTime.Now; //Console.Write("请输入现在的年:" ...
- 【2】Django安装
**万物负阴而抱阳,冲气以为和 ** ——老子<道德经> 我们静下心态,开始我们的Django之旅 本节内容 Django的安装 安装结果验证 了解官方文档 1. 安装Django 我们强 ...
- jquery怎样获取多个复选框的值?
jquery的遍历方法可以获取复选框所欲的选中值 1 2 $("input:checkbox:checked").each(function(index,element)); ...
- 利用Socket 客户端---->服务端 传送文件到指定路径,并返回一个友好的回馈
首先盲写的一个传输文件的方法,但测试发现了一个非常不容易发现的问题,这里先说明一下. 错误的代码如下: package com.TCP.java; import java.io.File; impor ...
- 0301mysql数据库建表情况
转自博客:http://blog.csdn.net/dreamcode/article/details/8557197 一. 表设计 库名.表名.字段名必须使用小写字母,“_”分割. 库名.表名.字段 ...
- DLR概念
参考文章 动态语言运行时(Dynamic Language Runtime,DLR)是一套基于.NET的类库,它的作用是简化在CLR上开发动态语言的工作,例如DLR中提供了表达式树的创建,代码生成.优 ...
- 洛谷 P2896 [USACO08FEB]一起吃饭Eating Together
P2896 [USACO08FEB]一起吃饭Eating Together 题目描述 The cows are so very silly about their dinner partners. T ...