mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-25 04:59:01 +08:00
refactor(architecture): 优化架构上下文中的服务缓存和空值处理
- 将服务缓存从 Dictionary 替换为 ConcurrentDictionary 以提高线程安全性 - 移除所有 GetService 相关方法的可空返回类型,统一改为非空返回 - 修改 GetOrCache 方法实现,使用 GetOrAdd 方法简化缓存逻辑 - 更新命令和事件总线调用方式,移除空值检查操作符 - 调整接口契约,明确服务不存在时抛出异常而非返回 null - 优化依赖注入容器的服务获取流程,增强错误处理机制
This commit is contained in:
parent
aeed1f903c
commit
95de78efae
@ -19,8 +19,8 @@ public interface IArchitectureContext
|
|||||||
/// 获取指定类型的服务实例
|
/// 获取指定类型的服务实例
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TService">服务类型</typeparam>
|
/// <typeparam name="TService">服务类型</typeparam>
|
||||||
/// <returns>服务实例,如果不存在则返回null</returns>
|
/// <returns>服务实例,如果不存在则抛出异常</returns>
|
||||||
TService? GetService<TService>() where TService : class;
|
TService GetService<TService>() where TService : class;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取指定类型的所有服务实例
|
/// 获取指定类型的所有服务实例
|
||||||
@ -33,8 +33,8 @@ public interface IArchitectureContext
|
|||||||
/// 获取指定类型的系统实例
|
/// 获取指定类型的系统实例
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TSystem">系统类型,必须继承自ISystem接口</typeparam>
|
/// <typeparam name="TSystem">系统类型,必须继承自ISystem接口</typeparam>
|
||||||
/// <returns>系统实例,如果不存在则返回null</returns>
|
/// <returns>系统实例,如果不存在则抛出异常</returns>
|
||||||
TSystem? GetSystem<TSystem>() where TSystem : class, ISystem;
|
TSystem GetSystem<TSystem>() where TSystem : class, ISystem;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取指定类型的所有系统实例
|
/// 获取指定类型的所有系统实例
|
||||||
@ -47,8 +47,8 @@ public interface IArchitectureContext
|
|||||||
/// 获取指定类型的模型实例
|
/// 获取指定类型的模型实例
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TModel">模型类型,必须继承自IModel接口</typeparam>
|
/// <typeparam name="TModel">模型类型,必须继承自IModel接口</typeparam>
|
||||||
/// <returns>模型实例,如果不存在则返回null</returns>
|
/// <returns>模型实例,如果不存在则抛出异常</returns>
|
||||||
TModel? GetModel<TModel>() where TModel : class, IModel;
|
TModel GetModel<TModel>() where TModel : class, IModel;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取指定类型的所有模型实例
|
/// 获取指定类型的所有模型实例
|
||||||
@ -61,8 +61,8 @@ public interface IArchitectureContext
|
|||||||
/// 获取指定类型的工具类实例
|
/// 获取指定类型的工具类实例
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TUtility">工具类类型,必须继承自IUtility接口</typeparam>
|
/// <typeparam name="TUtility">工具类类型,必须继承自IUtility接口</typeparam>
|
||||||
/// <returns>工具类实例,如果不存在则返回null</returns>
|
/// <returns>工具类实例,如果不存在则抛出异常</returns>
|
||||||
TUtility? GetUtility<TUtility>() where TUtility : class, IUtility;
|
TUtility GetUtility<TUtility>() where TUtility : class, IUtility;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取指定类型的所有工具类实例
|
/// 获取指定类型的所有工具类实例
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
using System.Collections.Concurrent;
|
||||||
using GFramework.Core.Abstractions.Architecture;
|
using GFramework.Core.Abstractions.Architecture;
|
||||||
using GFramework.Core.Abstractions.Command;
|
using GFramework.Core.Abstractions.Command;
|
||||||
using GFramework.Core.Abstractions.Environment;
|
using GFramework.Core.Abstractions.Environment;
|
||||||
@ -18,31 +19,31 @@ namespace GFramework.Core.Architectures;
|
|||||||
public class ArchitectureContext(IIocContainer container) : IArchitectureContext
|
public class ArchitectureContext(IIocContainer container) : IArchitectureContext
|
||||||
{
|
{
|
||||||
private readonly IIocContainer _container = container ?? throw new ArgumentNullException(nameof(container));
|
private readonly IIocContainer _container = container ?? throw new ArgumentNullException(nameof(container));
|
||||||
private readonly Dictionary<Type, object> _serviceCache = new();
|
private readonly ConcurrentDictionary<Type, object> _serviceCache = new();
|
||||||
|
|
||||||
#region Mediator Integration
|
#region Mediator Integration
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取 Mediator 实例(延迟加载)
|
/// 获取 Mediator 实例(延迟加载)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private IMediator? Mediator => GetOrCache<IMediator>();
|
private IMediator Mediator => GetOrCache<IMediator>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取 ISender 实例(更轻量的发送器)
|
/// 获取 ISender 实例(更轻量的发送器)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private ISender? Sender => GetOrCache<ISender>();
|
private ISender Sender => GetOrCache<ISender>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取 IPublisher 实例(用于发布通知)
|
/// 获取 IPublisher 实例(用于发布通知)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private IPublisher? Publisher => GetOrCache<IPublisher>();
|
private IPublisher Publisher => GetOrCache<IPublisher>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取指定类型的服务实例,如果缓存中存在则直接返回,否则从容器中获取并缓存
|
/// 获取指定类型的服务实例,如果缓存中存在则直接返回,否则从容器中获取并缓存
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TService">服务类型,必须为引用类型</typeparam>
|
/// <typeparam name="TService">服务类型,必须为引用类型</typeparam>
|
||||||
/// <returns>服务实例,如果未找到则返回null</returns>
|
/// <returns>服务实例,如果不存在则抛出异常</returns>
|
||||||
public TService? GetService<TService>() where TService : class
|
public TService GetService<TService>() where TService : class
|
||||||
{
|
{
|
||||||
return GetOrCache<TService>();
|
return GetOrCache<TService>();
|
||||||
}
|
}
|
||||||
@ -52,20 +53,14 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
|
|||||||
/// 首先尝试从缓存中获取服务实例,如果缓存中不存在则从容器中获取并存入缓存
|
/// 首先尝试从缓存中获取服务实例,如果缓存中不存在则从容器中获取并存入缓存
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TService">服务类型,必须为引用类型</typeparam>
|
/// <typeparam name="TService">服务类型,必须为引用类型</typeparam>
|
||||||
/// <returns>服务实例,如果未找到则返回null</returns>
|
/// <returns>服务实例,如果不存在则抛出异常</returns>
|
||||||
private TService? GetOrCache<TService>() where TService : class
|
private TService GetOrCache<TService>() where TService : class
|
||||||
{
|
{
|
||||||
// 尝试从服务缓存中获取已存在的服务实例
|
return (TService)_serviceCache.GetOrAdd(
|
||||||
if (_serviceCache.TryGetValue(typeof(TService), out var cached))
|
typeof(TService),
|
||||||
return (TService)cached;
|
_ => _container.Get<TService>()
|
||||||
|
?? throw new InvalidOperationException(
|
||||||
// 从依赖注入容器中获取服务实例
|
$"Service {typeof(TService)} not registered"));
|
||||||
var service = _container.Get<TService>();
|
|
||||||
// 如果服务实例存在,则将其存入缓存以供后续使用
|
|
||||||
if (service != null)
|
|
||||||
_serviceCache[typeof(TService)] = service;
|
|
||||||
|
|
||||||
return service;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -248,7 +243,7 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TSystem">目标系统类型</typeparam>
|
/// <typeparam name="TSystem">目标系统类型</typeparam>
|
||||||
/// <returns>对应的系统实例</returns>
|
/// <returns>对应的系统实例</returns>
|
||||||
public TSystem? GetSystem<TSystem>() where TSystem : class, ISystem
|
public TSystem GetSystem<TSystem>() where TSystem : class, ISystem
|
||||||
{
|
{
|
||||||
return GetService<TSystem>();
|
return GetService<TSystem>();
|
||||||
}
|
}
|
||||||
@ -268,7 +263,7 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TModel">目标模型类型</typeparam>
|
/// <typeparam name="TModel">目标模型类型</typeparam>
|
||||||
/// <returns>对应的模型实例</returns>
|
/// <returns>对应的模型实例</returns>
|
||||||
public TModel? GetModel<TModel>() where TModel : class, IModel
|
public TModel GetModel<TModel>() where TModel : class, IModel
|
||||||
{
|
{
|
||||||
return GetService<TModel>();
|
return GetService<TModel>();
|
||||||
}
|
}
|
||||||
@ -288,7 +283,7 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TUtility">目标工具类型</typeparam>
|
/// <typeparam name="TUtility">目标工具类型</typeparam>
|
||||||
/// <returns>对应的工具实例</returns>
|
/// <returns>对应的工具实例</returns>
|
||||||
public TUtility? GetUtility<TUtility>() where TUtility : class, IUtility
|
public TUtility GetUtility<TUtility>() where TUtility : class, IUtility
|
||||||
{
|
{
|
||||||
return GetService<TUtility>();
|
return GetService<TUtility>();
|
||||||
}
|
}
|
||||||
@ -416,7 +411,7 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
|
|||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(command);
|
ArgumentNullException.ThrowIfNull(command);
|
||||||
var commandBus = GetOrCache<ICommandExecutor>();
|
var commandBus = GetOrCache<ICommandExecutor>();
|
||||||
commandBus?.Send(command);
|
commandBus.Send(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -444,7 +439,7 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
|
|||||||
public void SendEvent<TEvent>() where TEvent : new()
|
public void SendEvent<TEvent>() where TEvent : new()
|
||||||
{
|
{
|
||||||
var eventBus = GetOrCache<IEventBus>();
|
var eventBus = GetOrCache<IEventBus>();
|
||||||
eventBus?.Send<TEvent>();
|
eventBus.Send<TEvent>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -456,7 +451,7 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
|
|||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(e);
|
ArgumentNullException.ThrowIfNull(e);
|
||||||
var eventBus = GetOrCache<IEventBus>();
|
var eventBus = GetOrCache<IEventBus>();
|
||||||
eventBus?.Send(e);
|
eventBus.Send(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -482,7 +477,7 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext
|
|||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(onEvent);
|
ArgumentNullException.ThrowIfNull(onEvent);
|
||||||
var eventBus = GetOrCache<IEventBus>();
|
var eventBus = GetOrCache<IEventBus>();
|
||||||
eventBus?.UnRegister(onEvent);
|
eventBus.UnRegister(onEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user