C# 一个初学者对 依赖注入 IOC 的理解( 含 Unity 的使用)
通过 人打电话 来谈谈自己对IOC的理解
版本1.0
public class Person
{
public AndroidPhone Phone { get; set; }
public void CallForSomebody()
{
Phone.Call();
}
}
public class AndroidPhone
{
public void Call()
{
Console.WriteLine($"{this.GetType().Name} is calling");
}
}
class Program
{
static void Main(string[] args)
{
Person person = new Person
{
Phone = new AndroidPhone()
};
person.CallForSomebody();
Console.ReadKey();
}
}
如果这时候,想给这个人一部苹果手机,那么必须要修改 Person 类, 将属性Phone的类型改成:ApplePhone.这显然违背了OOP思想的"对修改封闭,对扩展开放"原则.同时该代码也不符合"接口分离原则",于是乎做如下调整.
版本2.0
public class Person
{
//定义 Phone 为 接口 IPhone 类型
public IPhone Phone { get; set; }
public void CallForSomebody()
{
Phone.Call();
}
}
public interface IPhone
{
void Call();
}
public class ApplePhone : IPhone
{
public void Call()
{
Console.WriteLine($"{this.GetType().Name} is calling");
}
}
public class AndroidPhone : IPhone
{
public void Call()
{
Console.WriteLine($"{this.GetType().Name} is calling");
}
}
控制台程序:
Person person = new Person();
//给这个人一部安卓手机
person.Phone = new AndroidPhone();
person.CallForSomebody();
//给这个人一部苹果手机
person.Phone = new ApplePhone();
person.CallForSomebody();
如果需要给诺基亚手机,只需要在创建一个诺基亚Phone类,实现 IPhone 接口,而不需要修改底层代码.
依赖注入有三种方式:
public class Person
{
//属性注入
public IPhone Phone { get; set; }
//构造函数注入
public Person(IPhone phone)
{
this.Phone = phone;
}
//方法注入
public void MethodInject(IPhone phone)
{
this.Phone = phone;
}
public void CallForSomebody()
{
Phone.Call();
}
}
版本2.0虽然不需要修改底层代码,但是控制台的代码依然需要修改,我们需要将 new AndridPhone() 更改成 new ApplePhone()
版本3.0 ( 利用 Unity )
学习新东西都是从 hello world 开始,先来一段 Unity 的 hello world
//创建一个IOC容器
IUnityContainer container = new UnityContainer();
//想要安卓手机就注册安卓手机
//container.RegisterType<IPhone, AndroidPhone>();
//想要苹果手机就注册苹果手机
container.RegisterType<IPhone, ApplePhone>();
IPhone phone = container.Resolve<IPhone>();
Person person = new Person(phone);
person.CallForSomebody();
这段代码其实 跟 版本2.0没什么区别,甚至还复杂了,版本2.0是改 new AndroidPhone() , 这里是改 container.RegisterType<IPhone, AndroidPhone>()
版本4.0
新建一个配置文件:

<?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">
<containers>
<container name="unityContainer">
<!--type,mapTo:逗号左边是类的完全限定名,即命名空间+类名;逗号右边是程序集名称-->
<register type="IOCUnity.IPhone,IOCUnity" mapTo="IOCUnity.AndroidPhone,IOCUnity"></register>
</container>
</containers>
</unity>
</configuration>
控制台代码如下:
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap
{
ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + @"UnityXml\UnityConfig.xml")
};
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap,ConfigurationUserLevel.None);
UnityConfigurationSection section = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);
IUnityContainer container = new UnityContainer();
section.Configure(container, "unityContainer");
IPhone phone = container.Resolve<IPhone>();
Person person = new Person(phone);
person.CallForSomebody();
如果要换成 ApplePhone , 只需要将 配置文件中 mapTo="IOCUnity.AndroidPhone,IOCUnity" 更改成 mapTo="IOCUnity.ApplePhone,IOCUnity"> 即可,源代码不需要做任何修改
版本4.5
配置文件修改: 添加 name 标记
<register type="IOCUnity.IPhone,IOCUnity" mapTo="IOCUnity.ApplePhone,IOCUnity" name="Apple"></register>
<register type="IOCUnity.IPhone,IOCUnity" mapTo="IOCUnity.AndroidPhone,IOCUnity" name="Android"></register>
//创建一个安卓手机
//IPhone phone = container.Resolve<IPhone>("Android");
//创建一个苹果手机
IPhone phone = container.Resolve<IPhone>("Android");
版本5.0
控制台代码:
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap
{
ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + @"UnityXml\UnityConfig.xml")
};
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
UnityConfigurationSection section = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);
IUnityContainer container = new UnityContainer();
section.Configure(container, "unityContainer");
Person person = container.Resolve<Person>();
/*
上面创建了一个person,虽然我们没有给它的属性Phone赋值,但实际上 Unity 已经帮我们注入了.
Unity 有三种特性 分别对应三种注入方式:
[InjectionConstructor] 构造函数注入
[dependency] 属性注入
[InjectionMethod] 方法注入
如果一个特性都不打,那么Unity 默认会采用 构造函数注入,并且是选用入参最多的那个构造函数注入.因此,推荐采用 构造函数注入,因为它是无侵入式的,不需要打任何特性.
*/
person.CallForSomebody();
public class Person
{
//属性注入
[Dependency]
public IPhone Phone { get; set; }
//构造函数注入
[InjectionConstructor]
public Person(IPhone phone)
{
this.Phone = phone;
}
//方法注入
[InjectionMethod]
public void MethodInject(IPhone phone)
{
this.Phone = phone;
}
public void CallForSomebody()
{
Phone.Call();
}
}
<?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">
<containers>
<container name="unityContainer">
<!--type,mapTo:逗号左边是类的完全限定名,即命名空间+类名;逗号右边是程序集名称-->
<register type="IOCUnity.IPhone,IOCUnity" mapTo="IOCUnity.ApplePhone,IOCUnity"></register>
<register type="IOCUnity.Person,IOCUnity" mapTo="IOCUnity.Person,IOCUnity"></register>
</container>
</containers>
</unity>
</configuration>
C# 一个初学者对 依赖注入 IOC 的理解( 含 Unity 的使用)的更多相关文章
- 控制反转(Inversion of Control,英文缩写为IoC),另外一个名字叫做依赖注入(Dependency Injection,简称DI)
控制反转(Inversion of Control,英文缩写为IoC),另外一个名字叫做依赖注入(Dependency Injection,简称DI),是一个重要的面向对象编程的法则来削减计算机程序的 ...
- android使用篇(四) 注解依赖注入IOC实现绑定控件
在android使用篇(三) MVC模式中提到一个问题: 1) 视图层(View):一般採用XML文件进行界面的描写叙述,使用的时候能够很方便的引入,可是用xml编写了,又须要在Acitvity声明而 ...
- 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之九 || 依赖注入IoC学习 + AOP界面编程初探
更新 1.如果看不懂本文,或者比较困难,先别着急问问题,我单写了一个关于依赖注入的小Demo,可以下载看看,多思考思考注入的原理: https://github.com/anjoy8/BlogArti ...
- Z从壹开始前后端分离【 .NET Core2.2/3.0 +Vue2.0 】框架之九 || 依赖注入IoC学习 + AOP界面编程初探
本文梯子 本文3.0版本文章 更新 代码已上传Github+Gitee,文末有地址 零.今天完成的绿色部分 一.依赖注入的理解和思考 二.常见的IoC框架有哪些 1.Autofac+原生 2.三种注入 ...
- TypeC一个微软开发的超简单.NET依赖注入/IoC容器
控制反转(IoC,Inversion of Control)是由Martin Fowler总结出来的一种设计模式,用来减少代码间的耦合.一般而言,控制反转分为依赖注入(Dependency Injec ...
- 关于依赖注入IOC/DI的感想
之前一直不明白依赖注入有什么好处,甚至觉得它是鸡肋,现在想想,当时真是可笑. 这个想法正如同说接口是没有用处一样. 当整个项目非常庞大,各个方法之间的调用非常复杂,那么,可以想象一下,假设说没有任何的 ...
- DI依赖注入/IOC控制反转
DI依赖注入# 啥都不说,直接上代码 <?php class UserController { private $user; function __construct(UserModel $us ...
- 大话DI依赖注入+IOC控制反转(二) 之 浅析.Net Core中的DI与IOC
转发时请注明原创作者及地址,否则追究责任.原创:alunchen 在上一篇文章中,我们聊了很多关于定义的方面,比较孤燥,下面我们结合.Net Core聊一下依赖注入&控制反转. 三种对象生 ...
- 大话DI依赖注入+IOC控制反转(一) 之 定义
转发时请注明原创作者及地址,否则追究责任.原创:alunchen 依赖注入与控制反转 依赖注入与控制反转是老生常谈的问题.一般面试也会面试到这种问题.网上很多很多这方面的资料,搜索出来一大堆 ...
随机推荐
- 清除input[type=number]的默认样式
input[type=number] { -moz-appearance:textfield; } input[type=number]::-webkit-inner-spin-button, inp ...
- .net framework 4.5 +steeltoe+ springcloud 实现服务注册功能
首先得先了解并熟悉一下springcloud,并手动去搭建一个服务中心,具体可度娘教程. 如果是.net core的话,实现注册也是没有问题的,网上教程很多,可自行度娘. 最难的就是基于Framewo ...
- java--Object类接受任意引用数据类型对象
java学习进展到类,首先就对万类之父Object类进行举例练习,这里我是对一维数组和接口用Object接受数组和接口. package test1; public class enum1 { pub ...
- ASP.NET没有魔法——ASP.NET MVC界面美化及使用Bundle完成静态资源管理
对于一个应用来说界面的重要性无言而喻,而Web应用的界面是使用Html+Css以及Javascript实现的,ASP.NET MVC是一个用来构建Web应用的框架,它的界面也是Html实现的,对于一些 ...
- 审核Memcrashed Drdos攻击代码
0x00前言: 距离世界上最大的Drdos攻击已经过去了两个星期左右 昨天在交流的时候.群友在Github中找到了exploit. 0x01开始: #-- coding: utf8 -- #!/usr ...
- 使用MyBatis 框架犯的错误
最近做项目,数据层使用的是MyBatis框架,在使用过程中,犯了一些错误: resultMap和resultType书写错误导致问题 resultMap和resultType二者用法不一样: resu ...
- ServiceFabric极简文档-1.0 Service Fabric 自定义集群部署
Service Fabric 部署集群:https://docs.microsoft.com/zh-cn/azure/service-fabric/service-fabric-get-started ...
- 使用git指令下载github仓库代码(笔记)
通过Git指令下载源码 Git概念说明 三种状态:修改状态.暂存状态和Git仓库 基本的Git工作流程: 在工作目录中修改文件 暂存文件,将文件的快照放入暂存区域 提交更新,找到暂 ...
- C语言第九次博客作业--指针
一.PTA实验作业 题目1:两个4位正整数的后两位互换 1. 本题PTA提交列表 2. 设计思路 定义循环变量i,两个数组a[4],b[4] for i=0 to 3 a[i]*p取各个位 *p/=1 ...
- 格式化JSON数据
function formatJson(json, options) { var reg = null, formatted = '', pad = 0, PADDING = ' '; options ...