C# Unity容器的使用
最简单的使用方式(记得安装Unity NuGet包呀)
Console.WriteLine("***************Unity容器的初步应用***************");
IUnityContainer container = new UnityContainer();//1 声明一个容器
container.RegisterType<IPhone, AndroidPhone>();//2 初始化容器 注册类型
IPhone phone = container.Resolve<IPhone>();//3 反射创建对象
phone.Call();
- 当你想使用一个接口 注册不同类型是 你需要指定名称 像下面这样 child、grandchild 便是指定的名称
container.RegisterType<AbstractPad, ApplePad>();//抽象类
container.RegisterType<AbstractPad, ApplePad>("child");//1对多
container.RegisterType<AbstractPad, ApplePadChild>("grandchild");//1对多
这样在你创建时 你可以像下面这样 指定创建
AbstractPad pad = container.Resolve<AbstractPad>();
var childPad = container.Resolve<AbstractPad>("child");
var grandchildPad = container.Resolve<AbstractPad>("grandchild");
否则 注册是会出现覆盖的情况的 如下
container.RegisterType<AbstractPad, ApplePad>();//抽象类
container.RegisterType<AbstractPad, ApplePadChild>();//覆盖的
若是此时你创建对象 则pad将是 ApplePadChild类型
var pad = container.Resolve<AbstractPad>();
- 另外 父子类也会出现覆盖现象 比如
container.RegisterType<AbstractPad, ApplePad>();//抽象类
container.RegisterType<ApplePad, ApplePadChild>();//父子类 会覆盖<AbstractPad, ApplePad> 因为这个也是AbstractPad
此时创建对象 则pad也是 ApplePadChild类型
var pad = container.Resolve<AbstractPad>();
三种注入方式
- 属性注入 添加标识[Dependency]
[Dependency]//属性注入:不错,但是有对容器有依赖
public IMicrophone iMicrophone { get; set; }
- 构造函数注入--标识 [InjectionConstructor]
[InjectionConstructor]//构造函数注入:最好的,默认找参数最多的构造函数 【插注:.net core 内置容器默认寻找最少参数构造参数】
public ApplePhone(IHeadphone headphone)
{
this.iHeadphone = headphone;
Console.WriteLine("{0}带参数构造函数", this.GetType().Name);
}
此处的[InjectionConstructor]标识可省略 省略后 默认找参数最多的构造函数
- 方法注入--标识[InjectionMethod]
[InjectionMethod]//方法注入:最不好的,增加一个没有意义的方法,破坏封装
public void Init1234(IPower power)
{
this.iPower = power;
}
生命周期管理
//容器成了对象创建的入口,可以加入自己的管理逻辑:生命周期
IUnityContainer container = new UnityContainer();
container.RegisterType<IPhone, AndroidPhone>();//默认 瞬时
container.RegisterType<IPhone, AndroidPhone>(new TransientLifetimeManager());//默认 瞬时 每一次都是全新生成
container.RegisterType<IPhone, AndroidPhone>(new ContainerControlledLifetimeManager());//容器单例 单例就需自己实现
//container.RegisterType<IPhone, AndroidPhone>(new PerThreadLifetimeManager());//线程单例:相同线程的实例相同 不同线程的实例不同 web请求/多线程操作
container.RegisterType<IPhone, AndroidPhone>(new HierarchicalLifetimeManager());//分级容器单例
IUnityContainer childContainer = container.CreateChildContainer();//获取子容器
container.RegisterType<IPhone, AndroidPhone>(new ExternallyControlledLifetimeManager()); //外部可释放单例 单例是全局唯一;一旦释放大家都没了;
container.RegisterType<IPhone, AndroidPhone>(new PerResolveLifetimeManager());//循环引用 不推荐
配置文件使用
//不想要类;但是程序运行又必须要细节;配置文件
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "CfgFiles\\Unity.Config");//找配置文件的路径
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
UnityConfigurationSection section = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);
IUnityContainer container = new UnityContainer();
section.Configure(container, "testContainer");//配置文件节点名
IPhone phone = container.Resolve<IPhone>();
phone.Call();
IPhone android = container.Resolve<IPhone>("Android");
android.Call();
Aop配置
<container name="testContainerAOP">
<extension type="Interception"/>
<register type="Ruanmou.Interface.IPhone,Ruanmou.Interface" mapTo="Ruanmou.Service.AndroidPhone, Ruanmou.Service.Extend">
<interceptor type="InterfaceInterceptor"/>//以下是AOP插入工作
<interceptionBehavior type="Ruanmou.Framework.AOP.AuthorizeBehavior, Ruanmou.Framework"/>//type=类名, 类所在dll名称
<interceptionBehavior type="Ruanmou.Framework.AOP.SmsBehavior, Ruanmou.Framework"/>
<interceptionBehavior type="Ruanmou.Framework.AOP.ExceptionLoggingBehavior, Ruanmou.Framework"/>
<interceptionBehavior type="Ruanmou.Framework.AOP.CachingBehavior, Ruanmou.Framework"/>
<interceptionBehavior type="Ruanmou.Framework.AOP.LogBeforeBehavior, Ruanmou.Framework"/>
<interceptionBehavior type="Ruanmou.Framework.AOP.ParameterCheckBehavior, Ruanmou.Framework"/>
<interceptionBehavior type="Ruanmou.Framework.AOP.LogAfterBehavior, Ruanmou.Framework"/>
</register>
<register type="Ruanmou.Interface.IPhone,Ruanmou.Interface" mapTo="Ruanmou.Service.AndroidPhone, Ruanmou.Service.Extend" name="Android"/>
<register type="Ruanmou.Interface.IMicrophone, Ruanmou.Interface" mapTo="Ruanmou.Service.Microphone, Ruanmou.Service.Extend"/>
</register>
</container>
C# Unity容器的使用的更多相关文章
- 【中英对照】【EntLib6】【Unity】实验1:使用一个Unity容器
Lab 1: Using a Unity Container 实验1:使用一个Unity容器 Estimated time to complete this lab: 15 minutes 估计完成时 ...
- c#中的Unity容器
DIP是依赖倒置原则:一种软件架构设计的原则(抽象概念).依赖于抽象不依赖于细节 IOC即为控制反转(Inversion of Control):传统开发,上端依赖(调用/指定)下端对象,会有依赖,把 ...
- Unity容器中AOP应用示例程序
转发请注明出处:https://www.cnblogs.com/zhiyong-ITNote/p/9127001.html 实在没有找到Unity容器的AOP应用程序示例的说明,在微软官网找到了教程( ...
- Unity容器的简单AOP与DI的应用Demo(基于asp.net mvc框架)
转发请注明出处:https://home.cnblogs.com/u/zhiyong-ITNote/ 整个Demo是基于Controller-Service-Repository架构设计的,每一层之间 ...
- Unity容器在asp.net mvc中的IOC应用及AOP应用
<asp.net-mvc框架揭秘>一书中,有个示例,是使用unity容器来注入自定义的控制器工厂.代码示例可以自己去下载源码,在这里我就不说了.IOC容器的本质是解耦的实例化接口类,而如何 ...
- 【转载】C#中可使用Unity容器实现属性注入
简介 Unity :Unity是微软团队开发的一个轻量级,可扩展的依赖注入容器,为松散耦合应用程序提供了很好的解决方案,支持构造器注入,属性注入,方法注入. 控制反转:(Inversion of Co ...
- Unity容器实现自动注册
如何创建Unity容器? 首先NuGet搜索Unity, 该示例中使用的版本为4.0.1 新建控制台程序 示例中使用常规操作, 创建一个IPay接口, 分别有两个实现类: ApplePay.Huawe ...
- 2、手写Unity容器--第一层依赖注入
这个场景跟<手写Unity容器--极致简陋版Unity容器>不同,这里构造AndroidPhone的时候,AndroidPhone依赖于1个IPad 1.IPhone接口 namespac ...
- 1、手写Unity容器--极致简陋版Unity容器
模拟Unity容器实例化AndroidPhone 思路: 1.注册类型:把类型完整名称作为key添加到数据字典中,类型添加到数据字典的value中 2.获取实例:根据完整类型名称也就是key取出val ...
- Unity容器实现AOP面向切面编程
为什么要有AOP 需求总是变化的,比如经常会对一些方法后期增加日志.异常处理.权限.缓存.事务的处理,遇到这种情况我们往往只能修改类. 为了应对变化,我们常常使用设计模式解决,但是也有其局限性:设计模 ...
随机推荐
- 12. Mysql基础入门
课程大纲 • 数据库概述 • MySQL基本操作 • MySQL索引基础 • MySQL高级特性
- spring-1-spring介绍和IOC容器开发
一.介绍 1.版本 2.下载(jar包依赖) 下载 所以搜索:https://repo.spring.io/release/org/springframework/spring/ 文件分配 maven ...
- Dijkstra和堆优化
Dijkstra算法 由于我之前一直记的迪杰斯特拉的翻译导致我把dijkstra写成了dijstra--所以下文#define dijstra dijkstra 我以后叫她迪杰克斯歘! Dijskra ...
- jsp-->js-->jsp之间的关系
jsp和js通过form.submit();发送request请求createdIdSave.jsp 在CreatedIdSave.jsp中进行BL的增删改查操作,在jsp中将值保存到页面的scrip ...
- HashSet 的实现原理
HashSet 概述 对于 HashSet 而言,它是基于 HashMap 实现的,底层采用 HashMap 来保存元素,所以如果对 HashMap 比较熟悉了,那么学习 HashSet 也是很轻松的 ...
- Python 爬取页面内容
import urllib.request import requests from bs4 import BeautifulSoup url = "http://www.stats.gov ...
- Liferay Portal CE 反序列化命令执行漏洞(CVE-2020-7961)
影响范围 Liferay Portal 6.1.X Liferay Portal 6.2.X Liferay Portal 7.0.X Liferay Portal 7.1.X Liferay Por ...
- Blazor 路由及导航开发指南
翻译自 Waqas Anwar 2021年4月2日的文章 <A Developer's Guide To Blazor Routing and Navigation> [1] 检查传入的请 ...
- DataGrid列显示隐藏配置
1.列右键事件 private void data1_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { ContextMenu ...
- Java面向对象10——方法重写
方法重写 static : package oop.demon01.demon05; public class Application { public static void ...