diff --git a/Toolkit.Foundation/AsyncHandlerDelegate.cs b/Toolkit.Foundation/AsyncHandlerDelegate.cs deleted file mode 100644 index bc2a07c..0000000 --- a/Toolkit.Foundation/AsyncHandlerDelegate.cs +++ /dev/null @@ -1,3 +0,0 @@ -namespace Toolkit.Foundation; - -public delegate Task AsyncHandlerDelegate(); \ No newline at end of file diff --git a/Toolkit.Foundation/AsyncHandlerInitialization.cs b/Toolkit.Foundation/AsyncHandlerInitialization.cs index 98c03a6..5585331 100644 --- a/Toolkit.Foundation/AsyncHandlerInitialization.cs +++ b/Toolkit.Foundation/AsyncHandlerInitialization.cs @@ -19,16 +19,11 @@ public class AsyncHandlerInitialization(IServiceP foreach (IAsyncHandler handler in handlers) { - AsyncHandlerDelegate handlerDelegate = () => - handler.Handle(args.Message, args.CancellationToken); + Task ExecutePipeline(int index) =>index < 0 + ? handler.Handle(args.Message, args.CancellationToken) + : behaviors.ElementAt(index).Handle(args.Message, () => ExecutePipeline(index - 1)); - foreach (IAsyncPipelineBehavior? behavior in behaviors.Reverse()) - { - AsyncHandlerDelegate next = handlerDelegate; - handlerDelegate = () => behavior.Handle(args.Message, next); - } - - args.Reply(handlerDelegate()); + args.Reply(ExecutePipeline(behaviors.Count() - 1)); } }); } @@ -47,21 +42,15 @@ public class AsyncHandlerInitialization(IServiceProvider pro (provider, args) => { IEnumerable> handlers = provider.GetServices>(); - IEnumerable> behaviors = provider.GetServices>(); + IEnumerable> behaviors = provider.GetServices>(); foreach (IAsyncHandler handler in handlers) { - AsyncHandlerDelegate handlerDelegate = () => - handler.Handle(args.Message, args.CancellationToken).ContinueWith(_ => Unit.Value); + Task ExecutePipeline(int index) => index < 0 + ? handler.Handle(args.Message, args.CancellationToken).ContinueWith(_ => Unit.Value) + : behaviors.ElementAt(index).Handle(args.Message, () => ExecutePipeline(index - 1)).ContinueWith(_ => Unit.Value); - foreach (IAsyncPipelineBehavior behavior in behaviors.Reverse()) - { - AsyncHandlerDelegate next = handlerDelegate; - handlerDelegate = () => behavior.Handle(args.Message, next); - } - - handlerDelegate(); - args.Reply(Unit.Value); + args.Reply(ExecutePipeline(behaviors.Count() - 1)); } }); } diff --git a/Toolkit.Foundation/AsyncHandlerKeyedInitialization.cs b/Toolkit.Foundation/AsyncHandlerKeyedInitialization.cs index 86ea47f..c13b218 100644 --- a/Toolkit.Foundation/AsyncHandlerKeyedInitialization.cs +++ b/Toolkit.Foundation/AsyncHandlerKeyedInitialization.cs @@ -15,21 +15,15 @@ public class AsyncHandlerKeyedInitialization(string key, ISe (provider, args) => { IEnumerable> handlers = provider.GetKeyedServices>(key); - IEnumerable> behaviors = provider.GetServices>(); + IEnumerable> behaviors = provider.GetKeyedServices>(key); foreach (IAsyncHandler handler in handlers) { - AsyncHandlerDelegate handlerDelegate = () => - handler.Handle(args.Message, args.CancellationToken).ContinueWith(_ => Unit.Value); + Task ExecutePipeline(int index) => index < 0 + ? handler.Handle(args.Message, args.CancellationToken).ContinueWith(_ => Unit.Value) + : behaviors.ElementAt(index).Handle(args.Message, () => ExecutePipeline(index - 1)).ContinueWith(_ => Unit.Value); - foreach (IAsyncPipelineBehavior behavior in behaviors.Reverse()) - { - AsyncHandlerDelegate next = handlerDelegate; - handlerDelegate = () => behavior.Handle(args.Message, next); - } - - handlerDelegate(); - args.Reply(Unit.Value); + args.Reply(ExecutePipeline(behaviors.Count() - 1)); } }); } @@ -48,22 +42,17 @@ public class AsyncHandlerKeyedInitialization(stri (provider, args) => { IEnumerable> handlers = provider.GetKeyedServices>(key); - IEnumerable> behaviors = provider.GetServices>(); + IEnumerable> behaviors = provider.GetKeyedServices>(key); foreach (IAsyncHandler handler in handlers) { - AsyncHandlerDelegate handlerDelegate = () => - handler.Handle(args.Message, args.CancellationToken); + Task ExecutePipeline(int index) => index < 0 + ? handler.Handle(args.Message, args.CancellationToken) + : behaviors.ElementAt(index).Handle(args.Message, () => ExecutePipeline(index - 1)); - foreach (IAsyncPipelineBehavior behavior in behaviors.Reverse()) - { - AsyncHandlerDelegate next = handlerDelegate; - handlerDelegate = () => behavior.Handle(args.Message, next); - } - - args.Reply(handlerDelegate()); + args.Reply(ExecutePipeline(behaviors.Count() - 1)); } }); } } -} \ No newline at end of file +} diff --git a/Toolkit.Foundation/HandlerDelegate.cs b/Toolkit.Foundation/HandlerDelegate.cs deleted file mode 100644 index 60bb37e..0000000 --- a/Toolkit.Foundation/HandlerDelegate.cs +++ /dev/null @@ -1,3 +0,0 @@ -namespace Toolkit.Foundation; - -public delegate TResponse HandlerDelegate(); \ No newline at end of file diff --git a/Toolkit.Foundation/HandlerInitialization.cs b/Toolkit.Foundation/HandlerInitialization.cs index dc6deb1..bb344ec 100644 --- a/Toolkit.Foundation/HandlerInitialization.cs +++ b/Toolkit.Foundation/HandlerInitialization.cs @@ -17,28 +17,20 @@ public class HandlerInitialization(IServiceProvid IEnumerable> handlers = provider.GetServices>(); IEnumerable> behaviors = provider.GetServices>(); - HandlerDelegate handlerDelegate = () => + foreach (IHandler handler in handlers) { - TResponse response = default!; - foreach (IHandler handler in handlers) - { - response = handler.Handle(args.Message); - } - return response; - }; + TResponse ExecutePipeline(int index) => index < 0 + ? handler.Handle(args.Message) + : behaviors.ElementAt(index).Handle(args.Message, () => ExecutePipeline(index - 1)); - foreach (IPipelineBehavior behavior in behaviors.Reverse()) - { - HandlerDelegate next = handlerDelegate; - handlerDelegate = () => behavior.Handle(args.Message, next); + ExecutePipeline(behaviors.Count() - 1); } - - handlerDelegate(); }); } } } + public class HandlerInitialization(IServiceProvider provider) : IInitialization where THandler : class, IHandler where TMessage : class @@ -51,26 +43,28 @@ public class HandlerInitialization(IServiceProvider provider (provider, args) => { IEnumerable> handlers = provider.GetServices>(); - IEnumerable> behaviors = provider.GetServices>(); + IEnumerable> behaviors = provider.GetServices>(); - HandlerDelegate handlerDelegate = () => + foreach (IHandler handler in handlers) { - foreach (IHandler handler in handlers) + void ExecutePipeline(int index) { - handler.Handle(args); + if (index < 0) + { + handler.Handle(args); + return; + } + + behaviors.ElementAt(index).Handle(args, () => + { + ExecutePipeline(index - 1); + return Unit.Value; + }); } - return Unit.Value; - }; - foreach (IPipelineBehavior behavior in behaviors.Reverse()) - { - HandlerDelegate next = handlerDelegate; - handlerDelegate = () => behavior.Handle(args, next); + ExecutePipeline(behaviors.Count() - 1); } - - handlerDelegate(); }); } } } - diff --git a/Toolkit.Foundation/HandlerKeyedInitialization.cs b/Toolkit.Foundation/HandlerKeyedInitialization.cs index ac55c4b..ff7219a 100644 --- a/Toolkit.Foundation/HandlerKeyedInitialization.cs +++ b/Toolkit.Foundation/HandlerKeyedInitialization.cs @@ -15,30 +15,32 @@ public class HandlerKeyedInitialization(string key, IService (provider, args) => { IEnumerable> handlers = provider.GetKeyedServices>(key); - IEnumerable> behaviors = provider.GetServices>(); + IEnumerable> behaviors = provider.GetServices>(); - HandlerDelegate handlerDelegate = () => + foreach (IHandler handler in handlers) { - foreach (IHandler handler in handlers) + void ExecutePipeline(int index) { - handler.Handle(args); + if (index < 0) + { + handler.Handle(args); + return; + } + + behaviors.ElementAt(index).Handle(args, () => + { + ExecutePipeline(index - 1); + return Unit.Value; + }); } - return Unit.Value; - }; - foreach (IPipelineBehavior behavior in behaviors.Reverse()) - { - HandlerDelegate next = handlerDelegate; - handlerDelegate = () => behavior.Handle(args, next); + ExecutePipeline(behaviors.Count() - 1); } - - handlerDelegate(); }); } } } - public class HandlerKeyedInitialization(string key, IServiceProvider provider) : IInitialization where THandler : class, IHandler where TMessage : class @@ -53,24 +55,15 @@ public class HandlerKeyedInitialization(string ke IEnumerable> handlers = provider.GetKeyedServices>(key); IEnumerable> behaviors = provider.GetServices>(); - HandlerDelegate handlerDelegate = () => + foreach (IHandler handler in handlers) { - TResponse response = default!; - foreach (IHandler handler in handlers) - { - response = handler.Handle(args.Message); - } - return response; - }; + TResponse ExecutePipeline(int index) => index < 0 + ? handler.Handle(args.Message) + : behaviors.ElementAt(index).Handle(args.Message, () => ExecutePipeline(index - 1)); - foreach (IPipelineBehavior behavior in behaviors.Reverse()) - { - HandlerDelegate next = handlerDelegate; - handlerDelegate = () => behavior.Handle(args.Message, next); + ExecutePipeline(behaviors.Count() - 1); } - - handlerDelegate(); }); } } -} +} \ No newline at end of file diff --git a/Toolkit.Foundation/IAsyncHandler.cs b/Toolkit.Foundation/IAsyncHandler.cs index 5665c37..526ce02 100644 --- a/Toolkit.Foundation/IAsyncHandler.cs +++ b/Toolkit.Foundation/IAsyncHandler.cs @@ -3,11 +3,13 @@ public interface IAsyncHandler : IHandler { - Task Handle(TMessage args, CancellationToken cancellationToken = default); + Task Handle(TMessage args, + CancellationToken cancellationToken = default); } public interface IAsyncHandler : IHandler { - Task Handle(TMessage args, CancellationToken cancellationToken = default); + Task Handle(TMessage args, + CancellationToken cancellationToken = default); } diff --git a/Toolkit.Foundation/IAsyncPipelineBehavior.cs b/Toolkit.Foundation/IAsyncPipelineBehavior.cs index 9955b12..14997cb 100644 --- a/Toolkit.Foundation/IAsyncPipelineBehavior.cs +++ b/Toolkit.Foundation/IAsyncPipelineBehavior.cs @@ -1,8 +1,14 @@ namespace Toolkit.Foundation; -public interface IAsyncPipelineBehavior { Task Handle(TMessage message, - AsyncHandlerDelegate next); + Func> next); +} + +public interface IAsyncPipelineBehavior +{ + Task Handle(TMessage message, + Func next); } \ No newline at end of file diff --git a/Toolkit.Foundation/IPipelineBehavior.cs b/Toolkit.Foundation/IPipelineBehavior.cs index f29b682..5f57f9c 100644 --- a/Toolkit.Foundation/IPipelineBehavior.cs +++ b/Toolkit.Foundation/IPipelineBehavior.cs @@ -1,9 +1,14 @@ namespace Toolkit.Foundation; -public interface IPipelineBehavior { - TResponse Handle(TMessage message, - HandlerDelegate next); + TResponse Handle(TMessage message, + Func next); } +public interface IPipelineBehavior +{ + void Handle(TMessage message, + Func next); +} \ No newline at end of file diff --git a/Toolkit.Foundation/IServiceCollectionExtensions.cs b/Toolkit.Foundation/IServiceCollectionExtensions.cs index 0a793f8..bbbd070 100644 --- a/Toolkit.Foundation/IServiceCollectionExtensions.cs +++ b/Toolkit.Foundation/IServiceCollectionExtensions.cs @@ -163,23 +163,25 @@ public static class IServiceCollectionExtensions where TInitialization : class, IInitialization { - services.AddTransient(provider => provider.GetRequiredService().Create(parameters)); + services.AddTransient(provider => provider.GetRequiredService() + .Create(parameters)); + return services; } - public static IServiceCollection AddAsyncPipelineBehavior(this IServiceCollection services) - where TBehavior : class, - IAsyncPipelineBehavior + public static IServiceCollection AddAsyncPipelineBehavior(this IServiceCollection services, + Type behaviorType, + string? key = null) { - services.AddTransient, TBehavior>(); - return services; - } + if (key is { Length: > 0 }) + { + services.AddKeyedTransient(typeof(IAsyncPipelineBehavior<>), key, behaviorType); + } + else + { + services.AddTransient(typeof(IAsyncPipelineBehavior<>), behaviorType); + } - public static IServiceCollection AddPipelineBehavior(this IServiceCollection services) - where TBehavior : class, - IPipelineBehavior - { - services.AddTransient, TBehavior>(); return services; }