[IoC容器Unity]第一回:Unity预览
1.引言
高内聚,低耦合成为一个OO架构设计的一个参考标准。高内聚是一个模块或者一个类中成员跟这个模块或者类的关系尽量高,低耦合是不同模块或者不同类之间关系尽量简单。
拿咱国家举例来说,假如你是中国人。
高内聚:就是说你跟中国亲,关系好,国家会保护你。
低内聚:就是说你跟中国的关系不好,那还怎么混,改天就要批斗你,你就是个问题源。
低耦合:就是说你跟日本的关系非常正常,非常简单,这样的话,就不会被骂汉奸了。
高耦合:就是说你跟日本亲,活该被砸,被游街。
上面例子虚构的,不太贴切,但从中可以看出来高内聚和低耦合的处境是最好的。高内聚是对内部成员跟本身这个模块的关系的描述,低耦合是对成员跟外部模块之间关系的描述。对内对外也是一个相对范围,一个模块里面的小模块之间是耦合,对大模块是聚合,所以说耦合无处不在,我们都要低耦合,Untity就可以帮助我们。
2.Unity
Unity是一个IoC容器,用来实现依赖注入(Dependency Injection,DI),减少耦合的,Unity出自于伟大的微软。对依赖注入和IoC模式可以查看之前一篇文章IoC模式。
unity组件网址:http://unity.codeplex.com/
网址中有源码有文档有示例,大家可以下载。我是用的是2.1版本。
看看Unity能做些什么,列举部分如下:
1.Unity支持简单对象创建,特别是分层对象结构和依赖,以简化程序代码。其包含一个编译那些可能存在依赖于其他对象的对象实例机制。 ..... |
我们项目中什么时候要使用到Unity呢,如下情况:
1.所构建的系统依赖于健全的面向对象原则,但是大量不同的代码交织在一起而难以维护。 .............. |
先看看Unity容器IUnityContainer 接口的定义:
//Interface defining the behavior of the Unity dependency injection container.
public interface IUnityContainer : IDisposable
{
//The parent of this container.
IUnityContainer Parent { get; } //Get a sequence of Microsoft.Practices.Unity.ContainerRegistration that describe
//the current state of the container.
IEnumerable<ContainerRegistration> Registrations { get; } //Add an extension object to the container.
IUnityContainer AddExtension(UnityContainerExtension extension); //Run an existing object through the container and perform injection on it.
object BuildUp(Type t, object existing, string name, params ResolverOverride[] resolverOverrides); //Resolve access to a configuration interface exposed by an extension.
object Configure(Type configurationInterface); //Create a child container.
IUnityContainer CreateChildContainer(); //Register an instance with the container.
IUnityContainer RegisterInstance(Type t, string name, object instance, LifetimeManager lifetime); //Register a type mapping with the container, where the created instances will
//use the given Microsoft.Practices.Unity.LifetimeManager.
IUnityContainer RegisterType(Type from, Type to, string name, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers); //Remove all installed extensions from this container.
IUnityContainer RemoveAllExtensions(); //Resolve an instance of the requested type with the given name from the container.
object Resolve(Type t, string name, params ResolverOverride[] resolverOverrides); //Return instances of all registered types requested.
IEnumerable<object> ResolveAll(Type t, params ResolverOverride[] resolverOverrides); //Run an existing object through the container, and clean it up.
void Teardown(object o);
}
IUnityContainer 中有几个方法经常会使用到,如:RegisterInstance,RegisterType,Resolve等等。
Unity为了方便操作,专门为IUnityContainer 提供了许多的扩展方法,部分方法声明如:
/// <summary>
/// 扩展方法 v2.0.50727
/// </summary>
public static class UnityContainerExtensions
{
public static IUnityContainer AddNewExtension<TExtension>(this IUnityContainer container) where TExtension : UnityContainerExtension;
public static T BuildUp<T>(this IUnityContainer container, T existing, params ResolverOverride[] resolverOverrides);
public static T BuildUp<T>(this IUnityContainer container, T existing, string name, params ResolverOverride[] resolverOverrides);
public static object BuildUp(this IUnityContainer container, Type t, object existing, params ResolverOverride[] resolverOverrides);
public static TConfigurator Configure<TConfigurator>(this IUnityContainer container) where TConfigurator : IUnityContainerExtensionConfigurator;
public static bool IsRegistered<T>(this IUnityContainer container);
public static bool IsRegistered<T>(this IUnityContainer container, string nameToCheck);
public static bool IsRegistered(this IUnityContainer container, Type typeToCheck);
public static bool IsRegistered(this IUnityContainer container, Type typeToCheck, string nameToCheck);
public static IUnityContainer RegisterInstance<TInterface>(this IUnityContainer container, TInterface instance);
public static IUnityContainer RegisterInstance<TInterface>(this IUnityContainer container, string name, TInterface instance);
public static IUnityContainer RegisterInstance<TInterface>(this IUnityContainer container, TInterface instance, LifetimeManager lifetimeManager);
public static IUnityContainer RegisterInstance(this IUnityContainer container, Type t, object instance);
public static IUnityContainer RegisterInstance<TInterface>(this IUnityContainer container, string name, TInterface instance, LifetimeManager lifetimeManager);
public static IUnityContainer RegisterInstance(this IUnityContainer container, Type t, object instance, LifetimeManager lifetimeManager);
public static IUnityContainer RegisterInstance(this IUnityContainer container, Type t, string name, object instance);
public static IUnityContainer RegisterType<T>(this IUnityContainer container, params InjectionMember[] injectionMembers);
public static IUnityContainer RegisterType<TFrom, TTo>(this IUnityContainer container, params InjectionMember[] injectionMembers) where TTo : TFrom;
public static IUnityContainer RegisterType<T>(this IUnityContainer container, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers);
public static IUnityContainer RegisterType<TFrom, TTo>(this IUnityContainer container, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers) where TTo : TFrom;
public static IUnityContainer RegisterType<TFrom, TTo>(this IUnityContainer container, string name, params InjectionMember[] injectionMembers) where TTo : TFrom;
public static IUnityContainer RegisterType<T>(this IUnityContainer container, string name, params InjectionMember[] injectionMembers);
public static IUnityContainer RegisterType(this IUnityContainer container, Type t, params InjectionMember[] injectionMembers);
public static IUnityContainer RegisterType<T>(this IUnityContainer container, string name, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers);
public static IUnityContainer RegisterType<TFrom, TTo>(this IUnityContainer container, string name, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers) where TTo : TFrom;
public static IUnityContainer RegisterType(this IUnityContainer container, Type t, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers);
public static IUnityContainer RegisterType(this IUnityContainer container, Type t, string name, params InjectionMember[] injectionMembers);
public static IUnityContainer RegisterType(this IUnityContainer container, Type from, Type to, params InjectionMember[] injectionMembers);
public static IUnityContainer RegisterType(this IUnityContainer container, Type t, string name, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers);
public static IUnityContainer RegisterType(this IUnityContainer container, Type from, Type to, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers);
public static IUnityContainer RegisterType(this IUnityContainer container, Type from, Type to, string name, params InjectionMember[] injectionMembers);
public static T Resolve<T>(this IUnityContainer container, params ResolverOverride[] overrides);
public static T Resolve<T>(this IUnityContainer container, string name, params ResolverOverride[] overrides);
public static object Resolve(this IUnityContainer container, Type t, params ResolverOverride[] overrides);
public static IEnumerable<T> ResolveAll<T>(this IUnityContainer container, params ResolverOverride[] resolverOverrides); public static IUnityContainer LoadConfiguration(this IUnityContainer container);
public static IUnityContainer LoadConfiguration(this IUnityContainer container, string containerName);
public static IUnityContainer LoadConfiguration(this IUnityContainer container, UnityConfigurationSection section);
public static IUnityContainer LoadConfiguration(this IUnityContainer container, UnityConfigurationSection section, string containerName);
}
下面来个简单的例子,现在项目中添加Microsoft.Practices.Unity.dll和Microsoft.Practices.Unity.Configuration.dll的引用,准备几个类,如下:
/// <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);
}
}
用编程方式实现注入:
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();
}
}
配置文件方式:
<?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();
}
}
效果跟代码方式一样
Unity使用方式基本分为三步:
- 声明容器IUnityContainer
- Register注册类型
- Resolve解析类型对象
3.小结
看到IUnityContainer接口声明还是比较简单的,主要用到的方法也不多,另外提供了扩展方法,看起来代码挺多,都是一些方法重载,不要对Unity组建感到恐惧,我们只是简单的使用它,主要是使用IUnityContainer。
[IoC容器Unity]第一回:Unity预览的更多相关文章
- Power Pivot表属性无法切换回表预览模式的问题
近期Office365用户升级后解决了在Power Pivot中输入中文的问题,但是同时也带来了一个新的问题就是表属性窗口默认为“查询编辑器”模式,且无法切换回“表预览”模式. 本文和您分享在这种情况 ...
- 微软发布Azure Stack第一个技术预览版
为了提升商业灵敏度和加快创新步伐,各个企业都在迅速地转向云服务.在微软,我们已经见到微软智能云Azure的飞速发展和使用,每月我们都有近十万的新增订阅量.然而,我们也了解到还有很多企业在完全移到公有云 ...
- Unity 摄像头竖屏预览显示的问题
Unity可以通过WebCamTexture打开摄像头,通过 cameraRawImage.texture = camTexture; 将贴图给RawImage,但是WebCamTexture只能设置 ...
- [IoC容器Unity]第四回:使用范例
1.引言 前面几个章节介绍了Unity的基本使用,主要分为程序和配置文件两种方法的使用,可以参考一下链接, [IoC容器Unity]第一回:Unity预览 [IoC容器Unity]第二回:Lifeti ...
- 9种CSS3炫酷图片展开预览展示动画特效
详细内容请点击 在线预览立即下载 这是一组共9款CSS3炫酷图片预览展示动画特效插件.css的新特性可以让我们制作出各种炫酷的动画效果.该图片预览展示动画特效就是一个很好的例子,该效果开始时图片堆叠在 ...
- iOS开发之使用Storyboard预览UI在不同屏幕上的运行效果
在公司做项目一直使用Storyboard,虽然有时会遇到团队合作的Storyboard冲突问题,但是对于Storyboard开发效率之高还是比较划算的.在之前的博客中也提到过,团队合作使用Storyb ...
- Spring框架学习[IoC容器高级特性]
1.通过前面4篇文章对Spring IoC容器的源码分析,我们已经基本上了解了Spring IoC容器对Bean定义资源的定位.读入和解析过程,同时也清楚了当用户通过getBean方法向IoC容器获取 ...
- 新的理念、 新的解决方案、 新的Azure Stack技术预览
Jeffrey Snover 我们很高兴地宣布︰Azure Stack Technical Preview 2(TP2)已发布!我们朝着向您的数据中心提供Azure服务能力的目标又更近一步.自发布第一 ...
- Android 10开发者预览版功能介绍
Android P的开发者预览版最亮眼的功能莫过于支持“刘海屏”等屏幕显示.同样在适配可折叠设备方面,Android Q的第一个开发者预览版也很“接地气”,谷歌早在去年11月就发布了对可折叠设备的支持 ...
随机推荐
- C#博客目录
基础加强 1.索引器 2.密闭类.静态类及扩展方法 3.值.引用类型及结构体 4.秒懂IL.CTS.CLS和CLR 5.装箱与拆箱 6.引用相等与运算符重载 7.ref与out 8.委托和事件 9.对 ...
- 原生js标识当前导航位置(给当前导航一个className=active)
导航html结构为: <div class="header2-nav"> <a href="index.html">首页</a&g ...
- Windows环境下安装Oracle数据库
Windows环境 1.解压文件 1)Oracle下载官网地址: http://www.oracle.com/technetwork/cn/database/enterprise-edition/do ...
- 66.ajax--ajax请求多个url解决办法
ajax请求多个url解决办法 以下四种方法是我找的,我也进行实践过. 测试中有四个请求接口,原本需要13S,用了第三种方法缩减到7S,但是仍不能达到2S以内. 所以仅供参考,待我找到能缩减到2S以内 ...
- 正则表达式-----re库
1.正则表达式的概念 a.为什么要用正则? 用字符串匹配也是可以的: startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False.如果参数 be ...
- java的冒泡排序
public interface Sorter{ public <T extends Comparable<T>> void sort(T[] list); //定义两个待排序 ...
- ELK+Filebeat 集中式日志解决方案详解
链接:https://www.ibm.com/developerworks/cn/opensource/os-cn-elk-filebeat/index.html?ca=drs- ELK Stack ...
- SSIS Hekaton In-Memory OLTP 【翻译一篇外国文章】
来自:http://www.itprotoday.com/microsoft-sql-server/important-new-features-sql-server-2014 Microsoft's ...
- Nginx Install 记录
一.安装编译工具及库文件 yum -y install gcc yum -y install gcc-c++ yum -y install zlib; yum -y install pcre-deve ...
- git clone git@github.com:snuglove/ 报错
[root@qc_centos7_5 ~]# git clone git@github.com:snuglove/Job-hunting-related.git Cloning into 'Job-h ...