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是 ...
随机推荐
- 基于vue+springboot+docker网站搭建【一】 前言
前言 开一个系列记录下一次从0开始搭建一个网站的过程.前后端项目都是在github找的开源项目,主要用于练习部署. 前端:vue.js 后端: spring-boot 搭建环境:centOS7.6 ...
- json串处理2
请求百度地址坐标:http://api.map.baidu.com/location/ip?ak=y0Yb5ZgGK9blTDbR7Dwh9jGtn6X1YE48&coor=bd09ll&am ...
- SpringJDBC源码解析
读完本篇文章需要很长很长时间.... 传统JDBC 相信大家对传统的jdbc已经很熟悉了,无非就是下面这个流程 1234567891011 //1.加载驱动程序Class.forName(" ...
- nginx配置多个TLS证书,以及TLS SNI简介
背景 原来申请的正式域名备案通过,TLS证书也申请了.之前使用的临时域名和证书作为测试环境使用.于是要在单个ECS主机上配置nginx多个证书和多个域名. 实践 nginx部署多个TLS证书很简单,在 ...
- Python检测URL状态
需求:Python检测URL状态,并追加保存200的URL 代码一: #! /usr/bin/env python #coding=utf-8 import sys import requests d ...
- iOS/Xcode异常:no visible @interface for XXX declares the selector YYY
在iOS/Xcode开发过程中,出现如下异常信息: no visible @interface for XXX declares the selector YYY 分析原因: There are lo ...
- vue+element打印页面功能
项目中遇到了要打印页面的功能,我感之前使用的是一个第三方的插件,但是不兼容IE,后来发现直接调用window的API就可以了,MMP // 打印表单 printBtn() { window.print ...
- 路由拨号上网过Drcom
学校校园宽带是Drcom认证的 ,一人一号一设备.用着难受就决定想破解. 开始想着用软路由,但是感觉对电脑不友好,所以就决定买个路由器来搞. 一丶环境说明 学校使用的是Drcom 6.0 P版客户端. ...
- wait waitpid
定义 pid_t wait(int *status); pid_t waitpid(pid_t pid, int *status, int options); 暂时停止进程的执行,直到有信号来到或子进 ...
- C# 连接数据操作的时候抛异常,连接超时
先说说我的业务.我在发送优惠券的时候,同时给6千多个会员发送优惠券,执行了update 和insert语句,这写语句都是通过字符串拼接而来的.update和insert语句加起来一共是一万多条语句.在 ...