refactor(events): 将类型事件系统重构为事件总线

- 将TypeEventSystem重命名为EventBus
- 将IEasyEvent接口重命名为IEvent接口
- 将ITypeEventSystem接口重命名为IEventBus接口
- 更新Architecture类中使用TypeEventSystem为EventBus
- 更新ArchitectureContext中依赖注入参数类型
- 将EasyEvent泛型类重命名为Event泛型类
- 更新所有相关类型引用和实现
- 修改接口继承关系以使用新的事件接口命名
This commit is contained in:
GwWuYou 2026-01-11 11:17:30 +08:00
parent 8c8555985d
commit 157b3ce846
13 changed files with 45 additions and 50 deletions

View File

@ -21,7 +21,7 @@ public interface IArchitectureServices : IContextAware
/// 获取类型事件系统
/// </summary>
/// <returns>ITypeEventSystem类型的事件系统实例</returns>
ITypeEventSystem TypeEventSystem { get; }
IEventBus EventBus { get; }
/// <summary>
/// 获取命令总线

View File

@ -5,7 +5,7 @@ namespace GFramework.Core.Abstractions.events;
/// <summary>
/// 事件接口,定义了事件注册的基本功能
/// </summary>
public interface IEasyEvent
public interface IEvent
{
/// <summary>
/// 注册事件处理函数

View File

@ -3,9 +3,9 @@
namespace GFramework.Core.Abstractions.events;
/// <summary>
/// 类型事件系统接口,定义基于类型的事件发送、注册和注销功能
/// 事件总线接口,提供事件的发送、注册和注销功能
/// </summary>
public interface ITypeEventSystem
public interface IEventBus
{
/// <summary>
/// 发送事件,自动创建事件实例

View File

@ -7,7 +7,7 @@ namespace GFramework.Core.Abstractions.property;
/// 只读可绑定属性接口,提供属性值的读取和变更监听功能
/// </summary>
/// <typeparam name="T">属性值的类型</typeparam>
public interface IReadonlyBindableProperty<out T> : IEasyEvent
public interface IReadonlyBindableProperty<out T> : IEvent
{
/// <summary>
/// 获取属性的当前值

View File

@ -61,13 +61,7 @@ public abstract class Architecture(
/// </value>
private IIocContainer Container => Services.Container;
/// <summary>
/// 获取类型事件系统
/// </summary>
/// <value>
/// 通过Services属性获取的IArchitectureServices中的TypeEventSystem属性
/// </value>
private ITypeEventSystem TypeEventSystem => Services.TypeEventSystem;
private IEventBus EventBus => Services.EventBus;
private ICommandBus CommandBus => Services.CommandBus;
@ -226,7 +220,7 @@ public abstract class Architecture(
// 进入销毁阶段并发送销毁开始事件
_logger.Info("Starting architecture destruction");
EnterPhase(ArchitecturePhase.Destroying);
TypeEventSystem.Send(new ArchitectureEvents.ArchitectureDestroyingEvent());
EventBus.Send(new ArchitectureEvents.ArchitectureDestroyingEvent());
// 销毁所有系统组件并清空系统列表
_logger.Info($"Destroying {_allSystems.Count} systems");
@ -240,7 +234,7 @@ public abstract class Architecture(
// 进入已销毁阶段并发送销毁完成事件
EnterPhase(ArchitecturePhase.Destroyed);
TypeEventSystem.Send(new ArchitectureEvents.ArchitectureDestroyedEvent());
EventBus.Send(new ArchitectureEvents.ArchitectureDestroyedEvent());
_logger.Info("Architecture destruction completed");
}
@ -262,7 +256,7 @@ public abstract class Architecture(
_logger.Error("Architecture initialization failed:", e);
EnterPhase(ArchitecturePhase.FailedInitialization);
// 发送初始化失败事件
TypeEventSystem.Send(new ArchitectureEvents.ArchitectureFailedInitializationEvent());
EventBus.Send(new ArchitectureEvents.ArchitectureFailedInitializationEvent());
}
}
@ -281,7 +275,7 @@ public abstract class Architecture(
_logger.Error("Architecture initialization failed:", e);
EnterPhase(ArchitecturePhase.FailedInitialization);
// 发送初始化失败事件
TypeEventSystem.Send(new ArchitectureEvents.ArchitectureFailedInitializationEvent());
EventBus.Send(new ArchitectureEvents.ArchitectureFailedInitializationEvent());
}
}
@ -322,7 +316,7 @@ public abstract class Architecture(
_logger = LoggerFactoryResolver.Provider.CreateLogger(GetType().Name);
Environment.Initialize();
// 初始化架构上下文(如果尚未初始化)
_context ??= new ArchitectureContext(Container, TypeEventSystem, CommandBus, QueryBus, Environment);
_context ??= new ArchitectureContext(Container, EventBus, CommandBus, QueryBus, Environment);
// 将当前架构类型与上下文绑定到游戏上下文
GameContext.Bind(GetType(), _context);
@ -387,7 +381,7 @@ public abstract class Architecture(
_mInitialized = true;
EnterPhase(ArchitecturePhase.Ready);
TypeEventSystem.Send(new ArchitectureEvents.ArchitectureLifecycleReadyEvent());
EventBus.Send(new ArchitectureEvents.ArchitectureLifecycleReadyEvent());
_logger.Info($"Architecture {GetType().Name} is ready - all components initialized");
}

View File

@ -15,7 +15,7 @@ namespace GFramework.Core.architecture;
/// </summary>
public class ArchitectureContext(
IIocContainer container,
ITypeEventSystem typeEventSystem,
IEventBus eventBus,
ICommandBus commandBus,
IQueryBus queryBus,
IEnvironment environment)
@ -25,10 +25,11 @@ public class ArchitectureContext(
private readonly IIocContainer _container = container ?? throw new ArgumentNullException(nameof(container));
private readonly IEnvironment _environment = environment ?? throw new ArgumentNullException(nameof(environment));
private readonly IQueryBus _queryBus = queryBus ?? throw new ArgumentNullException(nameof(queryBus));
private readonly ITypeEventSystem _typeEventSystem =
typeEventSystem ?? throw new ArgumentNullException(nameof(typeEventSystem));
private readonly IEventBus _eventBus =
eventBus ?? throw new ArgumentNullException(nameof(eventBus));
private readonly IQueryBus _queryBus = queryBus ?? throw new ArgumentNullException(nameof(queryBus));
#region Query Execution
@ -113,7 +114,7 @@ public class ArchitectureContext(
/// <typeparam name="TEvent">事件类型</typeparam>
public void SendEvent<TEvent>() where TEvent : new()
{
_typeEventSystem.Send<TEvent>();
_eventBus.Send<TEvent>();
}
/// <summary>
@ -124,7 +125,7 @@ public class ArchitectureContext(
public void SendEvent<TEvent>(TEvent e) where TEvent : class
{
ArgumentNullException.ThrowIfNull(e);
_typeEventSystem.Send(e);
_eventBus.Send(e);
}
/// <summary>
@ -135,7 +136,7 @@ public class ArchitectureContext(
/// <returns>事件注销接口</returns>
public IUnRegister RegisterEvent<TEvent>(Action<TEvent> handler)
{
return handler == null ? throw new ArgumentNullException(nameof(handler)) : _typeEventSystem.Register(handler);
return handler == null ? throw new ArgumentNullException(nameof(handler)) : _eventBus.Register(handler);
}
/// <summary>
@ -146,7 +147,7 @@ public class ArchitectureContext(
public void UnRegisterEvent<TEvent>(Action<TEvent> onEvent)
{
ArgumentNullException.ThrowIfNull(onEvent);
_typeEventSystem.UnRegister(onEvent);
_eventBus.UnRegister(onEvent);
}
/// <summary>

View File

@ -25,7 +25,7 @@ public class ArchitectureServices : IArchitectureServices
/// <summary>
/// 获取类型事件系统
/// </summary>
public ITypeEventSystem TypeEventSystem { get; } = new TypeEventSystem();
public IEventBus EventBus { get; } = new EventBus();
/// <summary>
/// 获取命令总线

View File

@ -4,10 +4,10 @@ namespace GFramework.Core.events;
/// <summary>
/// 泛型事件类,支持一个泛型参数 T 的事件注册、注销与触发。
/// 实现了 IEasyEvent 接口以提供统一的事件操作接口。
/// 实现了 IEvent 接口以提供统一的事件操作接口。
/// </summary>
/// <typeparam name="T">事件回调函数的第一个参数类型。</typeparam>
public class EasyEvent<T> : IEasyEvent
public class Event<T> : IEvent
{
/// <summary>
/// 存储已注册的事件处理委托。
@ -16,12 +16,12 @@ public class EasyEvent<T> : IEasyEvent
private Action<T>? _mOnEvent = _ => { };
/// <summary>
/// 显式实现 IEasyEvent 接口中的 Register 方法。
/// 显式实现 IEvent 接口中的 Register 方法。
/// 允许使用无参 Action 来订阅当前带参事件。
/// </summary>
/// <param name="onEvent">无参事件处理方法。</param>
/// <returns>IUnRegister 对象,用于稍后注销该事件监听器。</returns>
IUnRegister IEasyEvent.Register(Action onEvent)
IUnRegister IEvent.Register(Action onEvent)
{
return Register(Action);
@ -67,7 +67,7 @@ public class EasyEvent<T> : IEasyEvent
/// </summary>
/// <typeparam name="T">第一个参数类型。</typeparam>
/// <typeparam name="TK">第二个参数类型。</typeparam>
public class EasyEvent<T, TK> : IEasyEvent
public class Event<T, TK> : IEvent
{
/// <summary>
/// 存储已注册的双参数事件处理委托。
@ -76,12 +76,12 @@ public class EasyEvent<T, TK> : IEasyEvent
private Action<T, TK>? _mOnEvent = (_, _) => { };
/// <summary>
/// 显式实现 IEasyEvent 接口中的 Register 方法。
/// 显式实现 IEvent 接口中的 Register 方法。
/// 允许使用无参 Action 来订阅当前带参事件。
/// </summary>
/// <param name="onEvent">无参事件处理方法。</param>
/// <returns>IUnRegister 对象,用于稍后注销该事件监听器。</returns>
IUnRegister IEasyEvent.Register(Action onEvent)
IUnRegister IEvent.Register(Action onEvent)
{
return Register(Action);

View File

@ -16,14 +16,14 @@ public class EasyEvents
/// <summary>
/// 存储事件类型与事件实例映射关系的字典
/// </summary>
private readonly Dictionary<Type, IEasyEvent> _mTypeEvents = new();
private readonly Dictionary<Type, IEvent> _mTypeEvents = new();
/// <summary>
/// 获取指定类型的全局事件实例
/// </summary>
/// <typeparam name="T">事件类型必须实现IEasyEvent接口</typeparam>
/// <returns>指定类型的事件实例,如果未注册则返回默认值</returns>
public static T Get<T>() where T : IEasyEvent
public static T Get<T>() where T : IEvent
{
return MGlobalEvents.GetEvent<T>();
}
@ -32,7 +32,7 @@ public class EasyEvents
/// 注册指定类型的全局事件
/// </summary>
/// <typeparam name="T">事件类型必须实现IEasyEvent接口且具有无参构造函数</typeparam>
public static void Register<T>() where T : IEasyEvent, new()
public static void Register<T>() where T : IEvent, new()
{
MGlobalEvents.AddEvent<T>();
}
@ -41,7 +41,7 @@ public class EasyEvents
/// 添加指定类型的事件到事件字典中
/// </summary>
/// <typeparam name="T">事件类型必须实现IEasyEvent接口且具有无参构造函数</typeparam>
public void AddEvent<T>() where T : IEasyEvent, new()
public void AddEvent<T>() where T : IEvent, new()
{
_mTypeEvents.Add(typeof(T), new T());
}
@ -51,7 +51,7 @@ public class EasyEvents
/// </summary>
/// <typeparam name="T">事件类型必须实现IEasyEvent接口</typeparam>
/// <returns>指定类型的事件实例,如果不存在则返回默认值</returns>
public T GetEvent<T>() where T : IEasyEvent
public T GetEvent<T>() where T : IEvent
{
return _mTypeEvents.TryGetValue(typeof(T), out var e) ? (T)e : default!;
}
@ -61,7 +61,7 @@ public class EasyEvents
/// </summary>
/// <typeparam name="T">事件类型必须实现IEasyEvent接口且具有无参构造函数</typeparam>
/// <returns>指定类型的事件实例</returns>
public T GetOrAddEvent<T>() where T : IEasyEvent, new()
public T GetOrAddEvent<T>() where T : IEvent, new()
{
var eType = typeof(T);
// 尝试从字典中获取事件实例

View File

@ -5,7 +5,7 @@ namespace GFramework.Core.events;
/// <summary>
/// 类型事件系统,提供基于类型的事件发送、注册和注销功能
/// </summary>
public class TypeEventSystem : ITypeEventSystem
public class EventBus : IEventBus
{
private readonly EasyEvents _mEvents = new();
@ -16,7 +16,7 @@ public class TypeEventSystem : ITypeEventSystem
public void Send<T>() where T : new()
{
_mEvents
.GetOrAddEvent<EasyEvent<T>>()
.GetOrAddEvent<Event<T>>()
.Trigger(new T());
}
@ -28,7 +28,7 @@ public class TypeEventSystem : ITypeEventSystem
public void Send<T>(T e)
{
_mEvents
.GetOrAddEvent<EasyEvent<T>>()
.GetOrAddEvent<Event<T>>()
.Trigger(e);
}
@ -40,7 +40,7 @@ public class TypeEventSystem : ITypeEventSystem
/// <returns>反注册接口,用于注销事件监听</returns>
public IUnRegister Register<T>(Action<T> onEvent)
{
return _mEvents.GetOrAddEvent<EasyEvent<T>>().Register(onEvent);
return _mEvents.GetOrAddEvent<Event<T>>().Register(onEvent);
}
/// <summary>
@ -50,6 +50,6 @@ public class TypeEventSystem : ITypeEventSystem
/// <param name="onEvent">要注销的事件处理回调函数</param>
public void UnRegister<T>(Action<T> onEvent)
{
_mEvents.GetEvent<EasyEvent<T>>().UnRegister(onEvent);
_mEvents.GetEvent<Event<T>>().UnRegister(onEvent);
}
}

View File

@ -18,11 +18,11 @@ public class OrEvent : IUnRegisterList
/// <summary>
/// 将指定的事件与当前OrEvent进行或逻辑组合
/// </summary>
/// <param name="easyEvent">要组合的事件对象</param>
/// <param name="event">要组合的事件对象</param>
/// <returns>返回当前OrEvent实例支持链式调用</returns>
public OrEvent Or(IEasyEvent easyEvent)
public OrEvent Or(IEvent @event)
{
easyEvent.Register(Trigger).AddToUnregisterList(this);
@event.Register(Trigger).AddToUnregisterList(this);
return this;
}

View File

@ -14,7 +14,7 @@ public static class OrEventExtensions
/// <param name="self">当前的IEasyEvent事件实例</param>
/// <param name="e">要与当前事件进行或运算的另一个IEasyEvent事件实例</param>
/// <returns>返回一个新的OrEvent实例表示两个事件的或运算结果</returns>
public static OrEvent Or(this IEasyEvent self, IEasyEvent e)
public static OrEvent Or(this IEvent self, IEvent e)
{
return new OrEvent().Or(self).Or(e);
}

View File

@ -54,7 +54,7 @@ public class BindableProperty<T>(T defaultValue = default!) : IBindableProperty<
/// </summary>
/// <param name="onEvent">无参事件回调</param>
/// <returns>可用于取消注册的接口</returns>
IUnRegister IEasyEvent.Register(Action onEvent)
IUnRegister IEvent.Register(Action onEvent)
{
return Register(Action);