DI 依赖注入之unity(mvc)
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)的更多相关文章
- 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 ...
- AutoFac IoC DI 依赖注入
AutoFac IoC DI 依赖注入 记录点点滴滴知识,为了更好的服务后来者! 一.为什么使用AutoFac? 之前介绍了Unity和Ninject两个IOC容器,但是发现园子里用AutoFac的貌 ...
- 控制反转、依赖注入、Unity容器
控制反转原则 依赖注入 Install-Package Unity:https://www.nuget.org/packages/Unity/ Github:https://github.com/un ...
- 依赖注入与Unity(一) 介绍
在你学习依赖注入和Unity之前,你需要明白你为什么要使用它们.为了明白为什么要使用它们,你应该明白依赖注入和Unity能够帮助你解决什么类型的问题.作为介绍部分,这一章不会涉及太多关于Uni ...
- 【依赖注入】Unity和Autofac
全面理解ASP.NET Core依赖注入:https://www.cnblogs.com/jesse2013/p/di-in-aspnetcore.html MSDN:https://docs.mic ...
- DI 依赖注入之StructureMap框架
DI 依赖注入之StructureMap框架 一.简叙: structureMap只是DI框架中的其中之一. 二.安装及使用: 1.懒人方法: 使用MVC5项目时,可以直接在nuget程序包中安装S ...
- IoC 依赖注入容器 Unity
原文:IoC 依赖注入容器 Unity IoC 是什么? 在软件工程领域,“控制反转(Inversion of Control,缩写为IoC)”是一种编程技术,表述在面向对象编程中,可描述为在编译时静 ...
- 依赖注入之unity(winform方式)
依赖注入之unity(winform方式) 要讲unity就必须先了解DI和IOC及DIP,如下链接提供DI和IOC的基础:https://www.cnblogs.com/zlp520/p/12015 ...
- 初识Spring框架实现IOC和DI(依赖注入)
学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的, IoC是 ...
随机推荐
- CountDownEvent 信号类来等待直到一定数量的操作完成
当主程序启动时,创建一个 CountDownEvent 类的实例,在其构造函数中指定个数操作完成发出信号,当前为2个操作完成会发出信号. /// <summary> /// 创建 Coun ...
- Vert.x(vertx) 认证和授权
每个线上系统几乎都是离不开认证和授权的,Vert.x提供了灵活.简单.便捷的认证和授权的支持.Vert.x抽象出了两个核心的认证和授权的接口,一个是 AuthProvider,另一个是User.通过这 ...
- 1-RocketMq 学习 中文文档(一)
原文:https://blog.csdn.net/weixin_40533111/article/details/84451096 1.基本概念及优势 rocketmq是一个基于发布订阅队列模型的消息 ...
- P3028 汽水机(差分)
题目 P3028 [USACO10OCT]汽水机Soda Machine 解析 差分,看到\(a[i]\leq 1e9\),离散化一下,在\(l\)处\(+1\),\(r+1\)处\(-1\),这样就 ...
- Win10 默认用Windows照片查看程序打开图片
::复制以下内容到记事本: @echo off&cd\&color 0a&cls echo 恢复Win10照片查看器 reg add "HKLM\SOFTWARE\M ...
- <Android Studio> 3.打包APK
我的IDE版本是 3.5 我希望输出的apk文件格式是: 名称_v版本_release/debug_日期 时间.apk 步骤: 1.打开build.gradle 末尾添加如下代码 def releas ...
- elasticsearch安装中文分词器插件smartcn
原文:http://blog.java1234.com/blog/articles/373.html elasticsearch安装中文分词器插件smartcn elasticsearch默认分词器比 ...
- Natas25-writeup
前言 题目链接: http://natas25.natas.labs.overthewire.org 做这一题花了一些时间,也是由于自己知识点掌握不足,所以分享下解题过程. 题目分析 首先,登录后看到 ...
- 字符串比较==和equals的区别
<Stack Overflow 上 370万浏览量的一个问题:如何比较 Java 的字符串?> 比较详细的比较了==和equals方法的区别. 那借此机会,我就来梳理一下 Stack Ov ...
- centos安装安全狗提示Need system command 'locate' to install safedog for linux的解决方法
今天为客户的centos服务器安装安全狗时提示Need system command 'locate' to install safedog for linux.Installation aborte ...