From 3ad4913f9e6ddabe67ba97a946be9ec0b97ac872 Mon Sep 17 00:00:00 2001
From: GeWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Mon, 2 Feb 2026 21:59:31 +0800
Subject: [PATCH] =?UTF-8?q?refactor(architecture):=20=E4=BC=98=E5=8C=96?=
=?UTF-8?q?=E6=9E=B6=E6=9E=84=E4=B8=8A=E4=B8=8B=E6=96=87=E7=9A=84=E6=9E=84?=
=?UTF-8?q?=E9=80=A0=E5=87=BD=E6=95=B0=E5=92=8C=E6=9C=8D=E5=8A=A1=E8=8E=B7?=
=?UTF-8?q?=E5=8F=96=E9=80=BB=E8=BE=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 使用主构造函数简化 ArchitectureContext 的初始化
- 移除私有字段赋值的冗余代码
- 添加 GetOrCache 方法的详细注释说明
- 为关键业务逻辑添加中文注释
- 保持服务缓存和服务获取的核心功能不变
---
.../architecture/ArchitectureContext.cs | 24 +++++++++++++------
1 file changed, 17 insertions(+), 7 deletions(-)
diff --git a/GFramework.Core/architecture/ArchitectureContext.cs b/GFramework.Core/architecture/ArchitectureContext.cs
index 744d371..a9776ec 100644
--- a/GFramework.Core/architecture/ArchitectureContext.cs
+++ b/GFramework.Core/architecture/ArchitectureContext.cs
@@ -13,33 +13,43 @@ namespace GFramework.Core.architecture;
///
/// 架构上下文类,提供对系统、模型、工具等组件的访问以及命令、查询、事件的执行管理
///
-public class ArchitectureContext : IArchitectureContext
+public class ArchitectureContext(IIocContainer container) : IArchitectureContext
{
- private readonly IIocContainer _container;
+ private readonly IIocContainer _container = container ?? throw new ArgumentNullException(nameof(container));
private readonly Dictionary _serviceCache = new();
- public ArchitectureContext(IIocContainer container)
- {
- _container = container ?? throw new ArgumentNullException(nameof(container));
- }
-
+ ///
+ /// 获取指定类型的服务实例,如果缓存中存在则直接返回,否则从容器中获取并缓存
+ ///
+ /// 服务类型,必须为引用类型
+ /// 服务实例,如果未找到则返回null
public TService? GetService() where TService : class
{
return GetOrCache();
}
+ ///
+ /// 从缓存中获取或创建指定类型的服务实例
+ /// 首先尝试从缓存中获取服务实例,如果缓存中不存在则从容器中获取并存入缓存
+ ///
+ /// 服务类型,必须为引用类型
+ /// 服务实例,如果未找到则返回null
private TService? GetOrCache() where TService : class
{
+ // 尝试从服务缓存中获取已存在的服务实例
if (_serviceCache.TryGetValue(typeof(TService), out var cached))
return (TService)cached;
+ // 从依赖注入容器中获取服务实例
var service = _container.Get();
+ // 如果服务实例存在,则将其存入缓存以供后续使用
if (service != null)
_serviceCache[typeof(TService)] = service;
return service;
}
+
#region Query Execution
///