IOC容器:Unity
一、什么是IOC
学习IOC之前先来了解一个依赖导致原则(DIP),依赖导致原则是IOC的核心原理。
依赖导致:即上层模块不应该依赖于低层模块,二者应该通过抽象来依赖。依赖于抽象,而不是依赖于细节。
首先来看下面的例子:
1、定义一个接口,封装数据库的基本CRUD操作,接口定义如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Data; 7 8 namespace DataBase.Interface 9 { 10 /// <summary> 11 /// 数据访问接口 12 /// </summary> 13 public interface IDbInterface 14 { 15 string Insert(); 16 string Delete(); 17 string Update(); 18 string Query(); 19 } 20 }
2、定义一个MSSQL类实现该接口,用来模仿SQLServer操作,MSSQL类定义如下:
1 using DataBase.Interface; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace DataBase.MSSQL 9 { 10 public class DbMSSQL : IDbInterface 11 { 12 public string Delete() 13 { 14 return "MSSQL执行删除"; 15 } 16 17 public string Insert() 18 { 19 return "MSSQL执行插入"; 20 } 21 22 public string Query() 23 { 24 return "MSSQL执行查询"; 25 } 26 27 public string Update() 28 { 29 return "MSSQL执行更新"; 30 } 31 } 32 }
3、定义一个Oracle类实现该接口,模仿Oracle的操作,Oracle类定义如下:
1 using DataBase.Interface; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace DataBase.Oracle 9 { 10 public class DbOracle : IDbInterface 11 { 12 public string Delete() 13 { 14 return "Oracle执行删除"; 15 } 16 17 public string Insert() 18 { 19 return "Oracle执行插入"; 20 } 21 22 public string Query() 23 { 24 return "Oracle执行查询"; 25 } 26 27 public string Update() 28 { 29 return "Oracle执行更新"; 30 } 31 } 32 }
4、定义一个控制台应用程序来调用:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using DataBase.Interface; 7 using DataBase.MSSQL; 8 9 namespace IOCConApp 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 // 常规做法,即程序的上端,依赖于下端,依赖于细节 16 DbMSSQL mssql = new DbMSSQL(); 17 } 18 } 19 }
常规做法是添加引用,然后直接实例化类,但是这样会依赖于细节实现,现将代码修改如下:
1 2 | // 通过抽象来依赖 IDbInterface dbInterface = new DbMSSQL(); |
但是这样修改以后,虽然左边是抽象了,但是右边还是依赖于细节。
那就究竟什么是IOC呢?
IOC(Inversion of Control)即控制反转,是一个重要的面向对象编程的法则来消减程序之间的耦合问题,把程序中上层对下层依赖,转移到一个第三方容器中来装配。IOC是程序设计的目标,实现方式包含依赖注入和依赖查找,在.net中只有依赖注入。
说到IOC,就不能不说DI。DI:即依赖注入,是IOC的实现手段。
二、使用Unity实现IOC
Unity是一个IoC容器,用来实现依赖注入(Dependency Injection,DI),减少耦合的,Unity出自于伟大的微软。
unity组件网址:http://unity.codeplex.com/
unity能够做什么呢,列举部分如下:
1.Unity支持简单对象创建,特别是分层对象结构和依赖,以简化程序代码。其包含一个编译那些可能存在依赖于其他对象的对象实例机制。
2.Unity支持必要的抽象,其允许开发者在运行时或配置去指定依赖关系同时可以简单的管理横切点(AOP)。
3.Unity增加了推迟到容器组件配置的灵活性。其同样支持一个容器层次的结构。
4.Unity拥有服务定位能力,对于一个程序在许多情况下重复使用组件来分离和集中功能是非常有用的。
5.Unity允许客户端储存或缓存容器。对于在ASP.NET Web applications中开发者将容器持久化于ASP.NET中的session或application中特别有效。
6.Unity拥有拦截能力,其允许开发者通过创建并执行handlers(在方法或属性被调用到达之前)来为已存在的组件增加一个函数,并再次为返回调用结果。
7.Unity可以从标准配置系统中读取配置信息,例如:XML文件,同时使用配置文件来配置容器。
8.Unity支持开发者实现自定义容器扩展,例如:你可以实现方法来允许额外的对象构造和容器特征,例如缓存。
9.Unity允许架构师和开发者在现代化的程序中更简单的实现通用设计模式。
什么情况下要使用unity呢?
1.所构建的系统依赖于健全的面向对象原则,但是大量不同的代码交织在一起而难以维护。
2.构建的对象和类需要依赖其他对象或类。
3.依赖于复杂的或需要抽象的对象。
4.希望利用构造函数、方法或属性的调用注入优势。
5.希望管理对象实例的生命周期。
6.希望能够在运行时管理并改变依赖关系。
7.希望在拦截方法或属性调用的时候生成一个策略链或管道处理容器来实现横切(AOP)任务。
8.希望在Web Application中的回发操作时能够缓存或持久化依赖关系。
1、程序中安装Unity
使用管理NuGet程序包来安装Unity,在项目上右键,选择管理NuGet程序包:
在搜索框里面输入Unity,点击右侧安装按钮进行安装:
出现以下信息表示安装成功:
2、使用Unity实现DI
先来看看最简单的Unity实现方式:
IUnityContainer container = new UnityContainer();//1、定义一个空容器 container.RegisterType<IDbInterface, DbMSSQL>();//2、注册类型,表示遇到IDbInterface的类型,创建DbMSSQL的实例 var db = container.Resolve<IDbInterface>(); Console.WriteLine(db.Insert()); Console.ReadKey();
结果:
从结果中可以看出,db是DbMSSQL类型的实例。
除了使用RegisterType注册类型以外,还可以注册一个实例,例如:
// 使用RegisterInstance注册IDbInterface的实例:new DbMSSQL() container.RegisterInstance<IDbInterface>(new DbMSSQL());
3、三种注入方式
三种注入方式:构造函数注入、属性注入、方法注入。
3.1 定义IHeadphone接口,代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace DataBase.Interface 8 { 9 public interface IHeadphone 10 { 11 12 } 13 }
3.2 定义IMicrophone接口,代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace DataBase.Interface 8 { 9 public interface IMicrophone 10 { 11 12 } 13 }
3.3 定义IPower接口,代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace DataBase.Interface 8 { 9 public interface IPower 10 { 11 12 } 13 }
3.4 定义IPhone接口,代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace DataBase.Interface 8 { 9 public interface IPhone 10 { 11 void Call(); 12 IMicrophone iMicrophone { get; set; } 13 IHeadphone iHeadphone { get; set; } 14 IPower iPower { get; set; } 15 } 16 }
3.5 分别实现上面定义的接口
IPhone接口的实现如下:
1 using DataBase.Interface; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 using Unity.Attributes; 8 9 namespace DataBase.MSSQL 10 { 11 public class ApplePhone : IPhone 12 { 13 [Dependency]//属性注入 14 public IMicrophone iMicrophone { get; set; } 15 public IHeadphone iHeadphone { get; set; } 16 public IPower iPower { get; set; } 17 18 [InjectionConstructor]//构造函数注入 19 public ApplePhone(IHeadphone headphone) 20 { 21 this.iHeadphone = headphone; 22 Console.WriteLine("{0}带参数构造函数", this.GetType().Name); 23 } 24 25 public void Call() 26 { 27 Console.WriteLine("{0}打电话", this.GetType().Name); ; 28 } 29 30 [InjectionMethod]//方法注入 31 public void Init1234(IPower power) 32 { 33 this.iPower = power; 34 } 35 } 36 }
IHeadphone接口的实现如下:
1 using DataBase.Interface; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace DataBase.MSSQL 9 { 10 public class Headphone : IHeadphone 11 { 12 public Headphone() 13 { 14 Console.WriteLine("Headphone 被构造"); 15 } 16 } 17 }
IMicrophone接口的实现如下:
1 using DataBase.Interface; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace DataBase.MSSQL 9 { 10 public class Microphone : IMicrophone 11 { 12 public Microphone() 13 { 14 Console.WriteLine("Microphone 被构造"); 15 } 16 } 17 }
IPower接口的实现如下:
1 using DataBase.Interface; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace DataBase.MSSQL 9 { 10 public class Power : IPower 11 { 12 public Power() 13 { 14 Console.WriteLine("Power 被构造"); 15 } 16 } 17 }
控制台程序调用:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using DataBase.Interface; 7 using DataBase.MSSQL; 8 using Unity; 9 10 namespace IOCConApp 11 { 12 /// <summary> 13 /// IOC():控制反转,把程序上层对下层的依赖,转移到第三方的容器来装配 14 /// 是程序设计的目标,实现方式包含了依赖注入和依赖查找(.net里面只有依赖注入) 15 /// DI:依赖注入,是IOC的实习方式。 16 /// </summary> 17 class Program 18 { 19 static void Main(string[] args) 20 { 21 #region MyRegion 22 //// 常规做法,即程序的上端,依赖于下端,依赖于细节 23 //DbMSSQL mssql = new DbMSSQL(); 24 25 //// 通过抽象来依赖 26 //IDbInterface dbInterface = new DbMSSQL(); 27 28 //IUnityContainer container = new UnityContainer();//1、定义一个空容器 29 //container.RegisterType<IDbInterface, DbMSSQL>();//2、注册类型,表示遇到IDbInterface的类型,创建DbMSSQL的实例 30 //var db = container.Resolve<IDbInterface>(); 31 32 //// 使用RegisterInstance注册IDbInterface的实例:new DbMSSQL() 33 //container.RegisterInstance<IDbInterface>(new DbMSSQL()); 34 //Console.WriteLine(db.Insert()); 35 #endregion 36 37 IUnityContainer container = new UnityContainer(); 38 container.RegisterType<IPhone, ApplePhone>(); 39 container.RegisterType<IMicrophone, Microphone>(); 40 container.RegisterType<IHeadphone, Headphone>(); 41 container.RegisterType<IPower, Power>(); 42 43 IPhone phone = container.Resolve<IPhone>(); 44 45 Console.WriteLine($"phone.iHeadphone==null? {phone.iHeadphone == null}"); 46 Console.WriteLine($"phone.iMicrophone==null? {phone.iMicrophone == null}"); 47 Console.WriteLine($"phone.iPower==null? {phone.iPower == null}"); 48 49 Console.ReadKey(); 50 } 51 } 52 }
输出结果:
从输出结果中可以看出三种注入方式的执行顺序:先执行构造函数注入,在执行属性注入,最后执行方法注入。
注意:默认情况下如果构造函数上面没有使用特性,那么默认找参数最多的构造函数执行注入。
4、一个接口多个实现进行注册
如果多个不同的实例实现同一个接口,这种情况该怎么注册呢?先来看看下面的代码:
1 IUnityContainer container = new UnityContainer();//1、定义一个空容器 2 container.RegisterType<IDbInterface, DbMSSQL>();//2、注册类型,表示遇到IDbInterface的类型,创建DbMSSQL的实例 3 container.RegisterType<IDbInterface, DbOracle>();//表示遇到IDbInterface的类型,创建DbMSSQL的实例 4 var db = container.Resolve<IDbInterface>(); 5 Console.WriteLine(db.Insert());
运行结果:
从运行结果中可以看出,后面注册的类型会把前面注册的类型给覆盖掉,那么该如何解决呢?可以通过参数的方式来解决,代码如下:
1 IUnityContainer container = new UnityContainer();//1、定义一个空容器 2 container.RegisterType<IDbInterface, DbMSSQL>("sql");//2、注册类型,表示遇到IDbInterface的类型,创建DbMSSQL的实例 3 container.RegisterType<IDbInterface, DbOracle>("oracle");//表示遇到IDbInterface的类型,创建DbMSSQL的实例 4 var sql = container.Resolve<IDbInterface>("sql"); 5 var oracle = container.Resolve<IDbInterface>("oracle"); 6 Console.WriteLine(sql.Insert()); 7 Console.WriteLine(oracle.Insert());
运行结果:
5、生命周期
生命周期及一个对象从创建到释放中间经过的时间。先看下面的代码:
1 IUnityContainer container = new UnityContainer(); 2 container.RegisterType<IDbInterface, DbMSSQL>(); 3 IDbInterface db1 = container.Resolve<IDbInterface>(); 4 IDbInterface db2 = container.Resolve<IDbInterface>(); 5 Console.WriteLine("HashCode:"+db1.GetHashCode().ToString()); 6 Console.WriteLine("HashCode:" + db2.GetHashCode().ToString()); 7 Console.WriteLine(object.ReferenceEquals(db1,db2));
结果:
表明db1和db2是两个不同的实例,即默认情况下生命周期是瞬时的,每次都是创建一个新的实例。
container.RegisterType<IDbInterface, DbMSSQL>(new TransientLifetimeManager());表示是瞬时生命周期,默认情况下即这种。
在看下面的代码:
1 IUnityContainer container = new UnityContainer(); 2 container.RegisterType<IDbInterface, DbMSSQL>(new ContainerControlledLifetimeManager()); 3 IDbInterface db1 = container.Resolve<IDbInterface>(); 4 IDbInterface db2 = container.Resolve<IDbInterface>(); 5 Console.WriteLine("HashCode:" + db1.GetHashCode().ToString()); 6 Console.WriteLine("HashCode:" + db2.GetHashCode().ToString()); 7 Console.WriteLine(object.ReferenceEquals(db1, db2));
结果:
上图的结果可以看出,db1和db2是同一个实例。
container.RegisterType<IDbInterface, DbMSSQL>(new ContainerControlledLifetimeManager())表示是容器单例,每次都是同一个实例。
6、使用配置文件实现
在上面的例子中,所有的例子都是一直在依赖于细节,那么怎么解决不依赖于细节呢?答案是只能使用配置文件,配置文件如下:
1 <configuration> 2 <configSections> 3 <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/> 4 </configSections> 5 <unity> 6 <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Unity.Interception.Configuration"/> 7 <containers> 8 <container name="testContainer"> 9 <!--逗号前面是接口类型的完全限定名:命名空间+接口名称,逗号后面是DLL文件的名称 name解决同一个接口不同实例问题--> 10 <register type="DataBase.Interface.IDbInterface,DataBase.Interface" mapTo="DataBase.MSSQL.DbMSSQL, DataBase.MSSQL" name="sql"/> 11 <register type="DataBase.Interface.IDbInterface,DataBase.Interface" mapTo="DataBase.Oracle.DbOracle, DataBase.Oracle" name="oracle"/> 12 </container> 13 </containers> 14 </unity> 15 </configuration>
注意:这个一个单独的配置文件,要把属性里面的复制到输出目录改为始终复制,那么这个配置文件才会生成到Debug目录里面。
程序如下:
1 ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); 2 fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "CfgFiles\\Unity.Config");//找配置文件的路径 3 Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); 4 UnityConfigurationSection section = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName); 5 IUnityContainer container = new UnityContainer(); 6 section.Configure(container, "testContainer"); 7 IDbInterface db = container.Resolve<IDbInterface>("sql"); 8 Console.WriteLine(db.Insert());
结果:
观察上面的代码,会发现,如果改成使用配置文件的方式实现的话,代码里面就不会依赖于细节了,只要一个接口类型。既然没有细节了,那么对项目进行如下的改造:把引用里面对细节的引用都去掉(即去掉DataBase.MSSQL和DataBase.Oracle),然后Debug文件夹里面没有这两个DLL了,但是这时需要把这两个DLL复制到Debug目录下面,否则程序运行的时候会找不到具体实现的类型。这样就意味着程序架构只依赖于接口。
引用里面只要对接口的引用了,没有对具体实现的引用。去掉了对细节的依赖。
注意:使用配置文件实现时,必须把接口的具体实现类复制到程序目录下面。
如果有额外添加了一种数据库,那么只需要修改配置文件,把新的实现类复制到程序目录下面即可实现程序的升级。
使用配置文件的时候,需要把UnityContainer容器定义为静态的,这样只需要读取一次配置文件即可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?