mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
feat(ioc): 添加基于Type的依赖注入方法
- 实现Get(Type type)方法以获取指定类型的单个实例 - 实现GetRequired(Type type)方法以获取指定类型的必需实例 - 实现GetAll(Type type)方法以获取指定类型的所有实例 - 在抽象层添加对应的接口定义 - 为新方法添加完整的XML文档注释 - 添加全局using System.Runtime指令
This commit is contained in:
parent
9427cc9dbf
commit
68d292dc64
@ -13,6 +13,7 @@
|
|||||||
|
|
||||||
global using System;
|
global using System;
|
||||||
global using System.Collections.Generic;
|
global using System.Collections.Generic;
|
||||||
|
global using System.Runtime;
|
||||||
global using System.Linq;
|
global using System.Linq;
|
||||||
global using System.Threading;
|
global using System.Threading;
|
||||||
global using System.Threading.Tasks;
|
global using System.Threading.Tasks;
|
||||||
@ -59,6 +59,15 @@ public interface IIocContainer : IContextAware
|
|||||||
/// <returns>找到的第一个实例;如果未找到则返回 null</returns>
|
/// <returns>找到的第一个实例;如果未找到则返回 null</returns>
|
||||||
T? Get<T>() where T : class;
|
T? Get<T>() where T : class;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 根据指定类型获取单个实例
|
||||||
|
/// 如果存在多个,只返回第一个
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="type">期望获取的实例类型</param>
|
||||||
|
/// <returns>找到的第一个实例;如果未找到则返回 null</returns>
|
||||||
|
object? Get(Type type);
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取指定类型的必需实例
|
/// 获取指定类型的必需实例
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -67,6 +76,15 @@ public interface IIocContainer : IContextAware
|
|||||||
/// <exception cref="InvalidOperationException">当没有注册实例或注册了多个实例时抛出</exception>
|
/// <exception cref="InvalidOperationException">当没有注册实例或注册了多个实例时抛出</exception>
|
||||||
T GetRequired<T>() where T : class;
|
T GetRequired<T>() where T : class;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取指定类型的必需实例
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="type">期望获取的实例类型</param>
|
||||||
|
/// <returns>找到的唯一实例</returns>
|
||||||
|
/// <exception cref="InvalidOperationException">当没有注册实例或注册了多个实例时抛出</exception>
|
||||||
|
object GetRequired(Type type);
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取指定类型的所有实例(接口 / 抽象类推荐使用)
|
/// 获取指定类型的所有实例(接口 / 抽象类推荐使用)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -74,6 +92,14 @@ public interface IIocContainer : IContextAware
|
|||||||
/// <returns>所有符合条件的实例列表;如果没有则返回空数组</returns>
|
/// <returns>所有符合条件的实例列表;如果没有则返回空数组</returns>
|
||||||
IReadOnlyList<T> GetAll<T>() where T : class;
|
IReadOnlyList<T> GetAll<T>() where T : class;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取指定类型的所有实例
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="type">期望获取的实例类型</param>
|
||||||
|
/// <returns>所有符合条件的实例列表;如果没有则返回空数组</returns>
|
||||||
|
IReadOnlyList<object> GetAll(Type type);
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取并排序(系统调度专用)
|
/// 获取并排序(系统调度专用)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@ -233,6 +233,33 @@ public class IocContainer : ContextAwareBase, IIocContainer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取指定类型的单个实例
|
||||||
|
/// 如果存在多个,只返回第一个
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="type">期望获取的实例类型</param>
|
||||||
|
/// <returns>找到的第一个实例;如果未找到则返回 null</returns>
|
||||||
|
public object? Get(Type type)
|
||||||
|
{
|
||||||
|
_lock.EnterReadLock();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (_typeIndex.TryGetValue(type, out var set) && set.Count > 0)
|
||||||
|
{
|
||||||
|
var result = set.First();
|
||||||
|
_logger.Debug($"Retrieved instance: {type.Name}");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.Debug($"No instance found for type: {type.Name}");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_lock.ExitReadLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取指定类型的必需实例
|
/// 获取指定类型的必需实例
|
||||||
@ -262,6 +289,34 @@ public class IocContainer : ContextAwareBase, IIocContainer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取指定类型的必需实例
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="type">期望获取的实例类型</param>
|
||||||
|
/// <returns>找到的唯一实例</returns>
|
||||||
|
/// <exception cref="InvalidOperationException">当没有注册实例或注册了多个实例时抛出</exception>
|
||||||
|
public object GetRequired(Type type)
|
||||||
|
{
|
||||||
|
var list = GetAll(type);
|
||||||
|
|
||||||
|
switch (list.Count)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
var notFoundMsg = $"No instance registered for {type.Name}";
|
||||||
|
_logger.Error(notFoundMsg);
|
||||||
|
throw new InvalidOperationException(notFoundMsg);
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
_logger.Debug($"Retrieved required instance: {type.Name}");
|
||||||
|
return list[0];
|
||||||
|
|
||||||
|
default:
|
||||||
|
var multipleMsg = $"Multiple instances registered for {type.Name}";
|
||||||
|
_logger.Error(multipleMsg);
|
||||||
|
throw new InvalidOperationException(multipleMsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取指定类型的所有实例(接口 / 抽象类推荐使用)
|
/// 获取指定类型的所有实例(接口 / 抽象类推荐使用)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -282,6 +337,26 @@ public class IocContainer : ContextAwareBase, IIocContainer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取指定类型的所有实例
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="type">期望获取的实例类型</param>
|
||||||
|
/// <returns>所有符合条件的实例列表;如果没有则返回空数组</returns>
|
||||||
|
public IReadOnlyList<object> GetAll(Type type)
|
||||||
|
{
|
||||||
|
_lock.EnterReadLock();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _typeIndex.TryGetValue(type, out var set)
|
||||||
|
? set.ToList() // 快照
|
||||||
|
: Array.Empty<object>();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_lock.ExitReadLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取并排序(系统调度专用)
|
/// 获取并排序(系统调度专用)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user