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 Handle( + object request, + global::System.Threading.CancellationToken cancellationToken + ); + } + [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "3.0.0.0")] + internal interface INotificationHandlerBase + { + global::System.Threading.Tasks.ValueTask Handle( + object notification, + global::System.Threading.CancellationToken cancellationToken + ); + } + [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "3.0.0.0")] + internal interface IStreamMessageHandlerBase + { + global::System.Collections.Generic.IAsyncEnumerable Handle( + object request, + global::System.Threading.CancellationToken cancellationToken + ); + } + + [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "3.0.0.0")] + internal interface IRequestHandlerBase : IMessageHandlerBase + { + global::System.Threading.Tasks.ValueTask Handle( + global::Mediator.IRequest request, + global::System.Threading.CancellationToken cancellationToken + ); + } + [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "3.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + [global::System.Diagnostics.DebuggerStepThroughAttribute] + internal sealed class RequestHandlerWrapper : IRequestHandlerBase + where TRequest : global::Mediator.IRequest + { + private global::Mediator.MessageHandlerDelegate _rootHandler = null!; + + public RequestHandlerWrapper Init( + global::Mediator.Internals.ContainerMetadata containerMetadata, + global::System.IServiceProvider sp + ) + { + var concreteHandler = sp.GetRequiredService>(); + var pipelineBehaviours = sp.GetServices>(); + var handler = (global::Mediator.MessageHandlerDelegate)concreteHandler.Handle; + + global::Mediator.IPipelineBehavior[] pipelineBehavioursArray; + if (containerMetadata.ServicesUnderlyingTypeIsArray) + { + global::System.Diagnostics.Debug.Assert( + pipelineBehaviours is global::Mediator.IPipelineBehavior[] + ); + pipelineBehavioursArray = global::System.Runtime.CompilerServices.Unsafe.As[]>( + pipelineBehaviours + ); + } + else + { + global::System.Diagnostics.Debug.Assert( + pipelineBehaviours is not global::Mediator.IPipelineBehavior[] + ); + pipelineBehavioursArray = pipelineBehaviours.ToArray(); + } + + for (int i = pipelineBehavioursArray.Length - 1; i >= 0; i--) + { + var pipeline = pipelineBehavioursArray[i]; + var handlerCopy = handler; + var pipelineCopy = pipeline; + handler = (TRequest message, System.Threading.CancellationToken cancellationToken) => pipelineCopy.Handle(message, handlerCopy, cancellationToken); + } + + _rootHandler = handler; + return this; + } + + public global::System.Threading.Tasks.ValueTask Handle( + TRequest request, + global::System.Threading.CancellationToken cancellationToken + ) + { + var handler = _rootHandler; + return handler(request, cancellationToken); + } + + public global::System.Threading.Tasks.ValueTask Handle( + global::Mediator.IRequest request, + global::System.Threading.CancellationToken cancellationToken + ) + { + return Handle((TRequest)request, cancellationToken); + } + + public async global::System.Threading.Tasks.ValueTask Handle( + object request, + global::System.Threading.CancellationToken cancellationToken + ) + { + return await Handle((TRequest)request, cancellationToken); + } + } + [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "3.0.0.0")] + internal interface IStreamRequestHandlerBase : IStreamMessageHandlerBase + { + global::System.Collections.Generic.IAsyncEnumerable Handle( + global::Mediator.IStreamRequest request, + global::System.Threading.CancellationToken cancellationToken + ); + } + [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "3.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + [global::System.Diagnostics.DebuggerStepThroughAttribute] + internal sealed class StreamRequestHandlerWrapper : IStreamRequestHandlerBase + where TRequest : global::Mediator.IStreamRequest + { + private global::Mediator.StreamHandlerDelegate _rootHandler = null!; + + public StreamRequestHandlerWrapper Init( + global::Mediator.Internals.ContainerMetadata containerMetadata, + global::System.IServiceProvider sp + ) + { + var concreteHandler = sp.GetRequiredService>(); + var pipelineBehaviours = sp.GetServices>(); + var handler = (global::Mediator.StreamHandlerDelegate)concreteHandler.Handle; + + global::Mediator.IStreamPipelineBehavior[] pipelineBehavioursArray; + if (containerMetadata.ServicesUnderlyingTypeIsArray) + { + global::System.Diagnostics.Debug.Assert( + pipelineBehaviours is global::Mediator.IStreamPipelineBehavior[] + ); + pipelineBehavioursArray = global::System.Runtime.CompilerServices.Unsafe.As[]>( + pipelineBehaviours + ); + } + else + { + global::System.Diagnostics.Debug.Assert( + pipelineBehaviours is not global::Mediator.IStreamPipelineBehavior[] + ); + pipelineBehavioursArray = pipelineBehaviours.ToArray(); + } + + for (int i = pipelineBehavioursArray.Length - 1; i >= 0; i--) + { + var pipeline = pipelineBehavioursArray[i]; + var handlerCopy = handler; + var pipelineCopy = pipeline; + handler = (TRequest message, System.Threading.CancellationToken cancellationToken) => pipelineCopy.Handle(message, handlerCopy, cancellationToken); + } + + _rootHandler = handler; + return this; + } + + public global::System.Collections.Generic.IAsyncEnumerable Handle( + TRequest request, + global::System.Threading.CancellationToken cancellationToken + ) + { + var handler = _rootHandler; + return handler(request, cancellationToken); + } + + public global::System.Collections.Generic.IAsyncEnumerable Handle( + global::Mediator.IStreamRequest request, + global::System.Threading.CancellationToken cancellationToken + ) + { + return Handle((TRequest)request, cancellationToken); + } + + public async global::System.Collections.Generic.IAsyncEnumerable Handle( + object request, + [global::System.Runtime.CompilerServices.EnumeratorCancellation] global::System.Threading.CancellationToken cancellationToken + ) + { + await foreach (var el in Handle((TRequest)request, cancellationToken)) + yield return el; + } + } + [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "3.0.0.0")] + internal interface ICommandHandlerBase : IMessageHandlerBase + { + global::System.Threading.Tasks.ValueTask Handle( + global::Mediator.ICommand request, + global::System.Threading.CancellationToken cancellationToken + ); + } + [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "3.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + [global::System.Diagnostics.DebuggerStepThroughAttribute] + internal sealed class CommandHandlerWrapper : ICommandHandlerBase + where TRequest : global::Mediator.ICommand + { + private global::Mediator.MessageHandlerDelegate _rootHandler = null!; + + public CommandHandlerWrapper Init( + global::Mediator.Internals.ContainerMetadata containerMetadata, + global::System.IServiceProvider sp + ) + { + var concreteHandler = sp.GetRequiredService>(); + var pipelineBehaviours = sp.GetServices>(); + var handler = (global::Mediator.MessageHandlerDelegate)concreteHandler.Handle; + + global::Mediator.IPipelineBehavior[] pipelineBehavioursArray; + if (containerMetadata.ServicesUnderlyingTypeIsArray) + { + global::System.Diagnostics.Debug.Assert( + pipelineBehaviours is global::Mediator.IPipelineBehavior[] + ); + pipelineBehavioursArray = global::System.Runtime.CompilerServices.Unsafe.As[]>( + pipelineBehaviours + ); + } + else + { + global::System.Diagnostics.Debug.Assert( + pipelineBehaviours is not global::Mediator.IPipelineBehavior[] + ); + pipelineBehavioursArray = pipelineBehaviours.ToArray(); + } + + for (int i = pipelineBehavioursArray.Length - 1; i >= 0; i--) + { + var pipeline = pipelineBehavioursArray[i]; + var handlerCopy = handler; + var pipelineCopy = pipeline; + handler = (TRequest message, System.Threading.CancellationToken cancellationToken) => pipelineCopy.Handle(message, handlerCopy, cancellationToken); + } + + _rootHandler = handler; + return this; + } + + public global::System.Threading.Tasks.ValueTask Handle( + TRequest request, + global::System.Threading.CancellationToken cancellationToken + ) + { + var handler = _rootHandler; + return handler(request, cancellationToken); + } + + public global::System.Threading.Tasks.ValueTask Handle( + global::Mediator.ICommand request, + global::System.Threading.CancellationToken cancellationToken + ) + { + return Handle((TRequest)request, cancellationToken); + } + + public async global::System.Threading.Tasks.ValueTask Handle( + object request, + global::System.Threading.CancellationToken cancellationToken + ) + { + return await Handle((TRequest)request, cancellationToken); + } + } + [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "3.0.0.0")] + internal interface IStreamCommandHandlerBase : IStreamMessageHandlerBase + { + global::System.Collections.Generic.IAsyncEnumerable Handle( + global::Mediator.IStreamCommand request, + global::System.Threading.CancellationToken cancellationToken + ); + } + [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "3.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + [global::System.Diagnostics.DebuggerStepThroughAttribute] + internal sealed class StreamCommandHandlerWrapper : IStreamCommandHandlerBase + where TRequest : global::Mediator.IStreamCommand + { + private global::Mediator.StreamHandlerDelegate _rootHandler = null!; + + public StreamCommandHandlerWrapper Init( + global::Mediator.Internals.ContainerMetadata containerMetadata, + global::System.IServiceProvider sp + ) + { + var concreteHandler = sp.GetRequiredService>(); + var pipelineBehaviours = sp.GetServices>(); + var handler = (global::Mediator.StreamHandlerDelegate)concreteHandler.Handle; + + global::Mediator.IStreamPipelineBehavior[] pipelineBehavioursArray; + if (containerMetadata.ServicesUnderlyingTypeIsArray) + { + global::System.Diagnostics.Debug.Assert( + pipelineBehaviours is global::Mediator.IStreamPipelineBehavior[] + ); + pipelineBehavioursArray = global::System.Runtime.CompilerServices.Unsafe.As[]>( + pipelineBehaviours + ); + } + else + { + global::System.Diagnostics.Debug.Assert( + pipelineBehaviours is not global::Mediator.IStreamPipelineBehavior[] + ); + pipelineBehavioursArray = pipelineBehaviours.ToArray(); + } + + for (int i = pipelineBehavioursArray.Length - 1; i >= 0; i--) + { + var pipeline = pipelineBehavioursArray[i]; + var handlerCopy = handler; + var pipelineCopy = pipeline; + handler = (TRequest message, System.Threading.CancellationToken cancellationToken) => pipelineCopy.Handle(message, handlerCopy, cancellationToken); + } + + _rootHandler = handler; + return this; + } + + public global::System.Collections.Generic.IAsyncEnumerable Handle( + TRequest request, + global::System.Threading.CancellationToken cancellationToken + ) + { + var handler = _rootHandler; + return handler(request, cancellationToken); + } + + public global::System.Collections.Generic.IAsyncEnumerable Handle( + global::Mediator.IStreamCommand request, + global::System.Threading.CancellationToken cancellationToken + ) + { + return Handle((TRequest)request, cancellationToken); + } + + public async global::System.Collections.Generic.IAsyncEnumerable Handle( + object request, + [global::System.Runtime.CompilerServices.EnumeratorCancellation] global::System.Threading.CancellationToken cancellationToken + ) + { + await foreach (var el in Handle((TRequest)request, cancellationToken)) + yield return el; + } + } + [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "3.0.0.0")] + internal interface IQueryHandlerBase : IMessageHandlerBase + { + global::System.Threading.Tasks.ValueTask Handle( + global::Mediator.IQuery request, + global::System.Threading.CancellationToken cancellationToken + ); + } + [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "3.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + [global::System.Diagnostics.DebuggerStepThroughAttribute] + internal sealed class QueryHandlerWrapper : IQueryHandlerBase + where TRequest : global::Mediator.IQuery + { + private global::Mediator.MessageHandlerDelegate _rootHandler = null!; + + public QueryHandlerWrapper Init( + global::Mediator.Internals.ContainerMetadata containerMetadata, + global::System.IServiceProvider sp + ) + { + var concreteHandler = sp.GetRequiredService>(); + var pipelineBehaviours = sp.GetServices>(); + var handler = (global::Mediator.MessageHandlerDelegate)concreteHandler.Handle; + + global::Mediator.IPipelineBehavior[] pipelineBehavioursArray; + if (containerMetadata.ServicesUnderlyingTypeIsArray) + { + global::System.Diagnostics.Debug.Assert( + pipelineBehaviours is global::Mediator.IPipelineBehavior[] + ); + pipelineBehavioursArray = global::System.Runtime.CompilerServices.Unsafe.As[]>( + pipelineBehaviours + ); + } + else + { + global::System.Diagnostics.Debug.Assert( + pipelineBehaviours is not global::Mediator.IPipelineBehavior[] + ); + pipelineBehavioursArray = pipelineBehaviours.ToArray(); + } + + for (int i = pipelineBehavioursArray.Length - 1; i >= 0; i--) + { + var pipeline = pipelineBehavioursArray[i]; + var handlerCopy = handler; + var pipelineCopy = pipeline; + handler = (TRequest message, System.Threading.CancellationToken cancellationToken) => pipelineCopy.Handle(message, handlerCopy, cancellationToken); + } + + _rootHandler = handler; + return this; + } + + public global::System.Threading.Tasks.ValueTask Handle( + TRequest request, + global::System.Threading.CancellationToken cancellationToken + ) + { + var handler = _rootHandler; + return handler(request, cancellationToken); + } + + public global::System.Threading.Tasks.ValueTask Handle( + global::Mediator.IQuery request, + global::System.Threading.CancellationToken cancellationToken + ) + { + return Handle((TRequest)request, cancellationToken); + } + + public async global::System.Threading.Tasks.ValueTask Handle( + object request, + global::System.Threading.CancellationToken cancellationToken + ) + { + return await Handle((TRequest)request, cancellationToken); + } + } + [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "3.0.0.0")] + internal interface IStreamQueryHandlerBase : IStreamMessageHandlerBase + { + global::System.Collections.Generic.IAsyncEnumerable Handle( + global::Mediator.IStreamQuery request, + global::System.Threading.CancellationToken cancellationToken + ); + } + [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "3.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + [global::System.Diagnostics.DebuggerStepThroughAttribute] + internal sealed class StreamQueryHandlerWrapper : IStreamQueryHandlerBase + where TRequest : global::Mediator.IStreamQuery + { + private global::Mediator.StreamHandlerDelegate _rootHandler = null!; + + public StreamQueryHandlerWrapper Init( + global::Mediator.Internals.ContainerMetadata containerMetadata, + global::System.IServiceProvider sp + ) + { + var concreteHandler = sp.GetRequiredService>(); + var pipelineBehaviours = sp.GetServices>(); + var handler = (global::Mediator.StreamHandlerDelegate)concreteHandler.Handle; + + global::Mediator.IStreamPipelineBehavior[] pipelineBehavioursArray; + if (containerMetadata.ServicesUnderlyingTypeIsArray) + { + global::System.Diagnostics.Debug.Assert( + pipelineBehaviours is global::Mediator.IStreamPipelineBehavior[] + ); + pipelineBehavioursArray = global::System.Runtime.CompilerServices.Unsafe.As[]>( + pipelineBehaviours + ); + } + else + { + global::System.Diagnostics.Debug.Assert( + pipelineBehaviours is not global::Mediator.IStreamPipelineBehavior[] + ); + pipelineBehavioursArray = pipelineBehaviours.ToArray(); + } + + for (int i = pipelineBehavioursArray.Length - 1; i >= 0; i--) + { + var pipeline = pipelineBehavioursArray[i]; + var handlerCopy = handler; + var pipelineCopy = pipeline; + handler = (TRequest message, System.Threading.CancellationToken cancellationToken) => pipelineCopy.Handle(message, handlerCopy, cancellationToken); + } + + _rootHandler = handler; + return this; + } + + public global::System.Collections.Generic.IAsyncEnumerable Handle( + TRequest request, + global::System.Threading.CancellationToken cancellationToken + ) + { + var handler = _rootHandler; + return handler(request, cancellationToken); + } + + public global::System.Collections.Generic.IAsyncEnumerable Handle( + global::Mediator.IStreamQuery request, + global::System.Threading.CancellationToken cancellationToken + ) + { + return Handle((TRequest)request, cancellationToken); + } + + public async global::System.Collections.Generic.IAsyncEnumerable Handle( + object request, + [global::System.Runtime.CompilerServices.EnumeratorCancellation] global::System.Threading.CancellationToken cancellationToken + ) + { + await foreach (var el in Handle((TRequest)request, cancellationToken)) + yield return el; + } + } + + [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "3.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + [global::System.Diagnostics.DebuggerStepThroughAttribute] + internal sealed class NotificationHandlerWrapper : INotificationHandlerBase + where TNotification : global::Mediator.INotification + { + private global::Mediator.ForeachAwaitPublisher _publisher = null!; + private global::Mediator.INotificationHandler[] _handlers = null!; + + public NotificationHandlerWrapper Init( + global::Mediator.Internals.ContainerMetadata containerMetadata, + global::System.IServiceProvider sp + ) + { + _publisher = containerMetadata.NotificationPublisher; + var handlers = sp.GetServices>(); + if (containerMetadata.ServicesUnderlyingTypeIsArray) + { + global::System.Diagnostics.Debug.Assert( + handlers is global::Mediator.INotificationHandler[], + $"Unexpected type: {handlers.GetType()}" + ); + _handlers = global::System.Runtime.CompilerServices.Unsafe.As[]>( + handlers + ); + } + else + { + global::System.Diagnostics.Debug.Assert( + handlers is not global::Mediator.INotificationHandler[], + $"Unexpected type: {handlers.GetType()}" + ); + _handlers = handlers.ToArray(); + } + return this; + } + + public global::System.Threading.Tasks.ValueTask Handle( + TNotification notification, + global::System.Threading.CancellationToken cancellationToken + ) + { + var handlers = _handlers; + if (handlers.Length == 0) + { + return default; + } + return _publisher.Publish( + new global::Mediator.NotificationHandlers(handlers, isArray: true), + notification, + cancellationToken + ); + } + + public global::System.Threading.Tasks.ValueTask Handle( + object notification, + global::System.Threading.CancellationToken cancellationToken + ) + { + return Handle((TNotification)notification, cancellationToken); + } + } + + internal interface IContainerProbe { } + internal sealed class ContainerProbe0 : IContainerProbe { } + internal sealed class ContainerProbe1 : IContainerProbe { } + + [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "3.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + [global::System.Diagnostics.DebuggerStepThroughAttribute] + internal sealed class ContainerMetadata + { + public readonly bool ServicesUnderlyingTypeIsArray; + + public readonly global::System.Collections.Frozen.FrozenDictionary RequestHandlerWrappers; + public readonly global::System.Collections.Frozen.FrozenDictionary CommandHandlerWrappers; + public readonly global::System.Collections.Frozen.FrozenDictionary QueryHandlerWrappers; + + public readonly global::System.Collections.Frozen.FrozenDictionary StreamRequestHandlerWrappers; + public readonly global::System.Collections.Frozen.FrozenDictionary StreamCommandHandlerWrappers; + public readonly global::System.Collections.Frozen.FrozenDictionary StreamQueryHandlerWrappers; + + public readonly global::System.Collections.Frozen.FrozenDictionary NotificationHandlerWrappers; + + public readonly global::Mediator.ForeachAwaitPublisher NotificationPublisher; + + public ContainerMetadata(global::System.IServiceProvider sp) + { + ServicesUnderlyingTypeIsArray = sp.GetServices() is global::Mediator.Internals.IContainerProbe[]; + + NotificationPublisher = sp.GetRequiredService(); + + var requestHandlerTypes = new global::System.Collections.Generic.Dictionary(0); + var commandHandlerTypes = new global::System.Collections.Generic.Dictionary(0); + var queryHandlerTypes = new global::System.Collections.Generic.Dictionary(0); + RequestHandlerWrappers = global::System.Collections.Frozen.FrozenDictionary.ToFrozenDictionary(requestHandlerTypes); + CommandHandlerWrappers = global::System.Collections.Frozen.FrozenDictionary.ToFrozenDictionary(commandHandlerTypes); + QueryHandlerWrappers = global::System.Collections.Frozen.FrozenDictionary.ToFrozenDictionary(queryHandlerTypes); + + var streamRequestHandlerTypes = new global::System.Collections.Generic.Dictionary(0); + var streamCommandHandlerTypes = new global::System.Collections.Generic.Dictionary(0); + var streamQueryHandlerTypes = new global::System.Collections.Generic.Dictionary(0); + StreamRequestHandlerWrappers = global::System.Collections.Frozen.FrozenDictionary.ToFrozenDictionary(streamRequestHandlerTypes); + StreamCommandHandlerWrappers = global::System.Collections.Frozen.FrozenDictionary.ToFrozenDictionary(streamCommandHandlerTypes); + StreamQueryHandlerWrappers = global::System.Collections.Frozen.FrozenDictionary.ToFrozenDictionary(streamQueryHandlerTypes); + + var notificationHandlerTypes = new global::System.Collections.Generic.Dictionary(0); + NotificationHandlerWrappers = global::System.Collections.Frozen.FrozenDictionary.ToFrozenDictionary(notificationHandlerTypes); + } + } +} + +namespace Mediator +{ + /// + /// Generated code for Mediator implementation. + /// This type is also registered as a DI service. + /// Can be used directly for high performance scenarios. + /// + [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "3.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute] + [global::System.Diagnostics.DebuggerStepThroughAttribute] + public sealed partial class Mediator : global::Mediator.IMediator, global::Mediator.ISender, global::Mediator.IPublisher + { + internal readonly global::System.IServiceProvider Services; + private FastLazyValue _containerMetadata; + private global::Mediator.ForeachAwaitPublisher? _notificationPublisher; + internal global::Mediator.ForeachAwaitPublisher NotificationPublisher + { + [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] + get + { + if (_notificationPublisher == null) + _notificationPublisher = _containerMetadata.Value.NotificationPublisher; + return _notificationPublisher!; + } + } + private bool? _servicesUnderlyingTypeIsArray; + internal bool ServicesUnderlyingTypeIsArray + { + [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] + get + { + if (_servicesUnderlyingTypeIsArray == null) + _servicesUnderlyingTypeIsArray = _containerMetadata.Value.ServicesUnderlyingTypeIsArray; + return _servicesUnderlyingTypeIsArray!.Value; + } + } + + /// + /// The lifetime of Mediator-related service registrations in DI container. + /// + public const global::Microsoft.Extensions.DependencyInjection.ServiceLifetime ServiceLifetime = global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton; + + /// + /// The name of the notification publisher service that was configured. + /// + public const string NotificationPublisherName = "ForeachAwaitPublisher"; + + /// + /// The total number of Mediator messages that were discovered. + /// + public const int TotalMessages = 0; + + /// + /// Constructor for DI, should not be used by consumer. + /// + public Mediator(global::System.IServiceProvider sp) + { + Services = sp; + _containerMetadata = new FastLazyValue( + self => self.Services.GetRequiredService(), + this + ); + } + + private struct FastLazyValue + { + private const long UNINIT = 0; + private const long INITING = 1; + private const long INITD = 2; + + private global::System.Func _generator; + private long _state; + private T _value; + private TArg _arg; + + public T Value + { + [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] + get + { + if (_state != INITD) + return ValueSlow; + + return _value; + } + } + + private T ValueSlow + { + [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] + get + { + var prevState = global::System.Threading.Interlocked.CompareExchange(ref _state, INITING, UNINIT); + switch (prevState) + { + case INITD: + // Someone has already completed init + return _value; + case INITING: + // Wait for someone else to complete + var spinWait = default(global::System.Threading.SpinWait); + while (global::System.Threading.Interlocked.Read(ref _state) < INITD) + spinWait.SpinOnce(); + return _value; + case UNINIT: + _value = _generator(_arg); + global::System.Threading.Interlocked.Exchange(ref _state, INITD); + return _value; + } + + return _value; + } + } + + public FastLazyValue(global::System.Func generator, TArg arg) + { + _generator = generator; + _state = UNINIT; + _value = default!; + _arg = arg; + } + } + + + + /// + /// Send request. + /// Throws if message is null. + /// Throws if request does not implement . + /// Throws if no handler is registered. + /// + /// Incoming request + /// Cancellation token + /// Awaitable task + public global::System.Threading.Tasks.ValueTask Send( + global::Mediator.IRequest request, + global::System.Threading.CancellationToken cancellationToken = default + ) + { + ThrowInvalidRequest(request, nameof(request)); + return default; + } + + private async global::System.Threading.Tasks.ValueTask SendAsync( + global::Mediator.IRequest request, + global::System.Threading.CancellationToken cancellationToken = default + ) + { + ThrowInvalidRequest(request, nameof(request)); + return default!; + } + + /// + /// Create stream for request. + /// Throws if message is null. + /// Throws if request does not implement . + /// Throws if no handler is registered. + /// + /// Incoming message + /// Cancellation token + /// Async enumerable + public global::System.Collections.Generic.IAsyncEnumerable CreateStream( + global::Mediator.IStreamRequest request, + global::System.Threading.CancellationToken cancellationToken = default + ) + { + ThrowInvalidStreamRequest(request, nameof(request)); + return default!; + } + + /// + /// Send command. + /// Throws if message is null. + /// Throws if command does not implement . + /// Throws if no handler is registered. + /// + /// Incoming command + /// Cancellation token + /// Awaitable task + public global::System.Threading.Tasks.ValueTask Send( + global::Mediator.ICommand command, + global::System.Threading.CancellationToken cancellationToken = default + ) + { + ThrowInvalidCommand(command, nameof(command)); + return default; + } + + private async global::System.Threading.Tasks.ValueTask SendAsync( + global::Mediator.ICommand command, + global::System.Threading.CancellationToken cancellationToken = default + ) + { + ThrowInvalidCommand(command, nameof(command)); + return default!; + } + + /// + /// Create stream for command. + /// Throws if message is null. + /// Throws if command does not implement . + /// Throws if no handler is registered. + /// + /// Incoming message + /// Cancellation token + /// Async enumerable + public global::System.Collections.Generic.IAsyncEnumerable CreateStream( + global::Mediator.IStreamCommand command, + global::System.Threading.CancellationToken cancellationToken = default + ) + { + ThrowInvalidStreamCommand(command, nameof(command)); + return default!; + } + + /// + /// Send query. + /// Throws if message is null. + /// Throws if query does not implement . + /// Throws if no handler is registered. + /// + /// Incoming query + /// Cancellation token + /// Awaitable task + public global::System.Threading.Tasks.ValueTask Send( + global::Mediator.IQuery query, + global::System.Threading.CancellationToken cancellationToken = default + ) + { + ThrowInvalidQuery(query, nameof(query)); + return default; + } + + private async global::System.Threading.Tasks.ValueTask SendAsync( + global::Mediator.IQuery query, + global::System.Threading.CancellationToken cancellationToken = default + ) + { + ThrowInvalidQuery(query, nameof(query)); + return default!; + } + + /// + /// Create stream for query. + /// Throws if message is null. + /// Throws if query does not implement . + /// Throws if no handler is registered. + /// + /// Incoming message + /// Cancellation token + /// Async enumerable + public global::System.Collections.Generic.IAsyncEnumerable CreateStream( + global::Mediator.IStreamQuery query, + global::System.Threading.CancellationToken cancellationToken = default + ) + { + ThrowInvalidStreamQuery(query, nameof(query)); + return default!; + } + + /// + /// Send message. + /// Throws if message is null. + /// Throws if message does not implement . + /// Throws if no handler is registered. + /// + /// Incoming message + /// Cancellation token + /// Awaitable task + public global::System.Threading.Tasks.ValueTask Send( + object message, + global::System.Threading.CancellationToken cancellationToken = default + ) + { + ThrowInvalidMessage(message, nameof(message)); + return default; + } + + /// + /// Create stream. + /// Throws if message is null. + /// Throws if message does not implement . + /// Throws if no handler is registered. + /// + /// Incoming message + /// Cancellation token + /// Async enumerable + public global::System.Collections.Generic.IAsyncEnumerable CreateStream( + object message, + global::System.Threading.CancellationToken cancellationToken = default + ) + { + ThrowInvalidStreamMessage(message, nameof(message)); + return default!; + } + + /// + /// Publish notification. + /// Throws if message is null. + /// Throws if notification does not implement . + /// Throws if handlers throw exception(s). + /// Drops messages + /// + /// Incoming notification + /// Cancellation token + /// Awaitable task + public global::System.Threading.Tasks.ValueTask Publish( + object notification, + global::System.Threading.CancellationToken cancellationToken = default + ) + { + ThrowInvalidNotification(notification, nameof(notification)); + return default; + } + + /// + /// Publish notification. + /// Throws if message is null. + /// Throws if notification does not implement . + /// Throws if handlers throw exception(s). + /// + /// Incoming notification + /// Cancellation token + /// Awaitable task + public global::System.Threading.Tasks.ValueTask Publish( + TNotification notification, + global::System.Threading.CancellationToken cancellationToken = default + ) + where TNotification : global::Mediator.INotification + { + ThrowInvalidNotification(notification, nameof(notification)); + return default; + } + +#if NETSTANDARD2_1_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.DoesNotReturn] +#endif + private static void ThrowMissingHandler(object msg) => + throw new global::Mediator.MissingMessageHandlerException(msg); + +#if NETSTANDARD2_1_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.DoesNotReturn] +#endif + private static void ThrowInvalidMessage(T? msg, string? paramName = null) + { + if (msg == null) + ThrowArgumentNull(paramName); + else if (!(msg is global::Mediator.IMessage)) + ThrowInvalidMessage(msg); + else + ThrowMissingHandler(msg); + } + +#if NETSTANDARD2_1_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.DoesNotReturn] +#endif + private static void ThrowInvalidRequest(T? msg, string? paramName = null) + { + if (msg == null) + ThrowArgumentNull(paramName); + else if (!(msg is global::Mediator.IBaseRequest)) + ThrowInvalidMessage(msg); + else + ThrowMissingHandler(msg); + } + +#if NETSTANDARD2_1_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.DoesNotReturn] +#endif + private static void ThrowInvalidCommand(T? msg, string? paramName = null) + { + if (msg == null) + ThrowArgumentNull(paramName); + else if (!(msg is global::Mediator.IBaseCommand)) + ThrowInvalidMessage(msg); + else + ThrowMissingHandler(msg); + } + +#if NETSTANDARD2_1_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.DoesNotReturn] +#endif + private static void ThrowInvalidQuery(T? msg, string? paramName = null) + { + if (msg == null) + ThrowArgumentNull(paramName); + else if (!(msg is global::Mediator.IBaseQuery)) + ThrowInvalidMessage(msg); + else + ThrowMissingHandler(msg); + } + +#if NETSTANDARD2_1_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.DoesNotReturn] +#endif + private static void ThrowInvalidStreamMessage(T? msg, string? paramName = null) + { + if (msg == null) + ThrowArgumentNull(paramName); + else if (!(msg is global::Mediator.IStreamMessage)) + ThrowInvalidMessage(msg); + else + ThrowMissingHandler(msg); + } + +#if NETSTANDARD2_1_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.DoesNotReturn] +#endif + private static void ThrowInvalidStreamRequest(T? msg, string? paramName = null) + { + if (msg == null) + ThrowArgumentNull(paramName); + else if (!(msg is global::Mediator.IBaseStreamRequest)) + ThrowInvalidMessage(msg); + else + ThrowMissingHandler(msg); + } + +#if NETSTANDARD2_1_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.DoesNotReturn] +#endif + private static void ThrowInvalidStreamCommand(T? msg, string? paramName = null) + { + if (msg == null) + ThrowArgumentNull(paramName); + else if (!(msg is global::Mediator.IBaseStreamCommand)) + ThrowInvalidMessage(msg); + else + ThrowMissingHandler(msg); + } + +#if NETSTANDARD2_1_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.DoesNotReturn] +#endif + private static void ThrowInvalidStreamQuery(T? msg, string? paramName = null) + { + if (msg == null) + ThrowArgumentNull(paramName); + else if (!(msg is global::Mediator.IBaseStreamQuery)) + ThrowInvalidMessage(msg); + else + ThrowMissingHandler(msg); + } + +#if NETSTANDARD2_1_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.DoesNotReturn] +#endif + private static void ThrowArgumentNull(string? paramName) => + throw new global::System.ArgumentNullException(paramName); + +#if NETSTANDARD2_1_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.DoesNotReturn] +#endif + private static void ThrowInvalidMessage(T msg) => + throw new global::Mediator.InvalidMessageException(msg); + + private static void ThrowIfNull(T? argument, string paramName) + { + if (argument == null) + ThrowArgumentNull(paramName); + } + + private static void ThrowInvalidNotification(T? argument, string paramName) + { + if (argument == null) + ThrowArgumentNull(paramName); + else if (!(argument is global::Mediator.INotification)) + ThrowInvalidMessage(argument); + } + +#if NETSTANDARD2_1_OR_GREATER + [global::System.Diagnostics.CodeAnalysis.DoesNotReturn] +#endif + private static void ThrowAggregateException(global::System.Collections.Generic.List exceptions) => + throw new global::System.AggregateException(exceptions); + + private static void MaybeThrowAggregateException(global::System.Collections.Generic.List? exceptions) + { + if (exceptions != null) + { + ThrowAggregateException(exceptions); + } + } + } +} diff --git a/GFramework.SourceGenerators/Generated/Mediator.SourceGenerator/Mediator.SourceGenerator.IncrementalMediatorGenerator/MediatorOptions.g.cs b/GFramework.SourceGenerators/Generated/Mediator.SourceGenerator/Mediator.SourceGenerator.IncrementalMediatorGenerator/MediatorOptions.g.cs new file mode 100644 index 0000000..309c180 --- /dev/null +++ b/GFramework.SourceGenerators/Generated/Mediator.SourceGenerator/Mediator.SourceGenerator.IncrementalMediatorGenerator/MediatorOptions.g.cs @@ -0,0 +1,55 @@ +// +// Generated by the Mediator source generator. +// + +namespace Mediator +{ + /// + /// Provide options for the Mediator source generator. + /// + [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "3.0.0.0")] + public sealed class MediatorOptions + { + /// + /// The namespace in which the Mediator implementation is generated. + /// By default, the namespace is "Mediator". + /// + public string Namespace { get; set; } = "Mediator"; + + /// + /// Wether or not generated types should be internal (they are public by default). + /// + public bool GenerateTypesAsInternal { get; set; } = false; + + /// + /// The type to use when publishing notifications. + /// By default, the type is . + /// + public global::System.Type NotificationPublisherType { get; set; } = typeof(global::Mediator.ForeachAwaitPublisher); + + /// + /// The default lifetime of the services registered in the DI container by the Mediator source generator. + /// By default, the lifetime is . + /// + public global::Microsoft.Extensions.DependencyInjection.ServiceLifetime ServiceLifetime { get; set; } = + global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton; + + /// + /// The collection of assemblies to scan for Mediator handlers. + /// By default, the collection is empty, in which case the source generator will scan all assemblies through references from the source generated project. + /// + public global::System.Collections.Generic.IReadOnlyList Assemblies { get; set; } = new global::Mediator.AssemblyReference[0]; + + /// + /// The collection of types of pipeline behaviors to register in DI. + /// When the type is an unconstructed generic type, the source generator will register all the constructed types of the generic type (open generics that is supported during AoT). + /// + public global::System.Collections.Generic.IReadOnlyList PipelineBehaviors { get; set; } = new global::System.Type[0]; + + /// + /// The collection of types of streaming pipeline behaviors to register in DI. + /// When the type is an unconstructed generic type, the source generator will register all the constructed types of the generic type (open generics that is supported during AoT). + /// + public global::System.Collections.Generic.IReadOnlyList StreamPipelineBehaviors { get; set; } = new global::System.Type[0]; + } +} diff --git a/GFramework.SourceGenerators/Generated/Mediator.SourceGenerator/Mediator.SourceGenerator.IncrementalMediatorGenerator/MediatorOptionsAttribute.g.cs b/GFramework.SourceGenerators/Generated/Mediator.SourceGenerator/Mediator.SourceGenerator.IncrementalMediatorGenerator/MediatorOptionsAttribute.g.cs new file mode 100644 index 0000000..8ecee2a --- /dev/null +++ b/GFramework.SourceGenerators/Generated/Mediator.SourceGenerator/Mediator.SourceGenerator.IncrementalMediatorGenerator/MediatorOptionsAttribute.g.cs @@ -0,0 +1,33 @@ +// +// Generated by the Mediator source generator. +// + +namespace Mediator +{ + /// + /// Provide options for the Mediator source generator. + /// + [global::System.AttributeUsage(global::System.AttributeTargets.Assembly, AllowMultiple = false)] + [global::System.CodeDom.Compiler.GeneratedCode("Mediator.SourceGenerator", "3.0.0.0")] + public sealed class MediatorOptionsAttribute : global::System.Attribute + { + /// + /// The namespace in which the Mediator implementation is generated. + /// By default, the namespace is "Mediator". + /// + public string Namespace { get; set; } = "Mediator"; + + /// + /// The type to use when publishing notifications. + /// By default, the type is . + /// + public global::System.Type NotificationPublisherType { get; set; } = typeof(global::Mediator.ForeachAwaitPublisher); + + /// + /// The default lifetime of the services registered in the DI container by the Mediator source generator. + /// By default, the lifetime is . + /// + public global::Microsoft.Extensions.DependencyInjection.ServiceLifetime ServiceLifetime { get; set; } = + global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton; + } +}