DI 依赖注入之unity(使用unity.mvc)

一.nuget下载安装:

使用Nuget安装Unity.MVC

安装完成后会在~/App_Start/目录下自动生成UnityMvcActivator.cs和UnityConfig.cs文件

二.配置:

打开UnityConfig文件,修改RegisterTypes()方法的代码

 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<IUserDAL, UserDAL>();
container.RegisterType<IUserBLL, UserBLL>();
}

二.使用:【注意对比之间的区别及实现方式,会比较容易学习】

1.代码方式注入

(1)构造函数注入(推荐):

public class UserController : Controller
{
public UserController(IUserBLL userBLL)
{
this.userBLL = userBLL;
} IUserBLL userBLL;
// GET: User
public ActionResult Index()
{
var list = userBLL.GetUserModels();
return View(list);
}
}

 无参数构造函数:切记增加特性:InjectionConstructor

(2)属性注入:

namespace ZLP.Web.Controllers
{
public class UserController : Controller
{
[Dependency]
public IUserBLL userBLL { get; set; }
// GET: User
public ActionResult Index()
{
var list = userBLL.GetUserModels();
return View(list);
}
}
}

错误:System.NullReferenceException:“未将对象引用设置到对象的实例。”

解决方法:

1.给要注入的属性增加Dependency特性,切记

2.引用是否是using Unity命名空间下的,别选错了(using System.Runtime.CompilerServices;)

3.属性的访问修饰符是否用public

(3)方法注入:

 IUserBLL userBLL;

        [InjectionMethod]
public void instance(IUserBLL userBLL)
{
this.userBLL = userBLL;
}

2.配置文件注入(推荐)

打开UnityConfig文件,修改RegisterTypes()方法的代码

 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<IUserDAL, UserDAL>();
//container.RegisterType<IUserBLL, UserBLL>(); //加载配置文件
container.LoadConfiguration();
//var section = (UnityConfigurationSection)ConfigurationManager.GetSection(UnityConfigurationSection.SectionName);
//container.LoadConfiguration(section);
}

配置文件配置:web.config

三.常见问题:

DI 依赖注入之unity(mvc)的更多相关文章

  1. DI 依赖注入之unity的MVC版本使用Microsoft.Practices.Unity1.2与2.0版本对比

    DI 依赖注入之unity的MVC版本使用Microsoft.Practices.Unity1.2与2.0版本对比 参考:https://www.cnblogs.com/xishuai/p/36702 ...

  2. AutoFac IoC DI 依赖注入

    AutoFac IoC DI 依赖注入 记录点点滴滴知识,为了更好的服务后来者! 一.为什么使用AutoFac? 之前介绍了Unity和Ninject两个IOC容器,但是发现园子里用AutoFac的貌 ...

  3. 控制反转、依赖注入、Unity容器

    控制反转原则 依赖注入 Install-Package Unity:https://www.nuget.org/packages/Unity/ Github:https://github.com/un ...

  4. 依赖注入与Unity(一) 介绍

        在你学习依赖注入和Unity之前,你需要明白你为什么要使用它们.为了明白为什么要使用它们,你应该明白依赖注入和Unity能够帮助你解决什么类型的问题.作为介绍部分,这一章不会涉及太多关于Uni ...

  5. 【依赖注入】Unity和Autofac

    全面理解ASP.NET Core依赖注入:https://www.cnblogs.com/jesse2013/p/di-in-aspnetcore.html MSDN:https://docs.mic ...

  6. DI 依赖注入之StructureMap框架

    DI  依赖注入之StructureMap框架 一.简叙: structureMap只是DI框架中的其中之一. 二.安装及使用: 1.懒人方法: 使用MVC5项目时,可以直接在nuget程序包中安装S ...

  7. IoC 依赖注入容器 Unity

    原文:IoC 依赖注入容器 Unity IoC 是什么? 在软件工程领域,“控制反转(Inversion of Control,缩写为IoC)”是一种编程技术,表述在面向对象编程中,可描述为在编译时静 ...

  8. 依赖注入之unity(winform方式)

    依赖注入之unity(winform方式) 要讲unity就必须先了解DI和IOC及DIP,如下链接提供DI和IOC的基础:https://www.cnblogs.com/zlp520/p/12015 ...

  9. 初识Spring框架实现IOC和DI(依赖注入)

    学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的, IoC是 ...

随机推荐

  1. 我是如何一步步编码完成万仓网ERP系统的(五)产品库设计 1.产品类别

    https://www.cnblogs.com/smh188/p/11533668.html(我是如何一步步编码完成万仓网ERP系统的(一)系统架构) https://www.cnblogs.com/ ...

  2. 费劲周折的Haskell开发环境搭建过程

    大概倒腾了一周才搭建好Haskell的开发环境,遇到了很多莫名其妙的问题. 首先,Haskell实在是够冷门,中文网站上的信息实在有限.仅有的一些安装教程分享都感觉不大靠谱,所以我还是直接去外网找吧. ...

  3. 24个Jvm面试题总结及答案

    1.什么是Java虚拟机?为什么Java被称作是“平台无关的编程语言”? Java虚拟机是一个可以执行Java字节码的虚拟机进程.Java源文件被编译成能被Java虚拟机执行的字节码文件. Java被 ...

  4. SpringBoot自定义Condition注解

        最近碰到个这样的需求,需要同一套代码适配个版本数据库(数据库不同,且部分表的字段及关联关系可能会不同),即这套代码配置不同的数据库都能跑.项目采用的框架为SpringBoot+Mybatis. ...

  5. qtp安装和使用

    QTP许可证密钥的破解步骤: 以前使用QTP9.2 使用此方法成功破解,现在本人使用的HP QuickTest Professional 11 英文版,也成功适用. 一.准备工作: 1. 由于注册码文 ...

  6. python3自动部署mariadb主从

    master import configparser import os def config_mariadb_yum(): exists = os.path.exists('/etc/yum.rep ...

  7. Window 2003 IIS + MySQL + PHP + Zend 环境配置

    图文详解 下载 Windows 2003 Zend, PHP, PHPMyadmin 与 MySQL Windows 2003 安装包中包含了 Zend,PHP 5.2.17,PHPWind8.7 和 ...

  8. MySQL事务优化

    ====================事务特性 事务隔离级别 事务控制语句 MySQL优化==================== 事务的概念 事务指逻辑上的一组操作,组成这组操作的各个单元,要么全 ...

  9. Leetcode79 Word Search

    题目描述 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed f ...

  10. Linux-grep,awk,sed

    grep 参考1:https://www.cnblogs.com/ITtangtang/p/3950497.html sed 参考:https://www.cnblogs.com/wangqiguo/ ...