【C#高级语法特性锦集】
https://blog.csdn.net/qq_34202873/article/details/91043366
C# IOC容器Unity
控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心。 控制反转一般分为两种类型,依赖注入(Dependency Injection,简称DI)和依赖查找(Dependency Lookup)。依赖注入应用比较广泛。
unity组件网址:http://unity.codeplex.com/
1. 先加载相关依赖项:NuGet
!!关键一点必须注意!!
用Unity.Interception 5.1.0(别用最新的,一大堆神BUG)
总共需要以下四个,直接安装
2. 问题描述
先准备几个类
/// <summary> /// 班级接口 /// </summary> public interface IClass { string ClassName { get; set; } void ShowInfo(); } /// <summary> /// 计科班 /// </summary> public class CbClass : IClass { public string ClassName { get; set; } public void ShowInfo() { Console.WriteLine("计科班:{0}", ClassName); } } /// <summary> /// 电商班 /// </summary> public class EcClass : IClass { public string ClassName { get; set; } public void ShowInfo() { Console.WriteLine("电商班:{0}", ClassName); } }
很多人初始化实例的时候就比如这样
CbClass cbclass = new CbClass();
使用依赖倒置原则的话则是这样
IClass class = new CbClass();
好一点的就再建个简单工厂封装一下
var class = Factory.CreateClass();
3. 问题解决
但类一旦多了以后,每增加类你都得修改工厂,这样就变得很繁杂了,这里使用依赖注入解决这个问题
public static void ContainerCodeTest1() { IUnityContainer container = new UnityContainer();
//默认注册(无命名),如果后面还有默认注册会覆盖前面的 container.RegisterType<IClass, CbClass>(); //命名注册 container.RegisterType<IClass, EcClass>("ec"); //解析默认对象 IClass cbClass = container.Resolve<IClass>(); cbClass.ShowInfo(); //指定命名解析对象 IClass ecClass = container.Resolve<IClass>("ec"); ecClass.ShowInfo(); //获取容器中所有IClass的注册的已命名对象 IEnumerable<IClass> classList = container.ResolveAll<IClass>(); foreach (var item in classList) { item.ShowInfo(); }
4. 使用配置文件解决
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<!--定义类型别名-->
<aliases>
<add alias="IClass" type="ConsoleApplication1.UnityDemo.IClass,ConsoleApplication1" />
<add alias="CbClass" type="ConsoleApplication1.UnityDemo.CbClass,ConsoleApplication1" />
<add alias="EcClass" type="ConsoleApplication1.UnityDemo.EcClass,ConsoleApplication1" />
</aliases>
<!--容器-->
<container name="FirstClass">
<!--映射关系-->
<register type="IClass" mapTo="CbClass"></register>
<register type="IClass" name="ec" mapTo="EcClass"></register>
</container>
</unity>
</configuration>
数据解析方法
public static void ContainerConfigurationTest1() { IUnityContainer container = new UnityContainer(); string configFile = "http://www.cnblogs.com/UnityDemo/Constructor/Unity.config"; var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFile }; //从config文件中读取配置信息 Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); //获取指定名称的配置节 UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
//载入名称为FirstClass 的container节点 container.LoadConfiguration(section, "FirstClass");
//解析默认对象
IClass cbClass = container.Resolve<IClass>();
cbClass.ShowInfo();
//指定命名解析对象
IClass ecClass = container.Resolve<IClass>("ec");
ecClass.ShowInfo();
//获取容器中所有IClass的注册的已命名对象
IEnumerable<IClass> classList = container.ResolveAll<IClass>();
foreach (var item in classList)
{
item.ShowInfo();
}
文章具体实现代码参考:http://www.cnblogs.com/qqlin/
转载自[C#/.NET IOC框架——Unity容器基础入门](https://blog.csdn.net/qq_34202873/article/details/85042409)转载:https://blog.csdn.net/sunzheng176/article/details/114204984
查看评论