diff --git a/GFramework.Core.Abstractions/GFramework.Core.Abstractions.csproj b/GFramework.Core.Abstractions/GFramework.Core.Abstractions.csproj
index 580f5dd..3068015 100644
--- a/GFramework.Core.Abstractions/GFramework.Core.Abstractions.csproj
+++ b/GFramework.Core.Abstractions/GFramework.Core.Abstractions.csproj
@@ -26,9 +26,5 @@
runtime; build; native; contentfiles; analyzers
-
- all
- runtime; build; native; contentfiles; analyzers; buildtransitive
-
diff --git a/GFramework.Core.Abstractions/architecture/IArchitecture.cs b/GFramework.Core.Abstractions/architecture/IArchitecture.cs
index 022201d..e5575a9 100644
--- a/GFramework.Core.Abstractions/architecture/IArchitecture.cs
+++ b/GFramework.Core.Abstractions/architecture/IArchitecture.cs
@@ -1,6 +1,7 @@
using GFramework.Core.Abstractions.model;
using GFramework.Core.Abstractions.system;
using GFramework.Core.Abstractions.utility;
+using Microsoft.Extensions.DependencyInjection;
namespace GFramework.Core.Abstractions.architecture;
@@ -15,6 +16,14 @@ public interface IArchitecture : IAsyncInitializable
///
IArchitectureContext Context { get; }
+ ///
+ /// 获取或设置用于配置服务集合的委托
+ ///
+ ///
+ /// 一个可为空的委托,用于配置IServiceCollection实例
+ ///
+ Action? Configurator { get; }
+
///
/// 初始化方法,用于执行对象的初始化操作
///
diff --git a/GFramework.Core.Abstractions/architecture/IArchitectureConfiguration.cs b/GFramework.Core.Abstractions/architecture/IArchitectureConfiguration.cs
index 1050dbc..3504e47 100644
--- a/GFramework.Core.Abstractions/architecture/IArchitectureConfiguration.cs
+++ b/GFramework.Core.Abstractions/architecture/IArchitectureConfiguration.cs
@@ -1,5 +1,4 @@
using GFramework.Core.Abstractions.properties;
-using Mediator;
namespace GFramework.Core.Abstractions.architecture;
@@ -17,11 +16,4 @@ public interface IArchitectureConfiguration
/// 获取或设置架构选项,包含架构相关的配置参数
///
ArchitectureProperties ArchitectureProperties { get; set; }
-
- ///
- /// 获取或设置Mediator配置委托
- /// 用于自定义Mediator框架的配置选项
- ///
- /// 配置Mediator选项的委托函数,可为null
- Action? Configurator { get; set; }
}
\ No newline at end of file
diff --git a/GFramework.Core.Abstractions/ioc/IIocContainer.cs b/GFramework.Core.Abstractions/ioc/IIocContainer.cs
index 2d3c79d..1a4d69d 100644
--- a/GFramework.Core.Abstractions/ioc/IIocContainer.cs
+++ b/GFramework.Core.Abstractions/ioc/IIocContainer.cs
@@ -1,6 +1,5 @@
using GFramework.Core.Abstractions.rule;
using GFramework.Core.Abstractions.system;
-using Mediator;
using Microsoft.Extensions.DependencyInjection;
namespace GFramework.Core.Abstractions.ioc;
@@ -80,12 +79,12 @@ public interface IIocContainer : IContextAware
void RegisterMediatorBehavior()
where TBehavior : class;
+
///
- /// 注册并配置Mediator框架
- /// 提供自定义配置选项来调整Mediator的行为
+ /// 配置服务
///
- /// 可选的配置委托函数,用于自定义Mediator选项
- void RegisterMediator(Action? configurator = null);
+ /// 服务配置委托
+ void ExecuteServicesHook(Action? configurator = null);
#endregion
diff --git a/GFramework.Core.Tests/mediator/MediatorComprehensiveTests.cs b/GFramework.Core.Tests/mediator/MediatorComprehensiveTests.cs
index 7e498b0..83aaea2 100644
--- a/GFramework.Core.Tests/mediator/MediatorComprehensiveTests.cs
+++ b/GFramework.Core.Tests/mediator/MediatorComprehensiveTests.cs
@@ -1,11 +1,6 @@
using System.Reflection;
using System.Runtime.CompilerServices;
-using GFramework.Core.Abstractions.architecture;
-using GFramework.Core.Abstractions.enums;
using GFramework.Core.Abstractions.events;
-using GFramework.Core.Abstractions.model;
-using GFramework.Core.Abstractions.system;
-using GFramework.Core.Abstractions.utility;
using GFramework.Core.architecture;
using GFramework.Core.command;
using GFramework.Core.environment;
@@ -16,6 +11,7 @@ using GFramework.Core.query;
using Mediator;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
+
// ✅ Mediator 库的命名空间
// ✅ 使用 global using 或别名来区分
@@ -50,28 +46,10 @@ public class MediatorComprehensiveTests
_container.RegisterPlurality(_environment);
// ✅ 注册 Mediator
- _container.RegisterMediator(options => { options.ServiceLifetime = ServiceLifetime.Singleton; });
-
- // ✅ 手动注册 Mediator Handlers
- _container.Services.AddSingleton, TestRequestHandler>();
- _container.Services.AddSingleton, TestCommandHandler>();
- _container.Services.AddSingleton, TestCommandWithResultHandler>();
- _container.Services.AddSingleton, TestQueryHandler>();
- _container.Services.AddSingleton, TestNotificationHandler>();
- _container.Services.AddSingleton, TestStreamRequestHandler>();
-
- // 注册测试组件(Legacy)
- _testSystem = new TestSystem();
- _testModel = new TestModel();
- _testUtility = new TestUtility();
- _testCommand = new TestTraditionalCommand();
- _testQuery = new TestTraditionalQuery { Result = 999 };
-
- _container.RegisterPlurality(_testSystem);
- _container.RegisterPlurality(_testModel);
- _container.RegisterPlurality(_testUtility);
- _container.RegisterPlurality(_testCommand);
- _container.RegisterPlurality(_testQuery);
+ _container.ExecuteServicesHook(configurator =>
+ {
+ configurator.AddMediator(options => { options.ServiceLifetime = ServiceLifetime.Singleton; });
+ });
// ✅ Freeze 容器
_container.Freeze();
@@ -89,11 +67,6 @@ public class MediatorComprehensiveTests
_queryBus = null;
_asyncQueryBus = null;
_environment = null;
- _testSystem = null;
- _testModel = null;
- _testUtility = null;
- _testCommand = null;
- _testQuery = null;
}
private ArchitectureContext? _context;
@@ -103,11 +76,6 @@ public class MediatorComprehensiveTests
private QueryExecutor? _queryBus;
private AsyncQueryExecutor? _asyncQueryBus;
private DefaultEnvironment? _environment;
- private TestSystem? _testSystem;
- private TestModel? _testModel;
- private TestUtility? _testUtility;
- private TestTraditionalCommand? _testCommand;
- private TestTraditionalQuery? _testQuery;
[Test]
public async Task SendRequestAsync_Should_ReturnResult_When_Request_IsValid()
@@ -200,32 +168,6 @@ public class MediatorComprehensiveTests
Assert.That(TestNotificationHandler.LastReceivedMessage, Is.EqualTo("test event"));
}
- [Test]
- public async Task Mediator_And_CommandExecutor_Should_Coexist()
- {
- // 使用传统方式(Legacy)
- _context!.SendCommand(_testCommand!);
- Assert.That(_testCommand!.Executed, Is.True);
-
- // 使用 Mediator 方式
- var mediatorCommand = new TestCommandWithResult { ResultValue = 123 };
- var result = await _context.SendAsync(mediatorCommand);
- Assert.That(result, Is.EqualTo(123));
- }
-
- [Test]
- public async Task Mediator_And_QueryExecutor_Should_Coexist()
- {
- // 使用传统方式(Legacy)
- var traditionalResult = _context!.SendQuery(_testQuery!);
- Assert.That(traditionalResult, Is.EqualTo(999));
-
- // 使用 Mediator 方式
- var mediatorQuery = new TestQuery { QueryResult = "mediator result" };
- var mediatorResult = await _context.QueryAsync(mediatorQuery);
- Assert.That(mediatorResult, Is.EqualTo("mediator result"));
- }
-
[Test]
public void GetService_Should_Use_Cache()
{
@@ -237,19 +179,6 @@ public class MediatorComprehensiveTests
Assert.That(secondResult, Is.SameAs(firstResult));
}
- [Test]
- public void Architecture_Component_Getters_Should_Work()
- {
- var system = _context!.GetSystem();
- var model = _context.GetModel();
- var utility = _context.GetUtility();
- var environment = _context.GetEnvironment();
-
- Assert.That(system, Is.SameAs(_testSystem));
- Assert.That(model, Is.SameAs(_testModel));
- Assert.That(utility, Is.SameAs(_testUtility));
- Assert.That(environment, Is.SameAs(_environment));
- }
[Test]
public void Unregistered_Mediator_Should_Throw_InvalidOperationException()
@@ -363,81 +292,4 @@ public sealed class TestStreamRequestHandler : IStreamRequestHandler _context = context;
- public IArchitectureContext GetContext() => _context;
-
- public void Init()
- {
- }
-
- public void Destroy()
- {
- }
-
- public void OnArchitecturePhase(ArchitecturePhase phase)
- {
- }
-}
-
-public class TestModel : IModel
-{
- private IArchitectureContext _context = null!;
- public int Id { get; init; }
-
- public void SetContext(IArchitectureContext context) => _context = context;
- public IArchitectureContext GetContext() => _context;
-
- public void Init()
- {
- }
-
- public void OnArchitecturePhase(ArchitecturePhase phase)
- {
- }
-
- public void Destroy()
- {
- }
-}
-
-public class TestUtility : IUtility
-{
- private IArchitectureContext _context = null!;
- public int Id { get; init; }
-
- public void SetContext(IArchitectureContext context) => _context = context;
- public IArchitectureContext GetContext() => _context;
-}
-
-// ✅ 使用你框架的 ICommand
-public class TestTraditionalCommand : ICommand
-{
- private IArchitectureContext _context = null!;
- public bool Executed { get; private set; }
-
- public void Execute() => Executed = true;
- public void SetContext(IArchitectureContext context) => _context = context;
- public IArchitectureContext GetContext() => _context;
-}
-
-// ✅ 使用你框架的 IQuery
-public class TestTraditionalQuery : IQuery
-{
- private IArchitectureContext _context = null!;
- public int Result { get; init; }
-
- public int Do() => Result;
- public void SetContext(IArchitectureContext context) => _context = context;
- public IArchitectureContext GetContext() => _context;
-}
-
#endregion
\ No newline at end of file
diff --git a/GFramework.Core/architecture/Architecture.cs b/GFramework.Core/architecture/Architecture.cs
index cc06fa2..bf9ee73 100644
--- a/GFramework.Core/architecture/Architecture.cs
+++ b/GFramework.Core/architecture/Architecture.cs
@@ -636,8 +636,13 @@ public abstract class Architecture(
// 为服务设置上下文
Services.SetContext(_context);
- // 添加 Mediator
- Container.RegisterMediator(Configuration.Configurator);
+ if (Configurator is null)
+ {
+ _logger.Debug("Mediator-based cqrs will not take effect without the service setter configured!");
+ }
+
+ // 执行服务钩子
+ Container.ExecuteServicesHook(Configurator);
// === 用户 Init ===
_logger.Debug("Calling user Init()");
Init();
@@ -660,11 +665,23 @@ public abstract class Architecture(
///
/// 等待架构初始化完成(Ready 阶段)
+ /// 如果架构已经处于就绪状态,则立即返回已完成的任务;
+ /// 否则返回一个任务,该任务将在架构进入就绪状态时完成。
///
+ /// 表示等待操作的Task对象
public Task WaitUntilReadyAsync()
{
return IsReady ? Task.CompletedTask : _readyTcs.Task;
}
+ ///
+ /// 获取用于配置服务集合的委托
+ /// 默认实现返回null,子类可以重写此属性以提供自定义配置逻辑
+ ///
+ ///
+ /// 一个可为空的Action委托,用于配置IServiceCollection实例
+ ///
+ public virtual Action? Configurator => null;
+
#endregion
}
\ No newline at end of file
diff --git a/GFramework.Core/architecture/ArchitectureConfiguration.cs b/GFramework.Core/architecture/ArchitectureConfiguration.cs
index 1af6a00..f431296 100644
--- a/GFramework.Core/architecture/ArchitectureConfiguration.cs
+++ b/GFramework.Core/architecture/ArchitectureConfiguration.cs
@@ -2,8 +2,6 @@
using GFramework.Core.Abstractions.logging;
using GFramework.Core.Abstractions.properties;
using GFramework.Core.logging;
-using Mediator;
-using Microsoft.Extensions.DependencyInjection;
namespace GFramework.Core.architecture;
@@ -34,17 +32,4 @@ public sealed class ArchitectureConfiguration : IArchitectureConfiguration
AllowLateRegistration = false,
StrictPhaseValidation = true
};
-
- ///
- /// 获取或设置Mediator配置委托
- /// 用于自定义Mediator框架的配置选项
- ///
- /// 配置Mediator选项的委托函数,可为null
- public Action? Configurator { get; set; } = options =>
- {
- options.Namespace = "GFramework.Core.Mediator";
- options.ServiceLifetime = ServiceLifetime.Singleton;
- options.GenerateTypesAsInternal = true;
- options.NotificationPublisherType = typeof(ForeachAwaitPublisher);
- };
}
\ No newline at end of file
diff --git a/GFramework.Core/ioc/MicrosoftDiContainer.cs b/GFramework.Core/ioc/MicrosoftDiContainer.cs
index 9961f03..b1a0093 100644
--- a/GFramework.Core/ioc/MicrosoftDiContainer.cs
+++ b/GFramework.Core/ioc/MicrosoftDiContainer.cs
@@ -266,29 +266,12 @@ public class MicrosoftDiContainer(IServiceCollection? serviceCollection = null)
}
///
- /// 注册 Mediator(基于 Source Generator)
+ /// 配置服务
///
- /// 可选的配置委托
- public void RegisterMediator(Action? configurator = null)
+ /// 服务配置委托
+ public void ExecuteServicesHook(Action? configurator = null)
{
- _lock.EnterWriteLock();
- try
- {
- ThrowIfFrozen();
-
- // 添加 Mediator
- Services.AddMediator(options =>
- {
- // 用户自定义配置
- configurator?.Invoke(options);
- });
-
- _logger.Info("Mediator registered with Source Generator");
- }
- finally
- {
- _lock.ExitWriteLock();
- }
+ configurator?.Invoke(Services);
}
#endregion
diff --git a/GFramework.SourceGenerators/Generated/Mediator.SourceGenerator/Mediator.SourceGenerator.IncrementalMediatorGenerator/AssemblyReference.g.cs b/GFramework.SourceGenerators/Generated/Mediator.SourceGenerator/Mediator.SourceGenerator.IncrementalMediatorGenerator/AssemblyReference.g.cs
new file mode 100644
index 0000000..ab2d7cd
--- /dev/null
+++ b/GFramework.SourceGenerators/Generated/Mediator.SourceGenerator/Mediator.SourceGenerator.IncrementalMediatorGenerator/AssemblyReference.g.cs
@@ -0,0 +1,38 @@
+//
+// Generated by the Mediator source generator.
+//
+
+namespace Mediator
+{
+ ///
+ /// Represents an assembly reference.
+ /// This is used to specify the types or assemblies to scan for Mediator handlers.
+ ///
+ [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "3.0.0.0")]
+ public sealed class AssemblyReference
+ {
+ ///
+ /// The assembly reference.
+ ///
+ public global::System.Reflection.Assembly Assembly { get; }
+
+ private AssemblyReference(global::System.Reflection.Assembly assembly)
+ {
+ Assembly = assembly;
+ }
+
+ ///
+ /// Creates a new instance of from the specified type.
+ ///
+ /// The type
+ /// A new instance of
+ public static implicit operator AssemblyReference(global::System.Type type) => new AssemblyReference(type.Assembly);
+
+ ///
+ /// Creates a new instance of from the specified assembly.
+ ///
+ /// The assembly
+ /// A new instance of
+ public static implicit operator AssemblyReference(global::System.Reflection.Assembly assembly) => new AssemblyReference(assembly);
+ }
+}
diff --git a/GFramework.SourceGenerators/Generated/Mediator.SourceGenerator/Mediator.SourceGenerator.IncrementalMediatorGenerator/Mediator.g.cs b/GFramework.SourceGenerators/Generated/Mediator.SourceGenerator/Mediator.SourceGenerator.IncrementalMediatorGenerator/Mediator.g.cs
new file mode 100644
index 0000000..13eafaa
--- /dev/null
+++ b/GFramework.SourceGenerators/Generated/Mediator.SourceGenerator/Mediator.SourceGenerator.IncrementalMediatorGenerator/Mediator.g.cs
@@ -0,0 +1,1179 @@
+//
+// Generated by the Mediator source generator.
+//
+
+#pragma warning disable CS8019 // Unused usings
+#pragma warning disable CS8321 // Unused local function
+#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
+
+#nullable enable
+
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.DependencyInjection.Extensions;
+using System.Linq;
+
+namespace Microsoft.Extensions.DependencyInjection
+{
+ ///
+ /// DI extensions for Mediator.
+ ///
+ [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "3.0.0.0")]
+ [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
+ [global::System.Diagnostics.DebuggerStepThroughAttribute]
+ public static class MediatorDependencyInjectionExtensions
+ {
+ ///
+ /// Adds the Mediator implementation and handlers of your application.
+ ///
+ public static IServiceCollection AddMediator(this IServiceCollection services)
+ {
+ return AddMediator(services, null);
+ }
+
+ ///
+ /// Adds the Mediator implementation and handlers of your application, with specified options.
+ ///
+ public static IServiceCollection AddMediator(this IServiceCollection services, global::System.Action? options)
+ {
+ var opts = new global::Mediator.MediatorOptions();
+ if (options != null)
+ options(opts);
+
+ var configuredViaAttribute = false;
+ if (opts.ServiceLifetime != global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton && !configuredViaAttribute)
+ {
+ var errMsg = "Invalid configuration detected for Mediator. ";
+ errMsg += "Generated code for 'Singleton' lifetime, but got '" + opts.ServiceLifetime + "' lifetime from options. ";
+ errMsg += "This means that the source generator hasn't seen the 'AddMediator' method call during compilation. ";
+ errMsg += "Make sure that the 'AddMediator' method is called from the project that references the Mediator.SourceGenerator package.";
+ throw new global::System.Exception(errMsg);
+ }
+
+ services.Add(new global::Microsoft.Extensions.DependencyInjection.ServiceDescriptor(typeof(global::Mediator.Mediator), typeof(global::Mediator.Mediator), global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton));
+ services.TryAdd(new global::Microsoft.Extensions.DependencyInjection.ServiceDescriptor(typeof(global::Mediator.IMediator), sp => sp.GetRequiredService(), global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton));
+ services.TryAdd(new global::Microsoft.Extensions.DependencyInjection.ServiceDescriptor(typeof(global::Mediator.ISender), sp => sp.GetRequiredService(), global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton));
+ services.TryAdd(new global::Microsoft.Extensions.DependencyInjection.ServiceDescriptor(typeof(global::Mediator.IPublisher), sp => sp.GetRequiredService(), global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton));
+
+ // Register the notification publisher that was configured
+ services.Add(new global::Microsoft.Extensions.DependencyInjection.ServiceDescriptor(typeof(global::Mediator.ForeachAwaitPublisher), typeof(global::Mediator.ForeachAwaitPublisher), global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton));
+ services.TryAdd(new global::Microsoft.Extensions.DependencyInjection.ServiceDescriptor(typeof(global::Mediator.INotificationPublisher), sp => sp.GetRequiredService(), global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton));
+
+ // Register internal components
+ services.Add(new global::Microsoft.Extensions.DependencyInjection.ServiceDescriptor(typeof(global::Mediator.Internals.IContainerProbe), typeof(global::Mediator.Internals.ContainerProbe0), global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton));
+ services.Add(new global::Microsoft.Extensions.DependencyInjection.ServiceDescriptor(typeof(global::Mediator.Internals.IContainerProbe), typeof(global::Mediator.Internals.ContainerProbe1), global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton));
+ services.Add(new global::Microsoft.Extensions.DependencyInjection.ServiceDescriptor(typeof(global::Mediator.Internals.ContainerMetadata), typeof(global::Mediator.Internals.ContainerMetadata), global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton));
+
+ return services;
+
+ }
+ }
+}
+
+namespace Mediator.Internals
+{
+ [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "3.0.0.0")]
+ internal interface IMessageHandlerBase
+ {
+ global::System.Threading.Tasks.ValueTask