本章将和你们分享ASP.NET Core中IOC容器的实现原理。html
首先咱们须要了解什么是IOC,为何要使用IOC容器?ide
类A用到了类B,咱们就说类A依赖类B。函数
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}"); } }}
上面的示例中,类Test就依赖了MyDependency类。测试
依赖倒置原则:高层模块不该该依赖于低层模块,两者都应该依赖于抽象。应该依赖于抽象,而不是依赖细节。大数据
什么是高层模块?这里的使用者Test类就称为高层模块。什么是低层模块?被使用者MyDependency类就称为低层模块。上面的示例中咱们的高层模块就依赖于咱们的低层模块。网站
那么这样子有什么很差呢?this
一、面向对象语言开发,就是类与类之间进行交互,若是高层直接依赖低层的细节,细节是多变的,那么低层的变化就致使上层的变化;spa
二、若是层数多了,低层的修改会直接水波效应传递到最上层,一点细微的改动都会致使整个系统从下往上的修改。线程
所以,上例按照依赖倒置原则修改以下:htm
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(); }}
控制反转是一种思想,所谓“控制反转”就是反转得到依赖对象的过程。
上面示例通过改造后虽然遵循了“依赖倒置原则”,可是违背了“开放封闭原则”,由于若是有一天想要修改myDependency为YourDependency类的实例,则须要修改Test类。
所以,咱们须要反转这种建立对象的过程:
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(); }}
上例中,将 _myDependency 的建立过程“反转”给了调用者。
所谓依赖注入,就是由IOC容器在运行期间,动态地将某种依赖关系注入到对象之中。
依赖注入就是能作到构造某个对象时,将依赖的对象自动初始化并注入 。
IOC是目标是效果,须要DI依赖注入的手段。
三种注入方式:构造函数注入--属性注入--方法注入(按时间顺序)。
构造函数注入用的最多,默认找参数最多的构造函数,能够不用特性,能够去掉对容器的依赖。
IOC容器的实现原理:
一、启动时保存注册信息。
二、在构造某个对象时,使用反射加特性,将依赖的对象自动初始化并注入。
三、对对象进行生命周期管理或者进行AOP扩展等。
下面咱们重点来看下模拟实现IOC容器的代码:
首先来看下项目的目录结构:
此处IOC容器中用到的自定义特性以下所示:
using System;namespace TianYaSharpCore.IOCDI.CustomAttribute{ /// <summary> /// 构造函数注入特性 /// </summary> [AttributeUsage(AttributeTargets.Constructor)] public class ConstructorInjectionAttribute : Attribute { }}
using System;namespace TianYaSharpCore.IOCDI.CustomAttribute{ /// <summary> /// 方法注入特性 /// </summary> [AttributeUsage(AttributeTargets.Method)] public class MethodInjectionAttribute : Attribute { }}
using System;namespace TianYaSharpCore.IOCDI.CustomAttribute{ /// <summary> /// 常量 /// </summary> [AttributeUsage(AttributeTargets.Parameter)] public class ParameterConstantAttribute : Attribute { }}
using System;namespace TianYaSharpCore.IOCDI.CustomAttribute{ /// <summary> /// 简称(别名) /// </summary> [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property)] public class ParameterShortNameAttribute : Attribute { public string ShortName { get; private set; } public ParameterShortNameAttribute(string shortName) { this.ShortName = shortName; } }}
using System;namespace TianYaSharpCore.IOCDI.CustomAttribute{ /// <summary> /// 属性注入特性 /// </summary> [AttributeUsage(AttributeTargets.Property)] public class PropertyInjectionAttribute : Attribute { }}
IOC容器实现以下所示:
using System;namespace TianYaSharpCore.IOCDI.CustomContainer{ public class IOCContainerRegistModel { public Type TargetType { get; set; } /// <summary> /// 生命周期 /// </summary> public LifetimeType Lifetime { get; set; } /// <summary> /// 仅限单例 /// </summary> public object SingletonInstance { get; set; } } /// <summary> /// 生命周期 /// </summary> public enum LifetimeType { Transient, //瞬时 Singleton, Scope, //做用域 PerThread //线程单例 //外部可释放单例 }}
using System;namespace TianYaSharpCore.IOCDI.CustomContainer{ /// <summary> /// IOC容器接口 /// </summary> public interface ITianYaIOCContainer { void Register<TFrom, TTo>(string shortName = null, object[] paraList = null, LifetimeType lifetimeType = LifetimeType.Transient) where TTo : TFrom; TFrom Resolve<TFrom>(string shortName = null); ITianYaIOCContainer CreateChildContainer(); }}
using System;using System.Collections.Generic;using System.Linq;using System.Reflection;using TianYaSharpCore.IOCDI.CustomAttribute;namespace TianYaSharpCore.IOCDI.CustomContainer{ /// <summary> /// IOC容器 /// </summary> public class TianYaIOCContainer : ITianYaIOCContainer { #region 字段或者属性 /// <summary> /// 保存注册信息 /// </summary> private Dictionary<string, IOCContainerRegistModel> _tianYaContainerDictionary = new Dictionary<string, IOCContainerRegistModel>(); /// <summary> /// 保存常量的值 /// </summary> private Dictionary<string, object[]> _tianYaContainerValueDictionary = new Dictionary<string, object[]>(); /// <summary> /// 做用域单例的对象 /// </summary> private Dictionary<string, object> _tianYaContainerScopeDictionary = new Dictionary<string, object>(); #endregion 字段或者属性 #region 构造函数 /// <summary> /// 无参构造行数 /// </summary> public TianYaIOCContainer() { } /// <summary> /// 主要在建立子容器的时候使用 /// </summary> private TianYaIOCContainer(Dictionary<string, IOCContainerRegistModel> tianYaContainerDictionary, Dictionary<string, object[]> tianYaContainerValueDictionary, Dictionary<string, object> tianYaContainerScopeDictionary) { this._tianYaContainerDictionary = tianYaContainerDictionary; this._tianYaContainerValueDictionary = tianYaContainerValueDictionary; this._tianYaContainerScopeDictionary = tianYaContainerScopeDictionary; } #endregion 构造函数 /// <summary> /// 建立子容器 /// </summary> public ITianYaIOCContainer CreateChildContainer() { return new TianYaIOCContainer(this._tianYaContainerDictionary, this._tianYaContainerValueDictionary, new Dictionary<string, object>()); //没有注册关系,最好能初始化进去 } /// <summary> /// 获取键 /// </summary> private string GetKey(string fullName, string shortName) => $"{fullName}___{shortName}"; /// <summary> /// 加个参数区分生命周期--并且注册关系得保存生命周期 /// </summary> /// <typeparam name="TFrom">要添加的服务的类型</typeparam> /// <typeparam name="TTo">要使用的实现的类型</typeparam> /// <param name="shortName">简称(别名)(主要用于解决单接口多实现)</param> /// <param name="paraList">常量参数</param> /// <param name="lifetimeType">生命周期</param> public void Register<TFrom, TTo>(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); } } /// <summary> /// 获取对象 /// </summary> public TFrom Resolve<TFrom>(string shortName = null) { return (TFrom)this.ResolveObject(typeof(TFrom), shortName); } /// <summary> /// 递归--能够完成不限层级的对象建立 /// </summary> 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;//获取参数的类型 IUserDAL string 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; } /// <summary> /// 获取简称(别名) /// </summary> 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; } } }}
测试用接口以下所示:
using System;namespace MyIOCDI.IService{ public interface ITestServiceA { void Show(); }}
using System;namespace MyIOCDI.IService{ public interface ITestServiceB { void Show(); }}
using System;namespace MyIOCDI.IService{ public interface ITestServiceC { void Show(); }}
using System;namespace MyIOCDI.IService{ public interface ITestServiceD { void Show(); }}
测试用接口对应的实现以下所示:
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"); } }}
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"); } }}
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"); } }}
using System;using MyIOCDI.IService;using TianYaSharpCore.IOCDI.CustomAttribute;namespace MyIOCDI.Service{ public class TestServiceD : ITestServiceD { /// <summary> /// 属性注入 /// </summary> [PropertyInjection] public ITestServiceA TestServiceA { get; set; } /// <summary> /// 带有别名的属性注入 /// </summary> [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"); } }}
使用IOC容器以下所示:
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>(); //将ITestServiceA注册到TestServiceA container.Register<ITestServiceB, TestServiceB>(); container.Register<ITestServiceB, TestServiceB>(shortName: "ServiceB"); container.Register<ITestServiceC, TestServiceC>(); container.Register<ITestServiceD, TestServiceD>(paraList: new object[] { "浪子天涯", 666 }, lifetimeType: LifetimeType.Singleton); ITestServiceD d1 = container.Resolve<ITestServiceD>(); //建立对象交给IOC容器 ITestServiceD d2 = container.Resolve<ITestServiceD>(); d1.Show(); Console.WriteLine($"object.ReferenceEquals(d1, d2) = {object.ReferenceEquals(d1, d2)}"); } Console.ReadKey(); } }}
运行结果以下:
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>(); //将ITestServiceA注册到TestServiceA // container.Register<ITestServiceB, TestServiceB>(); // container.Register<ITestServiceB, TestServiceB>(shortName: "ServiceB"); // container.Register<ITestServiceC, TestServiceC>(); // container.Register<ITestServiceD, TestServiceD>(paraList: new object[] { "浪子天涯", 666 }, lifetimeType: LifetimeType.Singleton); // ITestServiceD d1 = container.Resolve<ITestServiceD>(); //建立对象交给IOC容器 // ITestServiceD d2 = container.Resolve<ITestServiceD>(); // d1.Show(); // Console.WriteLine($"object.ReferenceEquals(d1, d2) = {object.ReferenceEquals(d1, d2)}"); //} { //生命周期:做用域 //就是Http请求时,一个请求处理过程当中,建立都是同一个实例;不一样的请求处理过程当中,就是不一样的实例; //得区分请求,Http请求---Asp.NetCore内置Kestrel,初始化一个容器实例;而后每次来一个Http请求,就clone一个, //或者叫建立子容器(包含注册关系),而后一个请求就一个子容器实例,那么就能够作到请求单例了(其实就是子容器单例) //主要能够去作DbContext Repository container.Register<ITestServiceA, TestServiceA>(lifetimeType: LifetimeType.Scope); ITestServiceA a1 = container.Resolve<ITestServiceA>(); ITestServiceA a2 = container.Resolve<ITestServiceA>(); Console.WriteLine(object.ReferenceEquals(a1, a2)); //T ITianYaIOCContainer container1 = container.CreateChildContainer(); ITestServiceA a11 = container1.Resolve<ITestServiceA>(); ITestServiceA a12 = container1.Resolve<ITestServiceA>(); ITianYaIOCContainer container2 = container.CreateChildContainer(); ITestServiceA a21 = container2.Resolve<ITestServiceA>(); ITestServiceA a22 = container2.Resolve<ITestServiceA>(); 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(); } }}
运行结果以下:
至此本文就介绍完了,若是以为对您有所启发请记得点个赞哦!!!
Demo源码:
连接:https://pan.baidu.com/s/15xpmWbEDbkm7evpr4iIZNg 提取码:ckes
此文由博主精心撰写转载请保留此原文连接:https://www.cnblogs.com/xyh9039/p/13663808.html
版权声明:若有雷同纯属巧合,若有侵权请及时联系本人修改,谢谢!!!
ASP.NET Core中IOC容器的实现原理