2024-09-12 20:32:54 +08:00
|
|
|
|
using Serein.Library.Api;
|
|
|
|
|
|
using Serein.Library.Attributes;
|
2024-09-17 14:20:27 +08:00
|
|
|
|
using Serein.Library.Web;
|
2024-09-12 20:32:54 +08:00
|
|
|
|
using System;
|
2024-08-06 15:41:14 +08:00
|
|
|
|
using System.Collections.Concurrent;
|
2024-09-12 20:32:54 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2024-08-06 15:41:14 +08:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
2024-09-12 20:32:54 +08:00
|
|
|
|
namespace Serein.Library.Utils
|
2024-08-06 15:41:14 +08:00
|
|
|
|
{
|
2024-09-16 19:53:36 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// IOC管理容器
|
|
|
|
|
|
/// </summary>
|
2024-09-16 21:38:34 +08:00
|
|
|
|
public class SereinIOC : ISereinIOC
|
2024-08-06 15:41:14 +08:00
|
|
|
|
{
|
2024-09-16 19:53:36 +08:00
|
|
|
|
/// <summary>
|
2024-09-17 14:20:27 +08:00
|
|
|
|
/// 类型集合,暂放待实例化的类型,完成实例化之后移除
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private readonly ConcurrentDictionary<string, Type> _typeMappings;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 实例集合(包含已完成注入、未完成注入的对象实例,计划在未来的版本中区分:)
|
2024-09-16 19:53:36 +08:00
|
|
|
|
/// </summary>
|
2024-08-06 15:41:14 +08:00
|
|
|
|
private readonly ConcurrentDictionary<string, object> _dependencies;
|
2024-09-17 14:20:27 +08:00
|
|
|
|
|
2024-09-16 19:53:36 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 未完成注入的实例集合。
|
|
|
|
|
|
/// 键:需要的类型名称
|
2024-09-17 14:20:27 +08:00
|
|
|
|
/// 值:元组(对象实例,对象的属性)
|
2024-09-16 19:53:36 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
private readonly ConcurrentDictionary<string, List<(object,PropertyInfo)>> _unfinishedDependencies;
|
|
|
|
|
|
|
2024-09-17 14:20:27 +08:00
|
|
|
|
|
2024-09-16 19:53:36 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 待实例化的类型
|
|
|
|
|
|
/// </summary>
|
2024-09-17 14:20:27 +08:00
|
|
|
|
// private readonly List<Type> _waitingForInstantiation;
|
2024-08-06 15:41:14 +08:00
|
|
|
|
|
2024-09-16 21:38:34 +08:00
|
|
|
|
public SereinIOC()
|
2024-08-06 15:41:14 +08:00
|
|
|
|
{
|
2024-09-16 19:53:36 +08:00
|
|
|
|
// 首先注册自己
|
2024-09-17 14:20:27 +08:00
|
|
|
|
_dependencies = new ConcurrentDictionary<string, object>();
|
|
|
|
|
|
_typeMappings = new ConcurrentDictionary<string, Type>();
|
2024-09-16 19:53:36 +08:00
|
|
|
|
_unfinishedDependencies = new ConcurrentDictionary<string, List<(object, PropertyInfo)>>();
|
2024-08-06 15:41:14 +08:00
|
|
|
|
}
|
2024-09-16 19:53:36 +08:00
|
|
|
|
|
2024-09-17 14:20:27 +08:00
|
|
|
|
public void InitRegister()
|
|
|
|
|
|
{
|
|
|
|
|
|
_dependencies[typeof(ISereinIOC).FullName] = this;
|
|
|
|
|
|
Register<IRouter, Router>();
|
|
|
|
|
|
/*foreach (var type in _typeMappings.Values)
|
|
|
|
|
|
{
|
|
|
|
|
|
Register(type);
|
|
|
|
|
|
}
|
|
|
|
|
|
Build();*/
|
|
|
|
|
|
}
|
2024-09-16 21:38:34 +08:00
|
|
|
|
|
|
|
|
|
|
#region 类型的注册
|
2024-08-06 15:41:14 +08:00
|
|
|
|
|
2024-09-16 19:53:36 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 注册类型
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="type">目标类型</param>
|
|
|
|
|
|
/// <param name="parameters">参数</param>
|
2024-09-16 21:38:34 +08:00
|
|
|
|
public ISereinIOC Register(Type type, params object[] parameters)
|
2024-08-06 15:41:14 +08:00
|
|
|
|
{
|
2024-09-17 14:20:27 +08:00
|
|
|
|
RegisterType(type?.FullName, type);
|
2024-08-06 15:41:14 +08:00
|
|
|
|
return this;
|
|
|
|
|
|
}
|
2024-09-16 19:53:36 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 注册类型
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="type">目标类型</param>
|
|
|
|
|
|
/// <param name="parameters">参数</param>
|
2024-09-16 21:38:34 +08:00
|
|
|
|
public ISereinIOC Register<T>(params object[] parameters)
|
2024-08-06 15:41:14 +08:00
|
|
|
|
{
|
2024-09-16 19:53:36 +08:00
|
|
|
|
var type = typeof(T);
|
|
|
|
|
|
RegisterType(type.FullName, type);
|
2024-08-06 15:41:14 +08:00
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-17 14:20:27 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 注册接口类型
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="type">目标类型</param>
|
|
|
|
|
|
/// <param name="parameters">参数</param>
|
2024-09-16 21:38:34 +08:00
|
|
|
|
public ISereinIOC Register<TService, TImplementation>(params object[] parameters)
|
2024-08-06 15:41:14 +08:00
|
|
|
|
where TImplementation : TService
|
|
|
|
|
|
{
|
2024-09-16 19:53:36 +08:00
|
|
|
|
var typeFullName = typeof(TService).FullName;
|
|
|
|
|
|
RegisterType(typeFullName, typeof(TImplementation));
|
2024-08-06 15:41:14 +08:00
|
|
|
|
return this;
|
2024-09-17 15:58:37 +08:00
|
|
|
|
}
|
2024-09-16 21:38:34 +08:00
|
|
|
|
#endregion
|
2024-08-06 15:41:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-09-16 19:53:36 +08:00
|
|
|
|
/// <summary>
|
2024-09-17 15:58:37 +08:00
|
|
|
|
/// 尝试从容器中获取对象,如果不存在目标类型的对象,则将类型信息登记到容器,并实例化注入依赖项。如果依然无法注册,则返回null。
|
2024-09-16 19:53:36 +08:00
|
|
|
|
/// </summary>
|
2024-09-17 14:20:27 +08:00
|
|
|
|
public object GetOrRegisterInstantiate(Type type)
|
2024-09-16 19:53:36 +08:00
|
|
|
|
{
|
2024-09-16 21:38:34 +08:00
|
|
|
|
// 尝试从容器中获取对象
|
2024-08-06 15:41:14 +08:00
|
|
|
|
if (!_dependencies.TryGetValue(type.FullName, out object value))
|
|
|
|
|
|
{
|
2024-09-17 15:58:37 +08:00
|
|
|
|
// 容器中不存在目标类型的对象
|
|
|
|
|
|
if (type.IsInterface)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_typeMappings.TryGetValue(type.FullName, out Type implementationType))
|
|
|
|
|
|
{
|
|
|
|
|
|
// 是接口类型,存在注册信息
|
|
|
|
|
|
Register(type);// 注册类型信息
|
|
|
|
|
|
value = Instantiate(implementationType); // 创建实例对象,并注入依赖
|
|
|
|
|
|
_dependencies.TryAdd(type.FullName, value); // 登记到IOC容器中
|
|
|
|
|
|
_typeMappings.TryRemove(type.FullName, out _); // 取消类型的注册信息
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
//需要获取接口类型的实例,但不存在类型注册信息
|
|
|
|
|
|
Console.WriteLine("当前需要获取接口,但没有注册实现类的类型,无法创建接口实例");
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// 不是接口,直接注册
|
|
|
|
|
|
Register(type);// 注册类型信息
|
|
|
|
|
|
value = Instantiate(type); // 创建实例对象,并注入依赖
|
|
|
|
|
|
_dependencies.TryAdd(type.FullName, value); // 登记到IOC容器中
|
|
|
|
|
|
}
|
2024-08-06 15:41:14 +08:00
|
|
|
|
}
|
2024-09-16 21:38:34 +08:00
|
|
|
|
return value;
|
|
|
|
|
|
}
|
2024-08-06 15:41:14 +08:00
|
|
|
|
|
2024-09-16 21:38:34 +08:00
|
|
|
|
/// <summary>
|
2024-09-17 15:58:37 +08:00
|
|
|
|
/// 尝试从容器中获取对象,如果不存在目标类型的对象,则将类型信息登记到容器,并实例化注入依赖项。如果依然无法注册,则返回null。
|
2024-09-16 21:38:34 +08:00
|
|
|
|
/// </summary>
|
2024-09-17 14:20:27 +08:00
|
|
|
|
public T GetOrRegisterInstantiate<T>()
|
2024-09-16 21:38:34 +08:00
|
|
|
|
{
|
2024-09-17 15:58:37 +08:00
|
|
|
|
return (T)GetOrRegisterInstantiate(typeof(T));
|
2024-09-16 21:38:34 +08:00
|
|
|
|
}
|
2024-08-06 15:41:14 +08:00
|
|
|
|
|
2024-09-16 21:38:34 +08:00
|
|
|
|
/// <summary>
|
2024-09-17 21:43:49 +08:00
|
|
|
|
/// 根据类型生成对应的实例,并注入其中的依赖项(类型信息不登记到IOC容器中),类型创建后自动注入其它需要此类型的对象
|
2024-09-16 21:38:34 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="controllerType"></param>
|
|
|
|
|
|
/// <param name="parameters"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public object Instantiate(Type controllerType, params object[] parameters)
|
|
|
|
|
|
{
|
|
|
|
|
|
var instance = CreateInstance(controllerType, parameters); // 创建目标类型的实例
|
|
|
|
|
|
if(instance != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
InjectDependencies(instance); // 完成创建后注入实例需要的依赖项
|
|
|
|
|
|
}
|
|
|
|
|
|
return instance;
|
2024-08-06 15:41:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-16 21:38:34 +08:00
|
|
|
|
#region 容器管理(清空,绑定)
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 清空容器对象
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public ISereinIOC Reset()
|
2024-08-06 15:41:14 +08:00
|
|
|
|
{
|
2024-09-16 21:38:34 +08:00
|
|
|
|
// 检查是否存在非托管资源
|
|
|
|
|
|
foreach (var instancei in _dependencies.Values)
|
2024-08-06 15:41:14 +08:00
|
|
|
|
{
|
2024-09-16 21:38:34 +08:00
|
|
|
|
if (typeof(IDisposable).IsAssignableFrom(instancei.GetType()) && instancei is IDisposable disposable)
|
|
|
|
|
|
{
|
|
|
|
|
|
disposable?.Dispose();
|
|
|
|
|
|
}
|
2024-08-06 15:41:14 +08:00
|
|
|
|
}
|
2024-09-16 21:38:34 +08:00
|
|
|
|
_unfinishedDependencies?.Clear();
|
|
|
|
|
|
_typeMappings?.Clear();
|
|
|
|
|
|
_dependencies?.Clear();
|
2024-09-17 14:20:27 +08:00
|
|
|
|
// _waitingForInstantiation?.Clear();
|
2024-09-16 21:38:34 +08:00
|
|
|
|
return this;
|
2024-08-06 15:41:14 +08:00
|
|
|
|
}
|
2024-09-16 19:53:36 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 实例化所有已注册的类型,并尝试绑定
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2024-09-16 21:38:34 +08:00
|
|
|
|
public ISereinIOC Build()
|
2024-08-06 15:41:14 +08:00
|
|
|
|
{
|
2024-09-17 14:20:27 +08:00
|
|
|
|
InitRegister();
|
2024-09-15 19:48:27 +08:00
|
|
|
|
// 遍历已注册类型
|
2024-09-16 19:53:36 +08:00
|
|
|
|
foreach (var type in _typeMappings.Values.ToArray())
|
2024-08-06 15:41:14 +08:00
|
|
|
|
{
|
2024-09-16 21:38:34 +08:00
|
|
|
|
|
|
|
|
|
|
if (_dependencies.ContainsKey(type.FullName))
|
2024-09-16 19:53:36 +08:00
|
|
|
|
{
|
|
|
|
|
|
// 已经存在实例,不用管
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
2024-08-06 15:41:14 +08:00
|
|
|
|
{
|
2024-09-16 19:53:36 +08:00
|
|
|
|
// 如果没有创建实例,则创建对应的实例
|
|
|
|
|
|
_dependencies[type.FullName] = CreateInstance(type);
|
2024-08-06 15:41:14 +08:00
|
|
|
|
}
|
2024-09-16 19:53:36 +08:00
|
|
|
|
// 移除类型的注册记录
|
2024-09-16 21:38:34 +08:00
|
|
|
|
_typeMappings.TryRemove(type.FullName, out _);
|
2024-08-06 15:41:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-15 19:48:27 +08:00
|
|
|
|
// 注入实例的依赖项
|
2024-08-06 15:41:14 +08:00
|
|
|
|
foreach (var instance in _dependencies.Values)
|
|
|
|
|
|
{
|
2024-09-16 19:53:36 +08:00
|
|
|
|
InjectDependencies(instance);
|
2024-08-06 15:41:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//var instance = Instantiate(item.Value);
|
|
|
|
|
|
|
2024-09-16 19:53:36 +08:00
|
|
|
|
// TryInstantiateWaitingDependencies();
|
2024-08-06 15:41:14 +08:00
|
|
|
|
|
2024-09-16 21:38:34 +08:00
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
2024-08-06 15:41:14 +08:00
|
|
|
|
|
2024-09-16 21:38:34 +08:00
|
|
|
|
#region 私有方法
|
2024-09-16 19:53:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 注册类型
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="typeFull"></param>
|
|
|
|
|
|
/// <param name="type"></param>
|
|
|
|
|
|
private void RegisterType(string typeFull, Type type)
|
2024-08-06 15:41:14 +08:00
|
|
|
|
{
|
2024-09-16 19:53:36 +08:00
|
|
|
|
if (!_typeMappings.ContainsKey(typeFull))
|
|
|
|
|
|
{
|
|
|
|
|
|
_typeMappings[typeFull] = type;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-08-06 15:41:14 +08:00
|
|
|
|
|
2024-09-16 19:53:36 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 创建实例时,尝试注入到由ioc容器管理、并需要此实例的对象。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private object CreateInstance(Type type, params object[] parameters)
|
|
|
|
|
|
{
|
|
|
|
|
|
var instance = Activator.CreateInstance(type);
|
2024-09-16 21:38:34 +08:00
|
|
|
|
if (_unfinishedDependencies.TryGetValue(type.FullName, out var unfinishedPropertyList))
|
2024-09-16 19:53:36 +08:00
|
|
|
|
{
|
|
|
|
|
|
foreach ((object obj, PropertyInfo property) in unfinishedPropertyList)
|
|
|
|
|
|
{
|
|
|
|
|
|
property.SetValue(obj, instance); //注入依赖项
|
|
|
|
|
|
}
|
2024-09-16 21:38:34 +08:00
|
|
|
|
|
|
|
|
|
|
if (_unfinishedDependencies.TryRemove(type.FullName, out unfinishedPropertyList))
|
2024-09-16 19:53:36 +08:00
|
|
|
|
{
|
|
|
|
|
|
unfinishedPropertyList.Clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return instance;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 注入目标实例的依赖项
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="instance"></param>
|
|
|
|
|
|
private bool InjectDependencies(object instance)
|
|
|
|
|
|
{
|
|
|
|
|
|
var properties = instance.GetType()
|
|
|
|
|
|
.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToArray()
|
|
|
|
|
|
.Where(p => p.CanWrite // 可写属性
|
|
|
|
|
|
&& p.GetCustomAttribute<AutoInjectionAttribute>() != null // 有特性标注需要注入
|
|
|
|
|
|
&& p.GetValue(instance) == null); // 属性为空
|
|
|
|
|
|
var isPass = true;
|
2024-08-06 15:41:14 +08:00
|
|
|
|
foreach (var property in properties)
|
|
|
|
|
|
{
|
|
|
|
|
|
var propertyType = property.PropertyType;
|
2024-09-16 19:53:36 +08:00
|
|
|
|
// 通过属性类型名称从ioc容器中获取对应的实例
|
2024-08-06 15:41:14 +08:00
|
|
|
|
if (_dependencies.TryGetValue(propertyType.FullName, out var dependencyInstance))
|
|
|
|
|
|
{
|
2024-09-16 19:53:36 +08:00
|
|
|
|
property.SetValue(instance, dependencyInstance); // 尝试写入到目标实例的属性中
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// 存在依赖项,但目标类型的实例暂未加载,需要等待需要实例完成注册
|
2024-09-16 21:38:34 +08:00
|
|
|
|
var unfinishedDependenciesList = _unfinishedDependencies.GetOrAdd(propertyType.FullName, _ = new List<(object, PropertyInfo)>());
|
2024-09-16 19:53:36 +08:00
|
|
|
|
var data = (instance, property);
|
2024-09-16 21:38:34 +08:00
|
|
|
|
if (!unfinishedDependenciesList.Contains(data))
|
|
|
|
|
|
{
|
|
|
|
|
|
unfinishedDependenciesList.Add(data);
|
|
|
|
|
|
}
|
|
|
|
|
|
isPass = false;
|
2024-08-06 15:41:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-09-16 19:53:36 +08:00
|
|
|
|
return isPass;
|
2024-08-06 15:41:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-16 21:38:34 +08:00
|
|
|
|
|
2024-09-16 19:53:36 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 再次尝试注入目标实例的依赖项
|
|
|
|
|
|
/// </summary>
|
2024-09-17 14:20:27 +08:00
|
|
|
|
//private void TryInstantiateWaitingDependencies()
|
|
|
|
|
|
//{
|
|
|
|
|
|
// foreach (var waitingType in _waitingForInstantiation.ToList())
|
|
|
|
|
|
// {
|
|
|
|
|
|
// if (_typeMappings.TryGetValue(waitingType.FullName, out var implementationType))
|
|
|
|
|
|
// {
|
|
|
|
|
|
// var instance = Instantiate(implementationType);
|
|
|
|
|
|
// if (instance != null)
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
|
|
|
|
// _dependencies[waitingType.FullName] = instance;
|
|
|
|
|
|
|
|
|
|
|
|
// _waitingForInstantiation.Remove(waitingType);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
//}
|
2024-09-16 21:38:34 +08:00
|
|
|
|
#endregion
|
2024-08-06 15:41:14 +08:00
|
|
|
|
|
|
|
|
|
|
#region run()
|
2024-09-16 21:38:34 +08:00
|
|
|
|
public ISereinIOC Run<T>(Action<T> action)
|
2024-08-06 15:41:14 +08:00
|
|
|
|
{
|
2024-09-17 14:20:27 +08:00
|
|
|
|
var service = GetOrRegisterInstantiate<T>();
|
2024-08-06 15:41:14 +08:00
|
|
|
|
if (service != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
action(service);
|
|
|
|
|
|
}
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-16 21:38:34 +08:00
|
|
|
|
public ISereinIOC Run<T1, T2>(Action<T1, T2> action)
|
2024-08-06 15:41:14 +08:00
|
|
|
|
{
|
2024-09-17 14:20:27 +08:00
|
|
|
|
var service1 = GetOrRegisterInstantiate<T1>();
|
|
|
|
|
|
var service2 = GetOrRegisterInstantiate<T2>();
|
2024-08-06 15:41:14 +08:00
|
|
|
|
|
|
|
|
|
|
action(service1, service2);
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-16 21:38:34 +08:00
|
|
|
|
public ISereinIOC Run<T1, T2, T3>(Action<T1, T2, T3> action)
|
2024-08-06 15:41:14 +08:00
|
|
|
|
{
|
2024-09-17 14:20:27 +08:00
|
|
|
|
var service1 = GetOrRegisterInstantiate<T1>();
|
|
|
|
|
|
var service2 = GetOrRegisterInstantiate<T2>();
|
|
|
|
|
|
var service3 = GetOrRegisterInstantiate<T3>();
|
2024-08-06 15:41:14 +08:00
|
|
|
|
action(service1, service2, service3);
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-16 21:38:34 +08:00
|
|
|
|
public ISereinIOC Run<T1, T2, T3, T4>(Action<T1, T2, T3, T4> action)
|
2024-08-06 15:41:14 +08:00
|
|
|
|
{
|
2024-09-17 14:20:27 +08:00
|
|
|
|
var service1 = GetOrRegisterInstantiate<T1>();
|
|
|
|
|
|
var service2 = GetOrRegisterInstantiate<T2>();
|
|
|
|
|
|
var service3 = GetOrRegisterInstantiate<T3>();
|
|
|
|
|
|
var service4 = GetOrRegisterInstantiate<T4>();
|
2024-08-06 15:41:14 +08:00
|
|
|
|
action(service1, service2, service3, service4);
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-16 21:38:34 +08:00
|
|
|
|
public ISereinIOC Run<T1, T2, T3, T4, T5>(Action<T1, T2, T3, T4, T5> action)
|
2024-08-06 15:41:14 +08:00
|
|
|
|
{
|
2024-09-17 14:20:27 +08:00
|
|
|
|
var service1 = GetOrRegisterInstantiate<T1>();
|
|
|
|
|
|
var service2 = GetOrRegisterInstantiate<T2>();
|
|
|
|
|
|
var service3 = GetOrRegisterInstantiate<T3>();
|
|
|
|
|
|
var service4 = GetOrRegisterInstantiate<T4>();
|
|
|
|
|
|
var service5 = GetOrRegisterInstantiate<T5>();
|
2024-08-06 15:41:14 +08:00
|
|
|
|
action(service1, service2, service3, service4, service5);
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-16 21:38:34 +08:00
|
|
|
|
public ISereinIOC Run<T1, T2, T3, T4, T5, T6>(Action<T1, T2, T3, T4, T5, T6> action)
|
2024-08-06 15:41:14 +08:00
|
|
|
|
{
|
2024-09-17 14:20:27 +08:00
|
|
|
|
var service1 = GetOrRegisterInstantiate<T1>();
|
|
|
|
|
|
var service2 = GetOrRegisterInstantiate<T2>();
|
|
|
|
|
|
var service3 = GetOrRegisterInstantiate<T3>();
|
|
|
|
|
|
var service4 = GetOrRegisterInstantiate<T4>();
|
|
|
|
|
|
var service5 = GetOrRegisterInstantiate<T5>();
|
|
|
|
|
|
var service6 = GetOrRegisterInstantiate<T6>();
|
2024-08-06 15:41:14 +08:00
|
|
|
|
action(service1, service2, service3, service4, service5, service6);
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-16 21:38:34 +08:00
|
|
|
|
public ISereinIOC Run<T1, T2, T3, T4, T5, T6, T7>(Action<T1, T2, T3, T4, T5, T6, T7> action)
|
2024-08-06 15:41:14 +08:00
|
|
|
|
{
|
2024-09-17 14:20:27 +08:00
|
|
|
|
var service1 = GetOrRegisterInstantiate<T1>();
|
|
|
|
|
|
var service2 = GetOrRegisterInstantiate<T2>();
|
|
|
|
|
|
var service3 = GetOrRegisterInstantiate<T3>();
|
|
|
|
|
|
var service4 = GetOrRegisterInstantiate<T4>();
|
|
|
|
|
|
var service5 = GetOrRegisterInstantiate<T5>();
|
|
|
|
|
|
var service6 = GetOrRegisterInstantiate<T6>();
|
|
|
|
|
|
var service7 = GetOrRegisterInstantiate<T7>();
|
2024-08-06 15:41:14 +08:00
|
|
|
|
action(service1, service2, service3, service4, service5, service6, service7);
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-16 21:38:34 +08:00
|
|
|
|
public ISereinIOC Run<T1, T2, T3, T4, T5, T6, T7, T8>(Action<T1, T2, T3, T4, T5, T6, T7, T8> action)
|
2024-08-06 15:41:14 +08:00
|
|
|
|
{
|
2024-09-17 14:20:27 +08:00
|
|
|
|
var service1 = GetOrRegisterInstantiate<T1>();
|
|
|
|
|
|
var service2 = GetOrRegisterInstantiate<T2>();
|
|
|
|
|
|
var service3 = GetOrRegisterInstantiate<T3>();
|
|
|
|
|
|
var service4 = GetOrRegisterInstantiate<T4>();
|
|
|
|
|
|
var service5 = GetOrRegisterInstantiate<T5>();
|
|
|
|
|
|
var service6 = GetOrRegisterInstantiate<T6>();
|
|
|
|
|
|
var service7 = GetOrRegisterInstantiate<T7>();
|
|
|
|
|
|
var service8 = GetOrRegisterInstantiate<T8>();
|
2024-08-06 15:41:14 +08:00
|
|
|
|
action(service1, service2, service3, service4, service5, service6, service7, service8);
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-15 19:48:27 +08:00
|
|
|
|
|
2024-08-06 15:41:14 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* public interface IServiceContainer
|
|
|
|
|
|
{
|
|
|
|
|
|
ServiceContainer Register<T>(params object[] parameters);
|
|
|
|
|
|
ServiceContainer Register<TService, TImplementation>(params object[] parameters) where TImplementation : TService;
|
|
|
|
|
|
TService Resolve<TService>();
|
|
|
|
|
|
void Get<T>(Action<T> action);
|
|
|
|
|
|
object Instantiate(Type type, params object[] parameters);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
public class ServiceContainer : IServiceContainer
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly Dictionary<Type, object> _dependencies;
|
|
|
|
|
|
public ServiceContainer()
|
|
|
|
|
|
{
|
|
|
|
|
|
_dependencies = new Dictionary<Type, object>
|
|
|
|
|
|
{
|
|
|
|
|
|
[typeof(IServiceContainer)] = this
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Get<T>(Action<T> action)
|
|
|
|
|
|
{
|
|
|
|
|
|
var service = Resolve<T>();
|
|
|
|
|
|
action(service);
|
|
|
|
|
|
}
|
|
|
|
|
|
public ServiceContainer Register<T>(params object[] parameters)
|
|
|
|
|
|
{
|
|
|
|
|
|
var instance = Instantiate(typeof(T), parameters);
|
|
|
|
|
|
_dependencies[typeof(T)] = instance;
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ServiceContainer Register<TService, TImplementation>(params object[] parameters)
|
|
|
|
|
|
where TImplementation : TService
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
_dependencies[typeof(TService)] = Instantiate(typeof(TImplementation), parameters);
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public TService Resolve<TService>()
|
|
|
|
|
|
{
|
|
|
|
|
|
return (TService)_dependencies[typeof(TService)];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public object Instantiate(Type controllerType, params object[] parameters)
|
|
|
|
|
|
{
|
|
|
|
|
|
var constructors = controllerType.GetConstructors(); // 获取控制器的所有构造函数
|
|
|
|
|
|
|
|
|
|
|
|
// 查找具有最多参数的构造函数
|
|
|
|
|
|
var constructor = constructors.OrderByDescending(c => c.GetParameters().Length).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
|
|
if (constructor != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (parameters.Length > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Activator.CreateInstance(controllerType, parameters);
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
var tmpParameters = constructor.GetParameters();
|
|
|
|
|
|
var dependencyInstances = new List<object>();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var parameter in tmpParameters)
|
|
|
|
|
|
{
|
|
|
|
|
|
var parameterType = parameter.ParameterType;
|
|
|
|
|
|
_dependencies.TryGetValue(parameterType, out var dependencyInstance);
|
|
|
|
|
|
dependencyInstances.Add(dependencyInstance);
|
|
|
|
|
|
if (dependencyInstance == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// 用解析的依赖项实例化目标类型
|
|
|
|
|
|
return Activator.CreateInstance(controllerType, dependencyInstances.ToArray());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return Activator.CreateInstance(controllerType);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|