Unity Ioc框架简单例子
IOC:英文全称:Inversion of Control,中文名称:控制反转,它还有个名字叫依赖注入(Dependency Injection)。
作用:将各层的对象以松耦合的方式组织在一起,解耦,各层对象的调用完全面向接口。当系统重构的时候,代码的改写量将大大减少。
理解依赖注入:
引入Unity:http://unity.codeplex.com/
或者VS GuGet中引用


Dependency 属性注入需要填特性
InjectionMethod 方法注入需要添加特性
public interface IA { }
public interface IB { }
public interface IC { }
public interface ID { }
public class A : IA
{
public IB B { get; set; }
[Dependency]
public IC C { get; set; }
public ID D { get; set; }
public A(IB b)
{
this.B = b;
}
[InjectionMethod]
public void Initialize(ID d)
{
this.D = d;
}
}
public class B : IB { }
public class C : IC { }
public class D : ID { }
代码方式注册:
static void Main(string[] args)
{
// http://unity.codeplex.com/ IUnityContainer container = new UnityContainer(); //注册映射
container.RegisterType<IA, A>();
container.RegisterType<IB, B>();
container.RegisterType<IC, C>();
container.RegisterType<ID, D>(); //得到A的实例
A a = (A)container.Resolve<IA>(); Console.WriteLine("a.B == null ? {0}", a.B == null ? "Yes" : "No");
Console.WriteLine("a.C == null ? {0}", a.C == null ? "Yes" : "No");
Console.WriteLine("a.D == null ? {0}", a.D == null ? "Yes" : "No"); Console.Read();
}
配置文件方式注册:
<configuration>
<configSections>
<section name="unity"
type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity>
<containers>
<container>
<register type="Client.IA, Client" mapTo="Client.A, Client"/>
<register type="Client.IB, Client" mapTo="Client.B, Client"/>
<register type="Client.IC, Client" mapTo="Client.C, Client"/>
<register type="Client.ID, Client" mapTo="Client.D, Client"/>
</container>
</containers>
</unity> </configuration>
static void Main(string[] args)
{
// http://unity.codeplex.com/
IUnityContainer container = new UnityContainer();
UnityConfigurationSection configuration = (UnityConfigurationSection)ConfigurationManager.GetSection(UnityConfigurationSection.SectionName);
configuration.Configure(container);
A a = (A)container.Resolve<IA>(); Console.WriteLine("a.B == null ? {0}", a.B == null ? "Yes" : "No");
Console.WriteLine("a.C == null ? {0}", a.C == null ? "Yes" : "No");
Console.WriteLine("a.D == null ? {0}", a.D == null ? "Yes" : "No"); Console.Read();
}
Unity Ioc框架简单例子的更多相关文章
- 今天研究Unity Ioc 框架
今天研究Unity Ioc 框架,被自己坑了两个多小时. 运行就报错,反反复复检查了很多次,配置文件,代码都没有问题,也从新写了好几遍. 最后仔细看报错消息才知道,config文件没有生成到目录……… ...
- AgileEAS.NET SOA 中间件平台.Net Socket通信框架-简单例子-实现简单的服务端客户端消息应答
一.AgileEAS.NET SOA中间件Socket/Tcp框架介绍 在文章AgileEAS.NET SOA 中间件平台Socket/Tcp通信框架介绍一文之中我们对AgileEAS.NET SOA ...
- MVC4 基于 Unity Ioc 框架的 ControllerFactory
首先引入Untiy框架. 可以在NuGet程序包 管理器中直接安装. 新建 继承DefaultControllerFactory 的UnityControllerFactory: 重写虚方法GetC ...
- .NET Unity IOC框架使用实例
1.IOC简介 IOC(Inversion of Control), 控制反转 DI (Dependency Injection),依赖注入 IOC的基本概念是:不创建对象,但是描述创建它们的方式.在 ...
- .NET领域最为流行的IOC框架之一Autofac WebAPI2使用Autofac实现IOC属性注入完美解决方案 AutoFac容器初步
.NET领域最为流行的IOC框架之一Autofac 一.前言 Autofac是.NET领域最为流行的IOC框架之一,微软的Orchad开源程序使用的就是Autofac,Nopcommerce开源程 ...
- 使用Microsoft的IoC框架:Unity来对.NET应用进行解耦
1.IoC/DI简介 IoC 即 Inversion of Control,DI 即 Dependency Injection,前一个中文含义为控制反转,后一个译为依赖注入,可以理解成一种编程模式,详 ...
- 【.NET6+WPF】WPF使用prism框架+Unity IOC容器实现MVVM双向绑定和依赖注入
前言:在C/S架构上,WPF无疑已经是"桌面一霸"了.在.NET生态环境中,很多小伙伴还在使用Winform开发C/S架构的桌面应用.但是WPF也有很多年的历史了,并且基于MVVM ...
- Spring框架系列(2) - Spring简单例子引入Spring要点
上文中我们简单介绍了Spring和Spring Framework的组件,那么这些Spring Framework组件是如何配合工作的呢?本文主要承接上文,向你展示Spring Framework组件 ...
- 1.Spring IoC简单例子
Spring IoC简单例子 1.IHelloMessage.java package com.tony.spring.chapter01; public interface IHelloMessag ...
随机推荐
- java中split的用法即回顾
package com.b; public class Cor { public static void main(String[] args) { String a = "this is ...
- 第15届浙江省赛 E LIS
LIS Time Limit: 1 Second Memory Limit: 65536 KB Special Judge DreamGrid is learning the LI ...
- 转: SQL中的where条件,在数据库中提取与应用浅析
SQL中的where条件,在数据库中提取与应用浅析 http://hedengcheng.com/?p=577 1问题描述 一条SQL,在数据库中是如何执行的呢?相信很多人都会对这个问题比较感兴趣.当 ...
- linux lcd设备驱动剖析二
上一节中,分析了s3c2410fb,c的入口出口函数,以及一些重要结构体的分析,初步知道了这是一个平台驱动的架构. 上一节文章链接:http://blog.csdn.net/lwj103862095/ ...
- IO模型比较分析
异步IO(Asynchronous I/O) Linux下的asynchronous IO其实用得不多,从内核2.6版本才开始引入.先看一下它的流程: 用户进程发起read操作之后,立刻就可以开始去做 ...
- Informatica PowerCenter下载地址
https://edelivery.oracle.com/EPD/Download/get_form?egroup_aru_number=12854075
- windows 进程监控 Procmon.exe
windows 进程监控 Procmon.exe window下一个程序打开太慢,可以用此程序监控.在哪一步慢了,读取文件还是注册表. ProcessMonitor3.2 Process Monito ...
- printf("%f\n", 3);输出结果为什么是0.000000(转载)
printf不会关心你输入的参数的类型,你输入的实际是 printf("%f",3),但是这个整型3不会被隐式类型转换为浮点型,而是被直接按内存内容当作浮点型 也就是说,内部使用等 ...
- Money Systems 货币系统(母函数)
Description 母牛们不但创建了他们自己的政府而且选择了建立了自己的货币系统. [In their own rebellious way],,他们对货币的数值感到好奇. 传统地,一个货币系统是 ...
- Spring Cloud Eureka 2 (Eureka Server搭建服务注册中心)
工具:IntelliJ IDEA 2017.1.2 x64.maven3.3.9 打开IDE file===>new===>project next next 选择相应的依赖 next ...