ioc容器的实现方法有哪些_ASP.NET Core中IOC容器的实现原理

09f9917849ec1f8e897b0b4621175d84.gif

本章将和大家分享ASP.NET Core中IOC容器的实现原理。

首先我们需要了解什么是IOC,为什么要使用IOC容器?

一、依赖

类A用到了类B,我们就说类A依赖类B。

e9b791a6c835faeb257bc45bd642239e.png

using System;namespace MyIOCDI
{public class Test
{public void Show()
{
MyDependency myDependency = new MyDependency(); //全是细节 myDependency.Show();
Console.WriteLine($"This is {this.GetType().FullName}");
}
}public class MyDependency
{public void Show()
{
Console.WriteLine($"This is {this.GetType().FullName}");
}
}
}

e9b791a6c835faeb257bc45bd642239e.png

上面的示例中,类Test就依赖了MyDependency类。

二、依赖倒置原则(Dependence Inversion Principle)

依赖倒置原则:高层模块不应该依赖于低层模块,二者都应该依赖于抽象。应该依赖于抽象,而不是依赖细节。

什么是高层模块?这里的使用者Test类就称为高层模块。什么是低层模块?被使用者MyDependency类就称为低层模块。上面的示例中我们的高层模块就依赖于我们的低层模块。

那么这样子有什么不好呢?

  1、面向对象语言开发,就是类与类之间进行交互,如果高层直接依赖低层的细节,细节是多变的,那么低层的变化就导致上层的变化;

  2、如果层数多了,低层的修改会直接水波效应传递到最上层,一点细微的改动都会导致整个系统从下往上的修改。

因此,上例按照依赖倒置原则修改如下:

e9b791a6c835faeb257bc45bd642239e.png

using System;namespace MyIOCDI
{public class Test
{public void Show()
{
IDepenency myDependency = new MyDependency(); //左边抽象右边细节 myDependency.Show();
Console.WriteLine($"This is {this.GetType().FullName}");
}
}public class MyDependency : IDepenency
{public void Show()
{
Console.WriteLine($"This is {this.GetType().FullName}");
}
}public interface IDepenency
{void Show();
}
}

e9b791a6c835faeb257bc45bd642239e.png

三、IOC控制反转

控制反转是一种思想,所谓“控制反转”就是反转获得依赖对象的过程。

上面示例经过改造后虽然遵循了“依赖倒置原则”,但是违背了“开放封闭原则”,因为如果有一天想要修改变量myDependency为YourDependency类的实例,则需要修改Test类。

因此,我们需要反转这种创建对象的过程:

e9b791a6c835faeb257bc45bd642239e.png

using System;namespace MyIOCDI
{public class Test
{private readonly IDepenency _myDependency;public Test(IDepenency myDependency)
{this._myDependency = myDependency;
}public void Show()
{
_myDependency.Show();
Console.WriteLine($"This is {this.GetType().FullName}");
}
}public class MyDependency : IDepenency
{public void Show()
{
Console.WriteLine($"This is {this.GetType().FullName}");
}
}public interface IDepenency
{void Show();
}
}

e9b791a6c835faeb257bc45bd642239e.png

上例中,将 _myDependency 的创建过程“反转”给了调用者。

四、依赖注入(Dependency Injection)

依赖注入是一种在类及其依赖对象之间实现控制反转(IOC)思想的技术。

所谓依赖注入,就是由IOC容器在运行期间,动态地将某种依赖关系注入到对象之中。

依赖注入就是能做到构造某个对象时,将依赖的对象自动初始化并注入 。

IOC是目标是效果,需要DI依赖注入的手段。

三种注入方式:构造函数注入--属性注入--方法注入(按时间顺序)。

构造函数注入用的最多,默认找参数最多的构造函数,可以不用特性,可以去掉对容器的依赖。

五、IOC容器的实现原理

IOC容器的实现原理:

  1、启动时保存注册信息。

  2、在构造某个对象时,根据注册信息使用反射加特性,将依赖的对象自动初始化并注入。

  3、对对象进行生命周期管理或者进行AOP扩展等。

下面我们重点来看下如何创建一个简易的IOC容器(当然,实际使用的IOC容器要比这复杂的多)。

首先来看下项目的目录结构:

6b83bfae57f8eb0f305f678416471bf3.png

此处IOC容器中用到的自定义特性如下所示:

e9b791a6c835faeb257bc45bd642239e.png

using System;namespace TianYaSharpCore.IOCDI.CustomAttribute
{/// /// 构造函数注入特性/// [AttributeUsage(AttributeTargets.Constructor)]public class ConstructorInjectionAttribute : Attribute
{
}
}

e9b791a6c835faeb257bc45bd642239e.png

e9b791a6c835faeb257bc45bd642239e.png

using System;namespace TianYaSharpCore.IOCDI.CustomAttribute
{/// /// 方法注入特性/// [AttributeUsage(AttributeTargets.Method)]public class MethodInjectionAttribute : Attribute
{
}
}

e9b791a6c835faeb257bc45bd642239e.png

e9b791a6c835faeb257bc45bd642239e.png

using System;namespace TianYaSharpCore.IOCDI.CustomAttribute
{/// /// 常量/// [AttributeUsage(AttributeTargets.Parameter)]public class ParameterConstantAttribute : Attribute
{
}
}

e9b791a6c835faeb257bc45bd642239e.png

e9b791a6c835faeb257bc45bd642239e.png

using System;namespace TianYaSharpCore.IOCDI.CustomAttribute
{/// /// 简称(别名)///
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property)]public class ParameterShortNameAttribute : Attribute
{public string ShortName { get; private set; }public ParameterShortNameAttribute(string shortName)
{this.ShortName = shortName;
}
}
}

e9b791a6c835faeb257bc45bd642239e.png

e9b791a6c835faeb257bc45bd642239e.png

using System;namespace TianYaSharpCore.IOCDI.CustomAttribute
{/// /// 属性注入特性/// [AttributeUsage(AttributeTargets.Property)]public class PropertyInjectionAttribute : Attribute
{
}
}

e9b791a6c835faeb257bc45bd642239e.png

创建一个简易的IOC容器,如下所示:

e9b791a6c835faeb257bc45bd642239e.png

using System;namespace TianYaSharpCore.IOCDI.CustomContainer
{public class IOCContainerRegistModel
{public Type TargetType { get; set; }/// /// 生命周期/// public LifetimeType Lifetime { get; set; }/// /// 仅限单例/// public object SingletonInstance { get; set; }
}/// /// 生命周期/// public enum LifetimeType
{
Transient, //瞬时 Singleton,
Scope, //作用域
PerThread //线程单例//外部可释放单例 }
}

e9b791a6c835faeb257bc45bd642239e.png

e9b791a6c835faeb257bc45bd642239e.png

using System;namespace TianYaSharpCore.IOCDI.CustomContainer
{/// /// IOC容器接口/// public interface ITianYaIOCContainer
{void Register(string shortName = null, object[] paraList = null, LifetimeType lifetimeType = LifetimeType.Transient)where TTo : TFrom;
TFrom Resolve(string shortName = null);
ITianYaIOCContainer CreateChildContainer();
}
}

e9b791a6c835faeb257bc45bd642239e.png

e9b791a6c835faeb257bc45bd642239e.png

using System;using System.Collections.Generic;using System.Linq;using System.Reflection;using TianYaSharpCore.IOCDI.CustomAttribute;namespace TianYaSharpCore.IOCDI.CustomContainer
{/// /// IOC容器/// public class TianYaIOCContainer : ITianYaIOCContainer
{#region 字段或者属性/// /// 保存注册信息/// private Dictionary<string, IOCContainerRegistModel> _tianYaContainerDictionary = new Dictionary<string, IOCContainerRegistModel>();/// /// 保存常量的值/// private Dictionary<string, object[]> _tianYaContainerValueDictionary = new Dictionary<string, object[]>();/// /// 作用域单例的对象/// private Dictionary<string, object> _tianYaContainerScopeDictionary = new Dictionary<string, object>();#endregion 字段或者属性#region 构造函数/// /// 无参构造行数/// public TianYaIOCContainer()
{
}/// /// 主要在创建子容器的时候使用/// private TianYaIOCContainer(Dictionary<string, IOCContainerRegistModel> tianYaContainerDictionary,
Dictionary<string, object[]> tianYaContainerValueDictionary, Dictionary<string, object> tianYaContainerScopeDictionary)
{this._tianYaContainerDictionary = tianYaContainerDictionary;this._tianYaContainerValueDictionary = tianYaContainerValueDictionary;this._tianYaContainerScopeDictionary = tianYaContainerScopeDictionary;
}#endregion 构造函数/// /// 创建子容器/// public ITianYaIOCContainer CreateChildContainer()
{return new TianYaIOCContainer(this._tianYaContainerDictionary, this._tianYaContainerValueDictionary,new Dictionary<string, object>()); //没有注册关系,最好能初始化进去 }/// /// 获取键/// private string GetKey(string fullName, string shortName) => $"{fullName}___{shortName}";/// /// 加个参数区分生命周期--而且注册关系得保存生命周期/// /// 要添加的服务的类型/// 要使用的实现的类型/// 简称(别名)(主要用于解决单接口多实现)/// 常量参数/// 生命周期public void Register(string shortName = null, object[] paraList = null, LifetimeType lifetimeType = LifetimeType.Transient)where TTo : TFrom
{this._tianYaContainerDictionary.Add(this.GetKey(typeof(TFrom).FullName, shortName), new IOCContainerRegistModel()
{
Lifetime = lifetimeType,
TargetType = typeof(TTo)
});if (paraList != null && paraList.Length > 0)
{this._tianYaContainerValueDictionary.Add(this.GetKey(typeof(TFrom).FullName, shortName), paraList);
}
}/// /// 获取对象/// public TFrom Resolve(string shortName = null)
{return (TFrom)this.ResolveObject(typeof(TFrom), shortName);
}/// /// 递归--可以完成不限层级的对象创建/// private object ResolveObject(Type abstractType, string shortName = null)
{string key = this.GetKey(abstractType.FullName, shortName);var model = this._tianYaContainerDictionary[key];#region 生命周期switch (model.Lifetime)
{case LifetimeType.Transient:
Console.WriteLine("Transient Do Nothing Before");break;case LifetimeType.Singleton:if (model.SingletonInstance == null)
{break;
}else
{return model.SingletonInstance;
}case LifetimeType.Scope:if (this._tianYaContainerScopeDictionary.ContainsKey(key))
{return this._tianYaContainerScopeDictionary[key];
}else
{break;
}default:break;
}#endregion 生命周期
Type type = model.TargetType;#region 选择合适的构造函数
ConstructorInfo ctor = null;//标记特性
ctor = type.GetConstructors().FirstOrDefault(c => c.IsDefined(typeof(ConstructorInjectionAttribute), true));if (ctor == null)
{//参数个数最多
ctor = type.GetConstructors().OrderByDescending(c => c.GetParameters().Length).First();
}//ctor = type.GetConstructors()[0]; //直接第一个#endregion 选择合适的构造函数#region 准备构造函数的参数
List<object> paraList = new List<object>();object[] paraConstant = this._tianYaContainerValueDictionary.ContainsKey(key) ? this._tianYaContainerValueDictionary[key] : null; //常量找出来int iIndex = 0;foreach (var para in ctor.GetParameters())
{if (para.IsDefined(typeof(ParameterConstantAttribute), true))
{
paraList.Add(paraConstant[iIndex]);
iIndex++;
}else
{
Type paraType = para.ParameterType; //获取参数的类型string paraShortName = this.GetShortName(para);object paraInstance = this.ResolveObject(paraType, paraShortName);
paraList.Add(paraInstance);
}
}#endregion 准备构造函数的参数object oInstance = null;
oInstance = Activator.CreateInstance(type, paraList.ToArray()); //创建对象,完成构造函数的注入#region 属性注入foreach (var prop in type.GetProperties().Where(p => p.IsDefined(typeof(PropertyInjectionAttribute), true)))
{
Type propType = prop.PropertyType;string paraShortName = this.GetShortName(prop);object propInstance = this.ResolveObject(propType, paraShortName);
prop.SetValue(oInstance, propInstance);
}#endregion 属性注入#region 方法注入foreach (var method in type.GetMethods().Where(m => m.IsDefined(typeof(MethodInjectionAttribute), true)))
{
List<object> paraInjectionList = new List<object>();foreach (var para in method.GetParameters())
{
Type paraType = para.ParameterType;//获取参数的类型 IUserDALstring paraShortName = this.GetShortName(para);object paraInstance = this.ResolveObject(paraType, paraShortName);
paraInjectionList.Add(paraInstance);
}
method.Invoke(oInstance, paraInjectionList.ToArray());
}#endregion 方法注入#region 生命周期switch (model.Lifetime)
{case LifetimeType.Transient:
Console.WriteLine("Transient Do Nothing After");break;case LifetimeType.Singleton:
model.SingletonInstance = oInstance;break;case LifetimeType.Scope:this._tianYaContainerScopeDictionary[key] = oInstance;break;default:break;
}#endregion 生命周期//return oInstance.AOP(abstractType); //AOP扩展return oInstance;
}/// /// 获取简称(别名)/// private string GetShortName(ICustomAttributeProvider provider)
{if (provider.IsDefined(typeof(ParameterShortNameAttribute), true))
{var attribute = (ParameterShortNameAttribute)(provider.GetCustomAttributes(typeof(ParameterShortNameAttribute), true)[0]);return attribute.ShortName;
}else
{return null;
}
}
}
}

e9b791a6c835faeb257bc45bd642239e.png

至此,我们就创建完了一个简易的IOC容器。

下面我们来添加一些用于测试的接口,如下所示:

e9b791a6c835faeb257bc45bd642239e.png

using System;namespace MyIOCDI.IService
{public interface ITestServiceA
{void Show();
}
}

e9b791a6c835faeb257bc45bd642239e.png

e9b791a6c835faeb257bc45bd642239e.png

using System;namespace MyIOCDI.IService
{public interface ITestServiceB
{void Show();
}
}

e9b791a6c835faeb257bc45bd642239e.png

e9b791a6c835faeb257bc45bd642239e.png

using System;namespace MyIOCDI.IService
{public interface ITestServiceC
{void Show();
}
}

e9b791a6c835faeb257bc45bd642239e.png

e9b791a6c835faeb257bc45bd642239e.png

using System;namespace MyIOCDI.IService
{public interface ITestServiceD
{void Show();
}
}

e9b791a6c835faeb257bc45bd642239e.png

接口对应的实现,如下所示:

e9b791a6c835faeb257bc45bd642239e.png

using System;using MyIOCDI.IService;namespace MyIOCDI.Service
{public class TestServiceA : ITestServiceA
{public TestServiceA()
{
Console.WriteLine($"{this.GetType().Name}被构造。。。");
}public void Show()
{
Console.WriteLine($"This is {this.GetType().Name} Show");
}
}
}

e9b791a6c835faeb257bc45bd642239e.png

e9b791a6c835faeb257bc45bd642239e.png

using System;using MyIOCDI.IService;namespace MyIOCDI.Service
{public class TestServiceB : ITestServiceB
{public TestServiceB()
{
Console.WriteLine($"{this.GetType().Name}被构造。。。");
}public void Show()
{
Console.WriteLine($"This is {this.GetType().Name} Show");
}
}
}

e9b791a6c835faeb257bc45bd642239e.png

e9b791a6c835faeb257bc45bd642239e.png

using System;using MyIOCDI.IService;namespace MyIOCDI.Service
{public class TestServiceC : ITestServiceC
{public TestServiceC()
{
Console.WriteLine($"{this.GetType().Name}被构造。。。");
}public void Show()
{
Console.WriteLine($"This is {this.GetType().Name} Show");
}
}
}

e9b791a6c835faeb257bc45bd642239e.png

e9b791a6c835faeb257bc45bd642239e.png

using System;using MyIOCDI.IService;using TianYaSharpCore.IOCDI.CustomAttribute;namespace MyIOCDI.Service
{public class TestServiceD : ITestServiceD
{/// /// 属性注入/// [PropertyInjection]public ITestServiceA TestServiceA { get; set; }/// /// 带有别名的属性注入///
[ParameterShortName("ServiceB")]
[PropertyInjection]public ITestServiceB TestServiceB { get; set; }public TestServiceD()
{
Console.WriteLine($"{this.GetType().Name}被构造。。。");
}#region 构造函数注入private readonly ITestServiceA _testServiceA;private readonly ITestServiceB _testServiceB;
[ConstructorInjection] //优先选择带有构造函数注入特性的public TestServiceD(ITestServiceA testServiceA, [ParameterConstant] string sValue, ITestServiceB testServiceB, [ParameterConstant] int iValue)
{
Console.WriteLine($"{this.GetType().Name}--{sValue}--{iValue}被构造。。。");
_testServiceA = testServiceA;
_testServiceB = testServiceB;
}#endregion 构造函数注入#region 方法注入private ITestServiceC _testServiceC;
[MethodInjection]public void Init(ITestServiceC testServiceC)
{
_testServiceC = testServiceC;
}#endregion 方法注入public void Show()
{
Console.WriteLine($"This is {this.GetType().Name} Show");
}
}
}

e9b791a6c835faeb257bc45bd642239e.png

最后来看下IOC容器的使用及其运行结果:

e9b791a6c835faeb257bc45bd642239e.png

using System;using TianYaSharpCore.IOCDI.CustomContainer;using MyIOCDI.IService;using MyIOCDI.Service;namespace MyIOCDI
{class Program
{static void Main(string[] args)
{
ITianYaIOCContainer container = new TianYaIOCContainer();
{//注册
container.Register(); //将ITestServiceA注册到TestServiceA
container.Register();
container.Register(shortName: "ServiceB");
container.Register();
container.Register(paraList: new object[] { "浪子天涯", 666 }, lifetimeType: LifetimeType.Singleton);
ITestServiceD d1 = container.Resolve(); //创建对象交给IOC容器
ITestServiceD d2 = container.Resolve();
d1.Show();
Console.WriteLine($"object.ReferenceEquals(d1, d2) = {object.ReferenceEquals(d1, d2)}");
}
Console.ReadKey();
}
}
}

e9b791a6c835faeb257bc45bd642239e.png

运行结果如下:

34d804852dae094c4e375e017f2bd7a2.png

生命周期为作用域的,其实就是子容器单例,如下所示:

e9b791a6c835faeb257bc45bd642239e.png

using System;using TianYaSharpCore.IOCDI.CustomContainer;using MyIOCDI.IService;using MyIOCDI.Service;namespace MyIOCDI
{class Program
{static void Main(string[] args)
{
ITianYaIOCContainer container = new TianYaIOCContainer();//{// //注册// container.Register(); //将ITestServiceA注册到TestServiceA// container.Register();// container.Register(shortName: "ServiceB");// container.Register();// container.Register(paraList: new object[] { "浪子天涯", 666 }, lifetimeType: LifetimeType.Singleton);// ITestServiceD d1 = container.Resolve(); //创建对象交给IOC容器// ITestServiceD d2 = container.Resolve();// d1.Show();// Console.WriteLine($"object.ReferenceEquals(d1, d2) = {object.ReferenceEquals(d1, d2)}");//}
{//生命周期:作用域//就是Http请求时,一个请求处理过程中,创建都是同一个实例;不同的请求处理过程中,就是不同的实例;//得区分请求,Http请求---Asp.NetCore内置Kestrel,初始化一个容器实例;然后每次来一个Http请求,就clone一个,//或者叫创建子容器(包含注册关系),然后一个请求就一个子容器实例,那么就可以做到请求单例了(其实就是子容器单例)//主要可以去做DbContext Repository
container.Register(lifetimeType: LifetimeType.Scope);
ITestServiceA a1 = container.Resolve();
ITestServiceA a2 = container.Resolve();
Console.WriteLine(object.ReferenceEquals(a1, a2)); //T
ITianYaIOCContainer container1 = container.CreateChildContainer();
ITestServiceA a11 = container1.Resolve();
ITestServiceA a12 = container1.Resolve();
ITianYaIOCContainer container2 = container.CreateChildContainer();
ITestServiceA a21 = container2.Resolve();
ITestServiceA a22 = container2.Resolve();
Console.WriteLine(object.ReferenceEquals(a11, a12)); //T
Console.WriteLine(object.ReferenceEquals(a21, a22)); //T
Console.WriteLine(object.ReferenceEquals(a11, a21)); //F
Console.WriteLine(object.ReferenceEquals(a11, a22)); //F
Console.WriteLine(object.ReferenceEquals(a12, a21)); //F
Console.WriteLine(object.ReferenceEquals(a12, a22)); //F }
Console.ReadKey();
}
}
}

e9b791a6c835faeb257bc45bd642239e.png

运行结果如下:

bbc952d97bdb57844ff5a7b4801aa8d1.png

至此本文就全部介绍完了,如果觉得对您有所启发请记得点个赞哦!!!

Demo源码:

链接:https://pan.baidu.com/s/15xpmWbEDbkm7evpr4iIZNg
提取码:ckes

fcf54c0b327116c1f791d10e2d30e6ca.png

往期 精彩 回顾

【推荐】.NET Core开发实战视频课程 ★★★

Redis基本使用及百亿数据量中的使用技巧分享(附视频地址及观看指南)

.NET Core中的一个接口多种实现的依赖注入与动态选择看这篇就够了

10个小技巧助您写出高性能的ASP.NET Core代码

现身说法:实际业务出发分析百亿数据量下的多表查询优化

关于C#异步编程你应该了解的几点建议

C#异步编程看这篇就够了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值