zl程序教程

您现在的位置是:首页 >  其他

当前栏目

IOC控制反转,DI依赖注入,自定义IOC及生命周期,反射

控制注入依赖反射 自定义 生命周期 反转 IOC
2023-09-11 14:13:57 时间

//1.依赖倒置原则
//2.IOC控制反转
//3.DI依赖注入
//4.Unity容器
//5.自定义IOC容器

IOC: 依赖抽象,不依赖细节,控制反转
IOC:工厂
DI:实现方式 依赖导致原则

 

1.创建容器,
2.指定注册关系:构造函数注入
3.生成对象
4.IOC生命周期管理:
默认是瞬时生命周期
单例模式:是同一个应用
线程单例:同一个线程是同一个实列

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ZXFramework.Common
{
public class ZXContainer : IZXContainer
{
private Dictionary<string, Type> ZXContainerDicationary = new Dictionary<string, Type>();
public void RegisterType<TFrom, TTo>() where TTo : TFrom
{
ZXContainerDicationary.Add(typeof(TFrom).FullName, typeof(TTo));
}

/// <summary>
/// T:是一个抽象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T Resolve<T>()
{
#region MyRegion
//string abstartName = typeof(T).FullName;
//Type type = ZXContainerDicationary[abstartName];
////怎么解决?
////1.先准备参数; 传递过去;
////2.确定执行哪个构造函数;---如果没有标记[InjectionConstructor];默认规则;选择参数最多的构造函数---如果标记[InjectionConstructor]特性---执行标记特性的这个构造函数
////3.确定要执行的构造函数参数的类型
////4.通过构造函数参数的类型得到具体的类型
////5.创建该类型的对象
////6.当做参数传递过去
//ConstructorInfo ctor = null;
////如果有ZXInjectionConstructor,就找出标记的有ZXInjectionConstructor特性的构造函数
//if (type.GetConstructors().Count(c => c.IsDefined(typeof(ZXInjectionConstructor), true)) > 0)
//{
// ctor = type.GetConstructors().FirstOrDefault(c => c.IsDefined(typeof(ZXInjectionConstructor), true));
//}
//else
//{
// ctor = type.GetConstructors().OrderByDescending(c => c.GetParameters().Length).First();
//}

//List<object> parameterlist = new List<object>();
//foreach (ParameterInfo parameter in ctor.GetParameters())
//{
// string parameterType = parameter.ParameterType.FullName;
// Type targetType = ZXContainerDicationary[parameterType];

// //1.先准备参数; 传递过去;
// //2.确定执行哪个构造函数;---如果没有标记[InjectionConstructor];默认规则;选择参数最多的构造函数---如果标记[InjectionConstructor]特性---执行标记特性的这个构造函数
// //3.确定要执行的构造函数参数的类型
// //4.通过构造函数参数的类型得到具体的类型
// //5.创建该类型的对象
// //6.当做参数传递过去
// object oParameter = Activator.CreateInstance(targetType);
// parameterlist.Add(oParameter);
//}
//object oInstance = Activator.CreateInstance(type, parameterlist.ToArray());//调用的五参数构造函数;//如果有两级依赖
//return (T)oInstance;
#endregion

string abstartName = typeof(T).FullName;
Type type = ZXContainerDicationary[abstartName];
return (T)this.ObjectInstance(type);
}

//不知道层级---且实现都是统一的=====递归的;

//递归一定是要有跳出条件的;

private object ObjectInstance(Type type)
{
ConstructorInfo ctor = null;
//如果有ZXInjectionConstructor,就找出标记的有ZXInjectionConstructor特性的构造函数
if (type.GetConstructors().Count(c => c.IsDefined(typeof(ZXInjectionConstructor), true)) > 0)
{
ctor = type.GetConstructors().Where(c => c.IsDefined(typeof(ZXInjectionConstructor), true)).OrderByDescending(c => c.GetParameters().Length).FirstOrDefault();
}
else
{
ctor = type.GetConstructors().OrderByDescending(c => c.GetParameters().Length).First();
}

List<object> parameterlist = new List<object>();
foreach (ParameterInfo parameter in ctor.GetParameters())
{
string parameterType = parameter.ParameterType.FullName;
Type targetType = ZXContainerDicationary[parameterType];
object oParameter = this.ObjectInstance(targetType); //隐形的跳出条件:找到最后后一个依赖的五参数构造函数后就不再去递归了;
parameterlist.Add(oParameter);
}

object oInstance = Activator.CreateInstance(type, parameterlist.ToArray());//调用的五参数构造函数;//如果有两级依赖
return oInstance;

}
}
}

IZXContainer container = new ZXContainer();//创建一个容器
container.RegisterType<IPhone, ApplePhone>();//告诉容器---抽象和细节的关系
container.RegisterType<IHeadphone, Headphone>();
container.RegisterType<IMicrophone, Microphone>();
container.RegisterType<IPower, Power>();
container.RegisterType<IBaseBll, BaseBll>();
IPhone phone = container.Resolve<IPhone>();//获取对象的实例

 

反射:

public class ObjectFactory
{
public static IPhone CreatePhone()
{
string classModule = ConfigurationManager.AppSettings["iPhoneType"];
Assembly assemly = Assembly.Load(classModule.Split(',')[1]);
Type type = assemly.GetType(classModule.Split(',')[0]);
return (IPhone)Activator.CreateInstance(type);//无参数构造函数
}

public static IPhone CreatePhone(IBaseBll iBLL)
{
string classModule = ConfigurationManager.AppSettings["iPhoneType"];
Assembly assemly = Assembly.Load(classModule.Split(',')[1]);
Type type = assemly.GetType(classModule.Split(',')[0]);
return (IPhone)Activator.CreateInstance(type, new object[] { iBLL });
}
}