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是 ...
随机推荐
- WPF 精修篇 非UI进程后台更新UI进程
原文:WPF 精修篇 非UI进程后台更新UI进程 <Grid> <Grid.RowDefinitions> <RowDefinition Height="11* ...
- .Net Core实战教程(二):设置Kestrel的IP与端口的几种方法
.Net Core实战教程(二):设置Kestrel的IP与端口的几种方法 1.直接写在代码方式 Program.cs代码如下: using System; using System.Collecti ...
- 3-RocketMQ 简单梳理 及 集群部署笔记
原文:https://www.cnblogs.com/kevingrace/p/9015836.html 一.RocketMQ 基础知识介绍Apache RocketMQ是阿里开源的一款高性能.高吞吐 ...
- spring data jpa hql动态查询案例
目的:根据入参条件不同,动态组装hql里的where语句. 1. 实现代码 public List<WrapStatis> queryStatisCriteriaBuilder(Strin ...
- 下载及安装Python详细步骤
安装python分三个步骤: *下载python *安装python *检查是否安装成功 1.下载Python (1)python下载地址https://www.python.org/download ...
- Python基础13
<玩1>中关于病假.事假的问题,说得不完全正确. 实际为哑变量. 有关看待问题的维度.出发点(即屁股在哪里) 转哑变量后可以提高模型精度. 机器学习不怕字段过多. 转哑变量是在增维.
- css层叠规则(层叠样式表)
CSS层叠规则: 1.找出所有相关的规则,这些规则都包含与一个给定元素匹配的选择器. 2.按权重(!important)和来源对应用到给定元素的所有声明进行排序. 3.按特殊性对应用到给定元素的所有声 ...
- OpenInstall实现APP无邀请码推广
1.登录OpenInstall网站,这里会为你创建一个AppKey,而这个东西在web页面会用到. 2.在推广页面中加入推广下载. <script type="text/javascr ...
- elasticsearch 索引和mapping导入导出命令
导mapping:elasticdump \ --input=http://192.168.102.13:9200/search_v1 \ --output=http://192.168.102.69 ...
- django+sqlite3进行web开发(一)
服务器配置 安装django sudo apt-get install python-django -y 安装mysql(可选) 也可以直接使用sqlite sudo apt-get install ...