Unity(IOC)学习笔记
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37591671/article/details/79432564
Unity(IOC)学习笔记
1.IOC
在介绍如何在程序中使用Unity之前,首先说一下什么是IOC:
IOC是Inversion of Control的缩写,被翻译为控制反转,是一种全新的设计模式,用来削减计算机程序的耦合问题,把程序上层对下层的依赖,转移到第三方的容器来装配。 控制反转一般分为两种类型,依赖注入(Dependency Injection,简称DI)和依赖查找(Dependency Lookup)。依赖注入应用比较广泛。
实现Ioc的框架有很多,比如astle Windsor、Unity、Spring.NET、StructureMap,我们这里学习使用微软提供的Unity。
2.Unity
下面介绍如何在程序中使用Unity:
第一步:
首先在Nuget添加Unity包,引用→右键→管理Nuget程序包,要实现AOP必须也添加下面的Unity.Interception包
这里添加Unity版本为5.5.6,Unity.Abstractions版本3.1.3,Unity.Interception版本5.3.1
第二步:
程序中添加一个配置文件的文件夹ConfigFiles,添加配置文件:
第三步:
配置文件:文件结构为
具体配置为:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>
</configSections>
<unity>
<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Unity.Interception.Configuration"/>
<containers>
<container name="testContainer">
<register type="MyInterface.IPhone,MyInterface" mapTo="MyServices.ApplePhone, MyServices"/>
<register type="MyInterface.IPhone,MyInterface" mapTo="MyServices.AndroidPhone, MyServices" name="Android"/>
<register type="MyInterface.IMicrophone, MyInterface" mapTo="MyServices.Microphone, MyServices"/>
<register type="MyInterface.IHeadphone, MyInterface" mapTo="MyServices.Headphone, MyServices"/>
<register type="MyInterface.IPower, MyInterface" mapTo="MyServices.Power, MyServices"/>
</container>
<container name="testContainerAOP">
<extension type="Interception"/>
<register type="MyInterface.IPhone,MyInterface" mapTo="MyServices.ApplePhone, MyServices">
<interceptor type="InterfaceInterceptor"/>
<interceptionBehavior type="MyFramework.LogBehavior, MyFramework"/>
<lifetime type="transient" />
<!--<constructor>
<param name="pubContext" type="IPubContext" />
<param name="id" type="System.Int32" value="3" />
</constructor>-->
</register>
<register type="MyInterface.IMicrophone, MyInterface" mapTo="MyServices.Microphone, MyServices"/>
<register type="MyInterface.IHeadphone, MyInterface" mapTo="MyServices.Headphone, MyServices"/>
<register type="MyInterface.IPower, MyInterface" mapTo="MyServices.Power, MyServices"/>
<register type="MyInterface.IPhone,MyInterface" mapTo="MyServices.AndroidPhone, MyServices" name="android"/>
<register type="MyInterface.IPhone,MyInterface" mapTo="MyServices.ApplePhone, MyServices" name="apple"/>
</container>
</containers>
</unity>
</configuration>
这里注册了两个Unity容器“testContainer”,“testContainerAOP”,后者是带有AOP的。
解释一下这个register:
<register type="MyInterface.IPhone,MyInterface" mapTo="MyServices.ApplePhone, MyServices"/>
MyInterface.IPhone:表示抽象接口的IPhone抽象类。
MyInterface为抽象类命名空间,即dll名称
MyServices.ApplePhone:表示实现的具体的ApplePhone类。
MyServices:为具体实现类命名空间,即dll名称
第四步:
Unity在程序中使用方式,这里是用配置文件:
1.首先要添加System.Configuration的引用(引用右键→添加引用→框架):
2.找配置文件的路径
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "CfgFiles\\Unity.Config");//找配置文件的路径
这里要注意吧Unity.config文件修改为始终复制:
3. 声明容器IUnityContainer
IUnityContainer container = new UnityContainer();
section.Configure(container, "testContainer");
4.Resolve解析类型对象
IPhone phone = container.Resolve<IPhone>();
phone.Call();
IPhone android = container.Resolve<IPhone>("Android");
android.Call();
这里要说一下通过命名注册来区分生成的不同对象实例,即 上面的"Android",在配置文件中是这样注册的:
<register type="MyInterface.IPhone,MyInterface" mapTo="MyServices.AndroidPhone, MyServices" name="Android"/>
5.将MyServices编译生成的dll拷贝到项目文件的Bin/debug文件夹。
6.在项目中添加对接口的引用:
具体代码:
static void Main(string[] args)
{
{
//通过配置文件
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "ConfigFiles\\Unity.Config");//找配置文件的路径
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
UnityConfigurationSection section = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);
IUnityContainer container = new UnityContainer();
section.Configure(container, "testContainer");
container.AddNewExtension<Interception>().Configure<Interception>()
.SetInterceptorFor<IPhone>(new InterfaceInterceptor());
IPhone phone = container.Resolve<IPhone>();
phone.Call();
IPhone android = container.Resolve<IPhone>("Android");
android.Call();
}
}
3.Unity中实现AOP
1.首先添加一个LogBehavior 的类,用来实现记录日志功能。
先要添加在Myframwork类库添加Unity,版本一样,以免冲突。然后继承IInterceptionBehavior接口。
public class LogBehavior: IInterceptionBehavior
{
public IEnumerable<Type> GetRequiredInterfaces()
{
return Type.EmptyTypes;
}
public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
{
Console.WriteLine("LogBehavior before");
IMethodReturn method = getNext()(input, getNext);
Console.WriteLine("LogBehavior after");
return method;
}
public bool WillExecute
{
get { return true; }
}
}
2.在配置文件中配置:
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>
</configSections>
<unity>
<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Unity.Interception.Configuration"/>
<containers>
<container name="testContainer">
<register type="MyInterface.IPhone,MyInterface" mapTo="MyServices.ApplePhone, MyServices"/>
<register type="MyInterface.IPhone,MyInterface" mapTo="MyServices.AndroidPhone, MyServices" name="Android"/>
<register type="MyInterface.IMicrophone, MyInterface" mapTo="MyServices.Microphone, MyServices"/>
<register type="MyInterface.IHeadphone, MyInterface" mapTo="MyServices.Headphone, MyServices"/>
<register type="MyInterface.IPower, MyInterface" mapTo="MyServices.Power, MyServices"/>
</container>
<container name="testContainerAOP">
<extension type="Interception"/>
<register type="MyInterface.IPhone,MyInterface" mapTo="MyServices.ApplePhone, MyServices">
<interceptor type="InterfaceInterceptor"/>
<interceptionBehavior type="Myframework.LogBehavior, Myframework"/>
<lifetime type="transient" />
</register>
<register type="MyInterface.IMicrophone, MyInterface" mapTo="MyServices.Microphone, MyServices"/>
<register type="MyInterface.IHeadphone, MyInterface" mapTo="MyServices.Headphone, MyServices"/>
<register type="MyInterface.IPower, MyInterface" mapTo="MyServices.Power, MyServices"/>
<register type="MyInterface.IPhone,MyInterface" mapTo="MyServices.AndroidPhone, MyServices" name="android"/>
<register type="MyInterface.IPhone,MyInterface" mapTo="MyServices.ApplePhone, MyServices" name="apple"/>
</container>
</containers>
</unity>
</configuration>
3.将Myframwork编译生成的dll拷贝到项目文件的Bin/debug文件夹。
4.在程序中使用:
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "ConfigFiles\\Unity.Config");//找配置文件的路径
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
UnityConfigurationSection section = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);
IUnityContainer container = new UnityContainer();
section.Configure(container, "testContainerAOP");
IPhone phone = container.Resolve<IPhone>();
phone.Call();
5.运行效果:
Unity(IOC)学习笔记的更多相关文章
- Unity Shader学习笔记-1
本篇文章是对Unity Shader入门精要的学习笔记,插图大部分来自冯乐乐女神的github 如果有什么说的不正确的请批评指正 目录 渲染流水线 流程图 Shader作用 屏幕映射 三角形遍历 两大 ...
- 从0开始学习Unity的学习笔记(I 界面学习和简单模型拼装)
先给一个大致今天学习的图,然后后面是细节 1.下载Unity :官网下载需要版本 2.Unity安装:一定不要有中文路径:一台电脑可以安装不同版本的Unity,但是要安装在不同的文件夹下: 3. 新建 ...
- 【Unity Shader学习笔记】Unity基础纹理-单张纹理
1 单张纹理 1.1 纹理 使用纹理映射(Texture Mapping)技术,我们把一张图片逐纹素(Texel)地控制模型的颜色. 美术人员建模时,会在建模软件中利用纹理展开技术把纹理映射坐标(Te ...
- Unity sqlite学习笔记一
1.SQLITE的常识 SQLite是一个开源免费的数据库,一般用于嵌入系统或者小规模的应用软件开发中,你可以像使用Access一样使用它. sqlite的主要优点:零配置(Zero Configur ...
- unity 3D 学习笔记
1.父对象的初始位置设,即刚开始的空对象的根节点位置应当设置成(0,0,0) 这样设置可以避免以后出现奇怪的坐标. GameObject实际上就是一些组件的容器. unity 使用公用变量原因是,在U ...
- Unity Shader学习笔记 - 用UV动画实现沙滩上的泡沫
这个泡沫效果来自远古时代的Unity官方海岛Demo, 原效果直接复制3个材质球在js脚本中做UV动画偏移,这里尝试在shader中做动画并且一个pass中完成: // Upgrade NOTE: r ...
- Unity的学习笔记(XLua的初学用法并在lua中使用unity周期函数)
自己最近也在研究怎么用lua控制UI,然后看着网上介绍,决定选用XLua,毕竟TX爸爸出的,有人维护,自己琢磨着怎么用,于是弄出来一个能用的作为记录. 当然,XLua主要是用于热更新,我自己是拿来尝试 ...
- 【Unity Shader学习笔记】Unity基础纹理-渐变纹理
纹理可以用来存储任何表面属性. 可以通过使用渐变纹理来实现插画风格的渲染效果. 这项技术是由Valve公司提出的.Valve使用它来渲染游戏中具有插画风格的角色. 我们使用半兰伯特模型计算漫反射. 因 ...
- 【Unity Shader学习笔记】Unity基础纹理-法线贴图
1 高度纹理 使用一张纹理改变物体表面法线,为模型提供更多细节. 有两种主要方法: 1.高度映射:使用一张高度纹理(height map)来模拟表面位移(displacement).得到一个修改后的法 ...
- 【Unity Shader学习笔记】Unity光照基础-高光反射
1.原理 1.1.Phong模型 计算高光反射需要表面法线.视角方向.光源方向.反射方向等. 在这四个矢量中,我们实际上只需要知道其中3个矢量即可,而第4个矢量(反射方向r)可以通过其他信息计算得到: ...
随机推荐
- PS:切图
1.从psd中获取资源 2.基本了解 3.简单的图片操作和调整 4.对自己的审美提高一.界面设置: 1.新建设置:ctr+n; 预设:Web,大小Web(1920*1080) 背景:透明 2.移动工具 ...
- Logstash之Logstash inputs(file和redis插件)、Logstash outputs(elasticsearch 和redis插件)和Filter plugins
前期博客 Logstash安装和设置(图文详解)(多节点的ELK集群安装在一个节点就好) Filebeat啊,根据input来监控数据,根据output来使用数据!!! 请移步, Filebeat之 ...
- 洛谷P3403跳楼机(最短路构造/同余最短路)
题目-> 解题思路: 最短路构造很神啊. 先用前两个值跑在第三个值模意义下的同余最短路(这步贪心可以证明,如果第三步长为z,那么如果n+z可以达到,n+2z同样可以达到) 最后计算与楼顶差多少个 ...
- Django快速搭建博客
准备工作: 1.Python 2.Django 3.Git 安装Python: 官网下载 安装Django: #安装最新版本的Django $ pip install django #或者指定安装版本 ...
- C#调用C++数组,结构体DLL
1.基本数据类型的传递 常见数据类型的传递 C/C++ C# 长度 short short 2Bytes int int 4Bytes long(该类型在传递的时候常常会弄混) int 4Bytes ...
- 【例题 7-14 UVA-1602】Lattice Animals
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 借鉴网上的题解的. 思路是. 用"标准化"的思想. 确定基准点(0,0) 然后假设(0,0)是第一个连通块. 然 ...
- 如何在同一台机器上安装多个MySQL的实例(转)
最近由于工作的需要,需要在同一台机器上搭建两个MySQL的实例,(注:已经存在了一个3306的MySQL的实例). 先说下,什么是mysql的多实例,简单的来说就是一台机器上安装了多个mysql的服务 ...
- ASP.net 环境搭建
https://www.cnblogs.com/leizhanjun/p/6081928.html
- RocketMQ(八):消息发送
匠心零度 转载请注明原创出处,谢谢! RocketMQ网络部署图 NameServer:在系统中是做命名服务,更新和发现 broker服务. Broker-Master:broker 消息主机服务器. ...
- 26.SpringBoot事务注解详解
转自:https://www.cnblogs.com/kesimin/p/9546225.html @Transactional spring 事务注解 1.简单开启事务管理 @EnableTrans ...