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)可以通过其他信息计算得到: ...
随机推荐
- 【DRF频率】
目录 使用自带的频率限制类 使用自定义的频率限制类 开发平台的API接口调用需要限制其频率,以节约服务器资源和避免恶意的频繁调用. DRF就为我们提供了一些频率限制的方法. DRF中的版本.认证.权限 ...
- linux系统常用日志
系统日志记录着系统运行中的记录信息,在服务或者系统发生故障的时候,通过查询系统日志,可以帮助我们诊断.系统日志可以预警安全问题,系统日志一般都存放在/var/log目录下 /var/log/dmesg ...
- absolute、relative,toggle()
測试代码例如以下: <div> <div class="global">不应用样式</div> <div class="glob ...
- [NOI.AC#30]candy 贪心
链接 一个直观的想法是,枚举最小的是谁,然后二分找到另外一个序列对应位置更新答案,复杂度 \(O(NlogN)\) 实际上不需要二分,因为每次当最大的变大之后,原来不行的最小值现在也一定不行,指针移动 ...
- php课程 18-60 cookie和session的最主要区别是什么
php课程 18-60 cookie和session的最主要区别是什么 一.总结 一句话总结:存储位置不同:cookie存储在客户端:session存储在服务端. 1.cookie和session在p ...
- ::的类名前有个 & ,什么意思?
转载自 http://www.imooc.com/qadetail/93985 MazePerson &MazePerson::setPersonPosition(int coordinat ...
- HDU 4010 Query on The Trees (动态树)(Link-Cut-Tree)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4010 题意; 先给你一棵树,有 \(4\) 种操作: 1.如果 \(x\) 和 \(y\) 不在同一 ...
- Day2二分图笔记
定义 左边一堆点 右边一堆点 树是一个二分图,奇数深度和偶数深度可以组成二分图, 二分图匹配 左边的点和右边的点有边 匈牙利算法 可能的答案 ans,n-ans,m-ans,n+m-ans || ...
- 对ng-repeat的表格内容添加不同样式:ng-style
对ng-repeat的表格内容添加不同样式,html代码: <tr ng-repeat="x in tableData"> <td>{{x.networkN ...
- 全新linux中通过编译方式安装nginx
先去官网下载linux.tar.gz包 http://nginx.org/en/download.html 传到linxu中 解压tar包 在软件包nginx-1.15.9目录下对NGINX进行配 ...