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. Oracle数据库触发器

    第一次写触发器,浪费了一个小时,少了一个;编译不通过 当A表有更新或插入数据时,则触发器执行向B表插入对应条件的数据1 CREATE OR REPLACE TRIGGER Test -- 触发器名称 ...

  2. 性能监控工具的配置及使用 - Spotlight On Oracle(oracle)

    一.    Spotlight On Oracle(oracle)1.1.   工具简介Spotlight是一个强有力的Oracle数据库实时性能诊断工具,提供了一个直观的.可视化的数据库活动展现.S ...

  3. .Net Core实战教程(二):设置Kestrel的IP与端口的几种方法

    .Net Core实战教程(二):设置Kestrel的IP与端口的几种方法 1.直接写在代码方式 Program.cs代码如下: using System; using System.Collecti ...

  4. Redis基本使用(一)

    redis window系统的redis是微软团队根据官方的linux版本高仿的 官方原版: https://redis.io/ 中文官网:http://www.redis.cn 1 redis下载和 ...

  5. springmvc注解@Controller和@RequestMapping

    Spring从2.5版本引入注解,从而让开发者的工作变得非常的轻松 springmvc注解Controller org.springframework.stereotype.Controller注解类 ...

  6. Vue.js最佳实践--给大量子孙组件传值(provide/inject)

    开发中有个需求,有个Parent组件(例如div)下,输入框,下拉框,radiobutton等可编辑的子孙组件几百个,根据某个值统一控制Parent下面的所有控件的disabled状态 类似于这样,给 ...

  7. 【转】WPF 异步执行方法后对 UI 进行更新的几种方法

    使用 async/await 的情况: private async void Button_Click(object sender, RoutedEventArgs e) { (sender as B ...

  8. SQL Server 修改表结构(转载)

    SQL Server 修改表结构 本文链接:https://blog.csdn.net/petezh/article/details/81744374 查看指定表结构 exec sp_help Rep ...

  9. mybatis中的高级查询

    Mybatis作为一个ORM框架,肯定是支持sql高级查询的. 下面的一个案例来为大家详细讲解mybatis中的高级查询. 案例说明: 此案例的业务关系是用户,订单,订单详情与商品之间的关系. 以订单 ...

  10. uwsgi no python application found错误的解决(python3+centos6)

    近期在努力把自己的项目从python2转到python3上,因为生产环境无法抛弃centos7,所以只好在centos7上安装了python3.装好了python3,将python命令软连接改成pyt ...