mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
refactor(architecture): 移除架构运行时并使用命令查询总线
- 移除了 IArchitectureRuntime 接口和 ArchitectureRuntime 类 - 在 ArchitectureContext 中添加了对 ICommandBus 和 IQueryBus 的依赖注入 - 修改 Architecture 类以使用 CommandBus 和 QueryBus 替代 Runtime - 更新 ArchitectureServices 以提供 CommandBus 和 QueryBus 服务 - 将组件初始化逻辑从 if-else 改为 switch 语句 - 更新 ContextAwareBase 以使用新的 GetFirstArchitectureContext 方法 - 添加了 CommandBus 和 QueryBus 的实现类 - 修复了 Godot 模块中系统获取的重复代码问题
This commit is contained in:
parent
f88f90f394
commit
0c9063a5fd
@ -15,11 +15,6 @@ public interface IArchitecture : IAsyncInitializable
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
IArchitectureContext Context { get; }
|
IArchitectureContext Context { get; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取架构运行时实例
|
|
||||||
/// </summary>
|
|
||||||
IArchitectureRuntime Runtime { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 初始化方法,用于执行对象的初始化操作
|
/// 初始化方法,用于执行对象的初始化操作
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@ -1,64 +0,0 @@
|
|||||||
using System;
|
|
||||||
using GFramework.Core.Abstractions.command;
|
|
||||||
using GFramework.Core.Abstractions.events;
|
|
||||||
using GFramework.Core.Abstractions.query;
|
|
||||||
|
|
||||||
namespace GFramework.Core.Abstractions.architecture;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 架构运行时接口,提供统一的命令、查询、事件操作入口
|
|
||||||
/// 负责委托 ArchitectureContext 的能力执行具体操作
|
|
||||||
/// </summary>
|
|
||||||
public interface IArchitectureRuntime
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 发送并执行指定的命令
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">命令类型,必须实现ICommand接口</typeparam>
|
|
||||||
/// <param name="command">要执行的命令实例</param>
|
|
||||||
void SendCommand<T>(T command) where T : ICommand;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 发送并执行带有返回值的命令
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TResult">命令执行结果的类型</typeparam>
|
|
||||||
/// <param name="command">要执行的命令实例</param>
|
|
||||||
/// <returns>命令执行的结果</returns>
|
|
||||||
TResult SendCommand<TResult>(ICommand<TResult> command);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 发送并执行查询操作
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TResult">查询结果的类型</typeparam>
|
|
||||||
/// <param name="query">要执行的查询实例</param>
|
|
||||||
/// <returns>查询的结果</returns>
|
|
||||||
TResult SendQuery<TResult>(IQuery<TResult> query);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 发送无参事件
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TEvent">事件类型,必须具有无参构造函数</typeparam>
|
|
||||||
void SendEvent<TEvent>() where TEvent : new();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 发送指定的事件实例
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TEvent">事件类型</typeparam>
|
|
||||||
/// <param name="e">要发送的事件实例</param>
|
|
||||||
void SendEvent<TEvent>(TEvent e) where TEvent : class;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 注册事件监听器
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TEvent">事件类型</typeparam>
|
|
||||||
/// <param name="onEvent">事件触发时的回调方法</param>
|
|
||||||
/// <returns>用于取消注册的句柄</returns>
|
|
||||||
IUnRegister RegisterEvent<TEvent>(Action<TEvent> onEvent);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 取消注册事件监听器
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TEvent">事件类型</typeparam>
|
|
||||||
/// <param name="onEvent">要取消注册的事件回调方法</param>
|
|
||||||
void UnRegisterEvent<TEvent>(Action<TEvent> onEvent);
|
|
||||||
}
|
|
||||||
@ -1,5 +1,7 @@
|
|||||||
using GFramework.Core.Abstractions.events;
|
using GFramework.Core.Abstractions.command;
|
||||||
|
using GFramework.Core.Abstractions.events;
|
||||||
using GFramework.Core.Abstractions.ioc;
|
using GFramework.Core.Abstractions.ioc;
|
||||||
|
using GFramework.Core.Abstractions.query;
|
||||||
using GFramework.Core.Abstractions.rule;
|
using GFramework.Core.Abstractions.rule;
|
||||||
|
|
||||||
namespace GFramework.Core.Abstractions.architecture;
|
namespace GFramework.Core.Abstractions.architecture;
|
||||||
@ -20,4 +22,14 @@ public interface IArchitectureServices : IContextAware
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>ITypeEventSystem类型的事件系统实例</returns>
|
/// <returns>ITypeEventSystem类型的事件系统实例</returns>
|
||||||
ITypeEventSystem TypeEventSystem { get; }
|
ITypeEventSystem TypeEventSystem { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取命令总线
|
||||||
|
/// </summary>
|
||||||
|
public ICommandBus CommandBus { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取查询总线
|
||||||
|
/// </summary>
|
||||||
|
public IQueryBus QueryBus { get; }
|
||||||
}
|
}
|
||||||
21
GFramework.Core.Abstractions/command/ICommandBus.cs
Normal file
21
GFramework.Core.Abstractions/command/ICommandBus.cs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
namespace GFramework.Core.Abstractions.command;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 定义命令总线接口,用于执行各种命令
|
||||||
|
/// </summary>
|
||||||
|
public interface ICommandBus
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 发送并执行一个命令
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="command">要执行的命令对象</param>
|
||||||
|
public void Send(ICommand command);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送并执行一个带返回值的命令
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResult">命令执行结果的类型</typeparam>
|
||||||
|
/// <param name="command">要执行的带返回值的命令对象</param>
|
||||||
|
/// <returns>命令执行的结果</returns>
|
||||||
|
public TResult Send<TResult>(ICommand<TResult> command);
|
||||||
|
}
|
||||||
15
GFramework.Core.Abstractions/query/IQueryBus.cs
Normal file
15
GFramework.Core.Abstractions/query/IQueryBus.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
namespace GFramework.Core.Abstractions.query;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 查询总线接口,用于发送和处理查询请求
|
||||||
|
/// </summary>
|
||||||
|
public interface IQueryBus
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 发送查询请求并返回结果
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResult">查询结果的类型</typeparam>
|
||||||
|
/// <param name="query">要发送的查询对象</param>
|
||||||
|
/// <returns>查询结果</returns>
|
||||||
|
public TResult Send<TResult>(IQuery<TResult> query);
|
||||||
|
}
|
||||||
@ -33,7 +33,6 @@ public class AsyncArchitectureTests : ArchitectureTestsBase<AsyncTestArchitectur
|
|||||||
await Architecture!.InitializeAsync();
|
await Architecture!.InitializeAsync();
|
||||||
|
|
||||||
Assert.That(Architecture.InitCalled, Is.True);
|
Assert.That(Architecture.InitCalled, Is.True);
|
||||||
Assert.That(Architecture.Runtime, Is.Not.Null);
|
|
||||||
Assert.That(Architecture.CurrentPhase, Is.EqualTo(ArchitecturePhase.Ready));
|
Assert.That(Architecture.CurrentPhase, Is.EqualTo(ArchitecturePhase.Ready));
|
||||||
|
|
||||||
var context = Architecture.Context;
|
var context = Architecture.Context;
|
||||||
|
|||||||
@ -30,7 +30,6 @@ public class SyncArchitectureTests : ArchitectureTestsBase<SyncTestArchitecture>
|
|||||||
// Assert
|
// Assert
|
||||||
Assert.That(Architecture.InitCalled, Is.True);
|
Assert.That(Architecture.InitCalled, Is.True);
|
||||||
|
|
||||||
Assert.That(Architecture.Runtime, Is.Not.Null);
|
|
||||||
|
|
||||||
var phase = Architecture.CurrentPhase;
|
var phase = Architecture.CurrentPhase;
|
||||||
Assert.That(phase, Is.EqualTo(ArchitecturePhase.Ready));
|
Assert.That(phase, Is.EqualTo(ArchitecturePhase.Ready));
|
||||||
|
|||||||
@ -1,9 +1,11 @@
|
|||||||
using GFramework.Core.Abstractions.architecture;
|
using GFramework.Core.Abstractions.architecture;
|
||||||
|
using GFramework.Core.Abstractions.command;
|
||||||
using GFramework.Core.Abstractions.enums;
|
using GFramework.Core.Abstractions.enums;
|
||||||
using GFramework.Core.Abstractions.events;
|
using GFramework.Core.Abstractions.events;
|
||||||
using GFramework.Core.Abstractions.ioc;
|
using GFramework.Core.Abstractions.ioc;
|
||||||
using GFramework.Core.Abstractions.logging;
|
using GFramework.Core.Abstractions.logging;
|
||||||
using GFramework.Core.Abstractions.model;
|
using GFramework.Core.Abstractions.model;
|
||||||
|
using GFramework.Core.Abstractions.query;
|
||||||
using GFramework.Core.Abstractions.system;
|
using GFramework.Core.Abstractions.system;
|
||||||
using GFramework.Core.Abstractions.utility;
|
using GFramework.Core.Abstractions.utility;
|
||||||
using GFramework.Core.events;
|
using GFramework.Core.events;
|
||||||
@ -55,13 +57,9 @@ public abstract class Architecture(
|
|||||||
/// </value>
|
/// </value>
|
||||||
private ITypeEventSystem TypeEventSystem => Services.TypeEventSystem;
|
private ITypeEventSystem TypeEventSystem => Services.TypeEventSystem;
|
||||||
|
|
||||||
/// <summary>
|
private ICommandBus CommandBus => Services.CommandBus;
|
||||||
/// 获取架构运行时实例
|
|
||||||
/// </summary>
|
private IQueryBus QueryBus => Services.QueryBus;
|
||||||
/// <value>
|
|
||||||
/// 统一的操作入口,负责命令、查询、事件的执行
|
|
||||||
/// </value>
|
|
||||||
public IArchitectureRuntime Runtime { get; private set; } = null!;
|
|
||||||
|
|
||||||
#region Module Management
|
#region Module Management
|
||||||
|
|
||||||
@ -282,14 +280,16 @@ public abstract class Architecture(
|
|||||||
{
|
{
|
||||||
await asyncInit.InitializeAsync();
|
await asyncInit.InitializeAsync();
|
||||||
}
|
}
|
||||||
else if (component is IModel model)
|
else
|
||||||
{
|
switch (component)
|
||||||
model.Init();
|
{
|
||||||
}
|
case IModel model:
|
||||||
else if (component is ISystem system)
|
model.Init();
|
||||||
{
|
break;
|
||||||
system.Init();
|
case ISystem system:
|
||||||
}
|
system.Init();
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -306,14 +306,10 @@ public abstract class Architecture(
|
|||||||
_logger = LoggerFactoryResolver.Provider.CreateLogger(GetType().Name);
|
_logger = LoggerFactoryResolver.Provider.CreateLogger(GetType().Name);
|
||||||
|
|
||||||
// 初始化架构上下文(如果尚未初始化)
|
// 初始化架构上下文(如果尚未初始化)
|
||||||
_context ??= new ArchitectureContext(Container, TypeEventSystem);
|
_context ??= new ArchitectureContext(Container, TypeEventSystem, CommandBus, QueryBus);
|
||||||
// 将当前架构类型与上下文绑定到游戏上下文
|
// 将当前架构类型与上下文绑定到游戏上下文
|
||||||
GameContext.Bind(GetType(), _context);
|
GameContext.Bind(GetType(), _context);
|
||||||
|
|
||||||
// 创建架构运行时实例
|
|
||||||
Runtime = new ArchitectureRuntime(_context);
|
|
||||||
// 设置上下文中的运行时引用
|
|
||||||
((ArchitectureContext)_context).Runtime = Runtime;
|
|
||||||
// 为服务设置上下文
|
// 为服务设置上下文
|
||||||
Services.SetContext(_context);
|
Services.SetContext(_context);
|
||||||
|
|
||||||
|
|||||||
@ -14,16 +14,18 @@ namespace GFramework.Core.architecture;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class ArchitectureContext(
|
public class ArchitectureContext(
|
||||||
IIocContainer container,
|
IIocContainer container,
|
||||||
ITypeEventSystem typeEventSystem)
|
ITypeEventSystem typeEventSystem,
|
||||||
|
ICommandBus commandBus,
|
||||||
|
IQueryBus queryBus)
|
||||||
: IArchitectureContext
|
: IArchitectureContext
|
||||||
{
|
{
|
||||||
|
private readonly ICommandBus _commandBus = commandBus ?? throw new ArgumentNullException(nameof(commandBus));
|
||||||
private readonly IIocContainer _container = container ?? throw new ArgumentNullException(nameof(container));
|
private readonly IIocContainer _container = container ?? throw new ArgumentNullException(nameof(container));
|
||||||
|
private readonly IQueryBus _queryBus = queryBus ?? throw new ArgumentNullException(nameof(queryBus));
|
||||||
|
|
||||||
private readonly ITypeEventSystem _typeEventSystem =
|
private readonly ITypeEventSystem _typeEventSystem =
|
||||||
typeEventSystem ?? throw new ArgumentNullException(nameof(typeEventSystem));
|
typeEventSystem ?? throw new ArgumentNullException(nameof(typeEventSystem));
|
||||||
|
|
||||||
internal IArchitectureRuntime Runtime { get; set; } = null!;
|
|
||||||
|
|
||||||
#region Query Execution
|
#region Query Execution
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -34,7 +36,7 @@ public class ArchitectureContext(
|
|||||||
/// <returns>查询结果</returns>
|
/// <returns>查询结果</returns>
|
||||||
public TResult SendQuery<TResult>(IQuery<TResult> query)
|
public TResult SendQuery<TResult>(IQuery<TResult> query)
|
||||||
{
|
{
|
||||||
return query == null ? throw new ArgumentNullException(nameof(query)) : Runtime.SendQuery(query);
|
return query == null ? throw new ArgumentNullException(nameof(query)) : _queryBus.Send(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -76,17 +78,17 @@ public class ArchitectureContext(
|
|||||||
#region Command Execution
|
#region Command Execution
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发送一个无返回结果的命令
|
/// 发送一个命令请求
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="command">要发送的命令</param>
|
/// <param name="command">要发送的命令</param>
|
||||||
public void SendCommand(ICommand command)
|
public void SendCommand(ICommand command)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(command);
|
ArgumentNullException.ThrowIfNull(command);
|
||||||
Runtime.SendCommand(command);
|
_commandBus.Send(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发送一个带返回值的命令
|
/// 发送一个带返回值的命令请求
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TResult">命令执行结果类型</typeparam>
|
/// <typeparam name="TResult">命令执行结果类型</typeparam>
|
||||||
/// <param name="command">要发送的命令</param>
|
/// <param name="command">要发送的命令</param>
|
||||||
@ -94,7 +96,7 @@ public class ArchitectureContext(
|
|||||||
public TResult SendCommand<TResult>(ICommand<TResult> command)
|
public TResult SendCommand<TResult>(ICommand<TResult> command)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(command);
|
ArgumentNullException.ThrowIfNull(command);
|
||||||
return Runtime.SendCommand(command);
|
return _commandBus.Send(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -117,7 +119,7 @@ public class ArchitectureContext(
|
|||||||
/// <param name="e">事件参数</param>
|
/// <param name="e">事件参数</param>
|
||||||
public void SendEvent<TEvent>(TEvent e) where TEvent : class
|
public void SendEvent<TEvent>(TEvent e) where TEvent : class
|
||||||
{
|
{
|
||||||
if (e == null) throw new ArgumentNullException(nameof(e));
|
ArgumentNullException.ThrowIfNull(e);
|
||||||
_typeEventSystem.Send(e);
|
_typeEventSystem.Send(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,98 +0,0 @@
|
|||||||
using GFramework.Core.Abstractions.architecture;
|
|
||||||
using GFramework.Core.Abstractions.command;
|
|
||||||
using GFramework.Core.Abstractions.events;
|
|
||||||
using GFramework.Core.Abstractions.query;
|
|
||||||
|
|
||||||
namespace GFramework.Core.architecture;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 架构运行时默认实现,委托 ArchitectureContext 执行具体操作
|
|
||||||
/// </summary>
|
|
||||||
public class ArchitectureRuntime(IArchitectureContext context) : IArchitectureRuntime
|
|
||||||
{
|
|
||||||
private readonly IArchitectureContext _context = context ?? throw new ArgumentNullException(nameof(context));
|
|
||||||
|
|
||||||
#region Query Execution
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 发起一次查询请求并获得其结果
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TResult">查询结果的数据类型</typeparam>
|
|
||||||
/// <param name="query">要发起的查询对象</param>
|
|
||||||
/// <returns>查询得到的结果数据</returns>
|
|
||||||
public TResult SendQuery<TResult>(IQuery<TResult> query)
|
|
||||||
{
|
|
||||||
return _context.SendQuery(query);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Command Execution
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 发送一个无返回结果的命令请求
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TCommand">命令的具体类型</typeparam>
|
|
||||||
/// <param name="command">要发送的命令对象</param>
|
|
||||||
public void SendCommand<TCommand>(TCommand command) where TCommand : ICommand
|
|
||||||
{
|
|
||||||
_context.SendCommand(command);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 发送一个带返回结果的命令请求
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TResult">命令执行后的返回值类型</typeparam>
|
|
||||||
/// <param name="command">要发送的命令对象</param>
|
|
||||||
/// <returns>命令执行的结果</returns>
|
|
||||||
public TResult SendCommand<TResult>(ICommand<TResult> command)
|
|
||||||
{
|
|
||||||
return _context.SendCommand(command);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Event Management
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 发布一个默认构造的新事件对象
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TEvent">事件类型</typeparam>
|
|
||||||
public void SendEvent<TEvent>() where TEvent : new()
|
|
||||||
{
|
|
||||||
_context.SendEvent<TEvent>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 发布一个具体的事件对象
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TEvent">事件类型</typeparam>
|
|
||||||
/// <param name="e">要发布的事件实例</param>
|
|
||||||
public void SendEvent<TEvent>(TEvent e) where TEvent : class
|
|
||||||
{
|
|
||||||
_context.SendEvent(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 订阅某个特定类型的事件
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TEvent">事件类型</typeparam>
|
|
||||||
/// <param name="onEvent">当事件发生时触发的动作</param>
|
|
||||||
/// <returns>可用于取消订阅的对象</returns>
|
|
||||||
public IUnRegister RegisterEvent<TEvent>(Action<TEvent> onEvent)
|
|
||||||
{
|
|
||||||
return _context.RegisterEvent(onEvent);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 取消对某类型事件的监听
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TEvent">事件类型</typeparam>
|
|
||||||
/// <param name="onEvent">之前绑定的事件处理器</param>
|
|
||||||
public void UnRegisterEvent<TEvent>(Action<TEvent> onEvent)
|
|
||||||
{
|
|
||||||
_context.UnRegisterEvent(onEvent);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
|
||||||
@ -1,23 +1,56 @@
|
|||||||
using GFramework.Core.Abstractions.architecture;
|
using GFramework.Core.Abstractions.architecture;
|
||||||
|
using GFramework.Core.Abstractions.command;
|
||||||
using GFramework.Core.Abstractions.events;
|
using GFramework.Core.Abstractions.events;
|
||||||
using GFramework.Core.Abstractions.ioc;
|
using GFramework.Core.Abstractions.ioc;
|
||||||
|
using GFramework.Core.Abstractions.query;
|
||||||
|
using GFramework.Core.command;
|
||||||
using GFramework.Core.events;
|
using GFramework.Core.events;
|
||||||
using GFramework.Core.ioc;
|
using GFramework.Core.ioc;
|
||||||
|
using GFramework.Core.query;
|
||||||
|
|
||||||
namespace GFramework.Core.architecture;
|
namespace GFramework.Core.architecture;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 架构服务类,提供依赖注入容器、事件系统、命令总线和查询总线等核心服务
|
||||||
|
/// </summary>
|
||||||
public class ArchitectureServices : IArchitectureServices
|
public class ArchitectureServices : IArchitectureServices
|
||||||
{
|
{
|
||||||
private IArchitectureContext _context = null!;
|
private IArchitectureContext _context = null!;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取依赖注入容器
|
||||||
|
/// </summary>
|
||||||
public IIocContainer Container { get; } = new IocContainer();
|
public IIocContainer Container { get; } = new IocContainer();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取类型事件系统
|
||||||
|
/// </summary>
|
||||||
public ITypeEventSystem TypeEventSystem { get; } = new TypeEventSystem();
|
public ITypeEventSystem TypeEventSystem { get; } = new TypeEventSystem();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取命令总线
|
||||||
|
/// </summary>
|
||||||
|
public ICommandBus CommandBus { get; } = new CommandBus();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取查询总线
|
||||||
|
/// </summary>
|
||||||
|
public IQueryBus QueryBus { get; } = new QueryBus();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设置架构上下文
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context">架构上下文对象</param>
|
||||||
public void SetContext(IArchitectureContext context)
|
public void SetContext(IArchitectureContext context)
|
||||||
{
|
{
|
||||||
_context = context;
|
_context = context;
|
||||||
Container.SetContext(context);
|
Container.SetContext(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取架构上下文
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>架构上下文对象</returns>
|
||||||
public IArchitectureContext GetContext()
|
public IArchitectureContext GetContext()
|
||||||
{
|
{
|
||||||
return _context;
|
return _context;
|
||||||
|
|||||||
@ -38,7 +38,7 @@ public static class GameContext
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>返回字典中的第一个架构上下文实例</returns>
|
/// <returns>返回字典中的第一个架构上下文实例</returns>
|
||||||
/// <exception cref="InvalidOperationException">当字典为空时抛出</exception>
|
/// <exception cref="InvalidOperationException">当字典为空时抛出</exception>
|
||||||
public static IArchitectureContext GetFirstArchitecture()
|
public static IArchitectureContext GetFirstArchitectureContext()
|
||||||
{
|
{
|
||||||
return ArchitectureDictionary.Values.First();
|
return ArchitectureDictionary.Values.First();
|
||||||
}
|
}
|
||||||
|
|||||||
35
GFramework.Core/command/CommandBus.cs
Normal file
35
GFramework.Core/command/CommandBus.cs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
using GFramework.Core.Abstractions.command;
|
||||||
|
|
||||||
|
namespace GFramework.Core.command;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 命令总线实现类,用于发送和执行命令
|
||||||
|
/// </summary>
|
||||||
|
public sealed class CommandBus : ICommandBus
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 发送并执行无返回值的命令
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="command">要执行的命令对象,不能为空</param>
|
||||||
|
/// <exception cref="ArgumentNullException">当command参数为null时抛出</exception>
|
||||||
|
public void Send(ICommand command)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(command);
|
||||||
|
|
||||||
|
command.Execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送并执行有返回值的命令
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResult">命令执行结果的类型</typeparam>
|
||||||
|
/// <param name="command">要执行的命令对象,不能为空</param>
|
||||||
|
/// <returns>命令执行的结果</returns>
|
||||||
|
/// <exception cref="ArgumentNullException">当command参数为null时抛出</exception>
|
||||||
|
public TResult Send<TResult>(ICommand<TResult> command)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(command);
|
||||||
|
|
||||||
|
return command.Execute();
|
||||||
|
}
|
||||||
|
}
|
||||||
23
GFramework.Core/query/QueryBus.cs
Normal file
23
GFramework.Core/query/QueryBus.cs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
using GFramework.Core.Abstractions.query;
|
||||||
|
|
||||||
|
namespace GFramework.Core.query;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 查询总线实现,负责执行查询并返回结果
|
||||||
|
/// </summary>
|
||||||
|
public sealed class QueryBus : IQueryBus
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 执行指定的查询并返回结果
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResult">查询结果的类型</typeparam>
|
||||||
|
/// <param name="query">要执行的查询对象</param>
|
||||||
|
/// <returns>查询执行结果</returns>
|
||||||
|
public TResult Send<TResult>(IQuery<TResult> query)
|
||||||
|
{
|
||||||
|
// 验证查询参数不为null
|
||||||
|
ArgumentNullException.ThrowIfNull(query);
|
||||||
|
|
||||||
|
return query.Do();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,5 +1,6 @@
|
|||||||
using GFramework.Core.Abstractions.architecture;
|
using GFramework.Core.Abstractions.architecture;
|
||||||
using GFramework.Core.Abstractions.rule;
|
using GFramework.Core.Abstractions.rule;
|
||||||
|
using GFramework.Core.architecture;
|
||||||
|
|
||||||
namespace GFramework.Core.rule;
|
namespace GFramework.Core.rule;
|
||||||
|
|
||||||
@ -11,7 +12,7 @@ public abstract class ContextAwareBase : IContextAware
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取当前实例的架构上下文
|
/// 获取当前实例的架构上下文
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected IArchitectureContext Context { get; set; } = null!;
|
protected IArchitectureContext? Context { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 设置架构上下文的实现方法,由框架调用
|
/// 设置架构上下文的实现方法,由框架调用
|
||||||
@ -29,6 +30,7 @@ public abstract class ContextAwareBase : IContextAware
|
|||||||
/// <returns>当前架构上下文对象</returns>
|
/// <returns>当前架构上下文对象</returns>
|
||||||
IArchitectureContext IContextAware.GetContext()
|
IArchitectureContext IContextAware.GetContext()
|
||||||
{
|
{
|
||||||
|
Context ??= GameContext.GetFirstArchitectureContext();
|
||||||
return Context;
|
return Context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
using GFramework.Core.Abstractions.enums;
|
using GFramework.Core.Abstractions.enums;
|
||||||
|
using GFramework.Core.extensions;
|
||||||
using GFramework.Core.system;
|
using GFramework.Core.system;
|
||||||
using GFramework.Game.Abstractions.assets;
|
using GFramework.Game.Abstractions.assets;
|
||||||
using GFramework.Godot.Abstractions.assets;
|
using GFramework.Godot.Abstractions.assets;
|
||||||
@ -63,8 +64,8 @@ public abstract class AbstractResourceFactorySystem : AbstractSystem, IResourceF
|
|||||||
protected override void OnInit()
|
protected override void OnInit()
|
||||||
{
|
{
|
||||||
_registry = new ResourceFactory.Registry();
|
_registry = new ResourceFactory.Registry();
|
||||||
_resourceLoadSystem = Context.GetSystem<IResourceLoadSystem>();
|
_assetCatalogSystem = this.GetSystem<IAssetCatalogSystem>();
|
||||||
_assetCatalogSystem = Context.GetSystem<IAssetCatalogSystem>();
|
_assetCatalogSystem = this.GetSystem<IAssetCatalogSystem>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
using GFramework.Core.extensions;
|
||||||
using GFramework.Core.system;
|
using GFramework.Core.system;
|
||||||
using GFramework.Game.Abstractions.assets;
|
using GFramework.Game.Abstractions.assets;
|
||||||
using GFramework.Godot.Abstractions.assets;
|
using GFramework.Godot.Abstractions.assets;
|
||||||
@ -431,9 +432,9 @@ public abstract class AbstractAudioManagerSystem : AbstractSystem, IAudioManager
|
|||||||
protected override void OnInit()
|
protected override void OnInit()
|
||||||
{
|
{
|
||||||
// 获取依赖的系统
|
// 获取依赖的系统
|
||||||
ResourceLoadSystem = Context.GetSystem<IResourceLoadSystem>();
|
ResourceLoadSystem = this.GetSystem<IResourceLoadSystem>();
|
||||||
AssetCatalogSystem = Context.GetSystem<IAssetCatalogSystem>();
|
AssetCatalogSystem = this.GetSystem<IAssetCatalogSystem>();
|
||||||
ResourceFactorySystem = Context.GetSystem<IResourceFactorySystem>();
|
ResourceFactorySystem = this.GetSystem<IResourceFactorySystem>();
|
||||||
|
|
||||||
// 初始化背景音乐播放器
|
// 初始化背景音乐播放器
|
||||||
MusicPlayer = new AudioStreamPlayer();
|
MusicPlayer = new AudioStreamPlayer();
|
||||||
|
|||||||
@ -64,7 +64,7 @@ public class ContextAwareGeneratorSnapshotTests
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>返回字典中的第一个架构上下文实例</returns>
|
/// <returns>返回字典中的第一个架构上下文实例</returns>
|
||||||
/// <exception cref="InvalidOperationException">当字典为空时抛出</exception>
|
/// <exception cref="InvalidOperationException">当字典为空时抛出</exception>
|
||||||
public static IArchitectureContext GetFirstArchitecture()
|
public static IArchitectureContext GetFirstArchitectureContext()
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -140,7 +140,7 @@ public sealed class ContextAwareGenerator : MetadataAttributeClassGeneratorBase
|
|||||||
sb.AppendLine(" if (_context == null)");
|
sb.AppendLine(" if (_context == null)");
|
||||||
sb.AppendLine(" {");
|
sb.AppendLine(" {");
|
||||||
sb.AppendLine(
|
sb.AppendLine(
|
||||||
" _context = global::GFramework.Core.architecture.GameContext.GetFirstArchitecture();");
|
" _context = global::GFramework.Core.architecture.GameContext.GetFirstArchitectureContext();");
|
||||||
sb.AppendLine(" }");
|
sb.AppendLine(" }");
|
||||||
sb.AppendLine();
|
sb.AppendLine();
|
||||||
sb.AppendLine(" return _context;");
|
sb.AppendLine(" return _context;");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user