ASP.NET MVC Dependency Injection

同志们,非常快速的Ioc注册接口和注入Mvc Controller,步骤如下:

安装Unity.Mvc NuGet Package 在你的项目中,打开Package Manager Console进行安装。

接下来我们可以看见两个类文件UnityConfig.cs 和 UnityMvcActivator.cs,接口注册需要在UnityConfig.cs里写你的接口实例,UnityMvcActivator.cs 它会帮助你自动resolver,放心吧,这个工具太无脑了,需要做的不多

1.注册接口

 public static class UnityConfig
{
#region Unity Container
private static Lazy<IUnityContainer> container =
new Lazy<IUnityContainer>(() =>
{
var container = new UnityContainer();
RegisterTypes(container);
return container;
}); /// <summary>
/// Configured Unity Container.
/// </summary>
public static IUnityContainer Container => container.Value;
#endregion /// <summary>
/// Registers the type mappings with the Unity container.
/// </summary>
/// <param name="container">The unity container to configure.</param>
/// <remarks>
/// There is no need to register concrete types such as controllers or
/// API controllers (unless you want to change the defaults), as Unity
/// allows resolving a concrete type even if it was not previously
/// registered.
/// </remarks>
public static void RegisterTypes(IUnityContainer container)
{
// NOTE: To load from web.config uncomment the line below.
// Make sure to add a Unity.Configuration to the using statements.
// container.LoadConfiguration(); // TODO: Register your type's mappings here.
// container.RegisterType<IProductRepository, ProductRepository>();
container.RegisterType<IRepository, RepositoryImpl>();
}

  2.注入接口到Controller

 public class HomeController : Controller
{
private IRepository _repository;
public HomeController(IRepository repository)
{
_repository = repository;
} public ActionResult Index()
{
var result = _repository.Test();
return View(result);
}

  3.启动Ioc初始化,代码在Global.asax

 public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
UnityConfig.RegisterTypes(new UnityContainer());
}
}

  少年F5启动你的程序,见证奇迹的时刻,最快速的搭建.net Mvc Ioc 接口实例以及Controller的依赖注入。

C# Ioc ASP.NET MVC Dependency Injection的更多相关文章

  1. 控制反转Inversion of Control (IoC) 与 依赖注入Dependency Injection (DI)

    控制反转和依赖注入 控制反转和依赖注入是两个密不可分的方法用来分离你应用程序中的依赖性.控制反转Inversion of Control (IoC) 意味着一个对象不会新创建一个对象并依赖着它来完成工 ...

  2. 建立MVC的依赖项注入 Setting up MVC Dependency Injection 精通ASP-NET-MVC-5-弗瑞曼

    The result of the three steps I showed you in the previous section is that the knowledge about the i ...

  3. Dependency Injection in ASP.NET MVC

    原文引自http://www.dotnetcurry.com/ShowArticle.aspx?ID=786 1.传统三层结构,相邻两层之间交互: 2.如果使用EntityFramework则View ...

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

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

  5. ASP.NET MVC IOC之Unity攻略

    ASP.NET MVC IOC之Unity攻略 一.你知道IOC与DI吗? 1.IOC(Inversion of Control )——控制反转 即依赖对象不在被依赖模块的类中直接通过new来获取 先 ...

  6. ASP.NET没有魔法——ASP.NET MVC IoC

    之前的文章介绍了MVC如何通过ControllerFactory及ControllerActivator创建Controller,而Controller又是如何通过ControllerBase这个模板 ...

  7. asp.net core 系列之Dependency injection(依赖注入)

    这篇文章主要讲解asp.net core 依赖注入的一些内容. ASP.NET Core支持依赖注入.这是一种在类和其依赖之间实现控制反转的一种技术(IOC). 一.依赖注入概述 1.原始的代码 依赖 ...

  8. ASP.NET没有魔法——ASP.NET MVC IoC代码篇

    上一篇文章主要以文字的形式介绍了IoC及其在ASP.NET MVC中的使用,本章将从以下几点介绍如何使用代码在ASP.NET MVC中实现依赖注入: ● AutoFac及安装 ● 容器的创建 ● 创建 ...

  9. Dependency Injection in ASP.NET Web API 2

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

随机推荐

  1. Eclipse中使用Maven新建 Servlet 2.5的 SpringMVC项目

    1.前言: 最近在学习SpringMVC框架,由于使用Eclipse创建的webAPP项目默认使用的还是比较旧的servlet2.3,而且默认使用的还是JDK1.5,所以便有一次开始了我的配置之路 2 ...

  2. 【转】adb操作命令详解及大全

    adb是什么?:adb的全称为Android Debug Bridge,就是起到调试桥的作用.通过adb我们可以在Eclipse中方面通过DDMS来调试Android程序,说白了就是debug工具.a ...

  3. 新浪新闻页面抓取(JAVA-Jsoup)

    1.使用gradle建立工程: 工程格式如下: include ':spider-demo' rootProject.name = 'my-spider-demo' settings def void ...

  4. hdu_1031_结构体排序

    题目很好理解,将列求和,取前k大的 我的代码思路:对列求和,后取出前k大的id加入结果数组,对比后面和第k大相同的评分id也加入到结果数组,最后对结果数组排序 代码: #include<cstd ...

  5. 说说 typedef 的那些事

    最近在复习数据结构时,经常看到 typedef 的身影,但始终不清楚 typedef 的用法具体时怎么样的,特地查阅<C Primer Plus 第5版>,并将查到的内容写出来供大家沟通学 ...

  6. [国嵌攻略][071][Coredump故障分析]

    Core Dump定义 Core Dump又叫核心转存.当程序在运行过程中发生异常,Linux系统可以把程序出错时的内存内容存储在一个core文件中,这个过程叫Core Dump. Core Dump ...

  7. 人生苦短,我用Python 第一篇

    一.变量名 自定义变量名只能用 数字.字母.下划线组成. 使用变量前,需对其先赋值 注意:1.变量名不能以数字开头:      2.不能是关键字:例如:'and', 'as', 'assert', ' ...

  8. 工作小结(关于webpack)

    今天在工作中遇到了一个新问题,是关于webpack的. 是这样的,我在项目中添加了一个新页面,修改完配置文件后,开始运行,刚开始并没有什么问题,很顺利,后来我又添加了一个页面,然后修改配置文件,然后运 ...

  9. 全国银行列表json格式

    var list=[ { value:'CDB', text:'国家开发银行' }, { value:'ICBC', text:'中国工商银行' }, { value:'ABC', text:'中国农 ...

  10. Spark性能调优之JVM调优

    Spark性能调优之JVM调优 通过一张图让你明白以下四个问题                1.JVM GC机制,堆内存的组成                2.Spark的调优为什么会和JVM的调 ...