refactor(architecture): 优化事件系统泛型约束和命名规范

- 为 SendEvent 方法添加 class 约束以确保类型安全
- 统一泛型参数命名从 T 到 TEvent 提高可读性
- 移除不必要的 using 语句简化文件依赖
- 保持事件发送和注册功能的原有行为不变
This commit is contained in:
GeWuYou 2025-12-25 08:25:12 +08:00
parent 5b7eaea142
commit 270411c66c
2 changed files with 9 additions and 12 deletions

View File

@ -67,7 +67,7 @@ public class ArchitectureRuntime(IArchitectureContext context) : IArchitectureRu
/// </summary>
/// <typeparam name="TEvent">事件类型</typeparam>
/// <param name="e">要发布的事件实例</param>
public void SendEvent<TEvent>(TEvent e)
public void SendEvent<TEvent>(TEvent e) where TEvent : class
{
_context.SendEvent(e);
}

View File

@ -1,9 +1,6 @@
using GFramework.Core.command;
using GFramework.Core.events;
using GFramework.Core.model;
using GFramework.Core.query;
using GFramework.Core.system;
using GFramework.Core.utility;
namespace GFramework.Core.architecture;
@ -39,28 +36,28 @@ public interface IArchitectureRuntime
/// <summary>
/// 发送无参事件
/// </summary>
/// <typeparam name="T">事件类型,必须具有无参构造函数</typeparam>
void SendEvent<T>() where T : new();
/// <typeparam name="TEvent">事件类型,必须具有无参构造函数</typeparam>
void SendEvent<TEvent>() where TEvent : new();
/// <summary>
/// 发送指定的事件实例
/// </summary>
/// <typeparam name="T">事件类型</typeparam>
/// <typeparam name="TEvent">事件类型</typeparam>
/// <param name="e">要发送的事件实例</param>
void SendEvent<T>(T e);
void SendEvent<TEvent>(TEvent e) where TEvent : class;
/// <summary>
/// 注册事件监听器
/// </summary>
/// <typeparam name="T">事件类型</typeparam>
/// <typeparam name="TEvent">事件类型</typeparam>
/// <param name="onEvent">事件触发时的回调方法</param>
/// <returns>用于取消注册的句柄</returns>
IUnRegister RegisterEvent<T>(Action<T> onEvent);
IUnRegister RegisterEvent<TEvent>(Action<TEvent> onEvent);
/// <summary>
/// 取消注册事件监听器
/// </summary>
/// <typeparam name="T">事件类型</typeparam>
/// <typeparam name="TEvent">事件类型</typeparam>
/// <param name="onEvent">要取消注册的事件回调方法</param>
void UnRegisterEvent<T>(Action<T> onEvent);
void UnRegisterEvent<TEvent>(Action<TEvent> onEvent);
}