Files
Toolkit2/Toolkit.Foundation/AsyncHandlerInitialization.cs
T
2024-12-04 22:35:58 +00:00

60 lines
2.8 KiB
C#

using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.DependencyInjection;
namespace Toolkit.Foundation;
public class AsyncHandlerInitialization<TMessage, TResponse, THandler>(IMessenger messenger,
IServiceProvider provider) :
IInitialization, IInitializationScoped where THandler : class, IAsyncHandler<TMessage, TResponse>
where TMessage : class
{
public void Initialize()
{
if (!messenger.IsRegistered<AsyncResponseEventArgs<TMessage, TResponse>>(provider))
{
messenger.Register<IServiceProvider, AsyncResponseEventArgs<TMessage, TResponse>>(provider,
(provider, args) =>
{
IEnumerable<IAsyncHandler<TMessage, TResponse>> handlers = provider.GetServices<IAsyncHandler<TMessage, TResponse>>();
IEnumerable<IAsyncPipelineBehavior<TMessage, TResponse>> behaviors = provider.GetServices<IAsyncPipelineBehavior<TMessage, TResponse>>();
foreach (IAsyncHandler<TMessage, TResponse> handler in handlers)
{
Task<TResponse> ExecutePipeline(int index) =>index < 0
? handler.Handle(args.Message, args.CancellationToken)
: behaviors.ElementAt(index).Handle(args.Message, () => ExecutePipeline(index - 1));
args.Reply(ExecutePipeline(behaviors.Count() - 1));
}
});
}
}
}
public class AsyncHandlerInitialization<TMessage, THandler>(IMessenger messenger,
IServiceProvider provider) :
IInitialization, IInitializationScoped where THandler : class, IAsyncHandler<TMessage>
where TMessage : class
{
public void Initialize()
{
if (!messenger.IsRegistered<AsyncResponseEventArgs<TMessage, Unit>>(provider))
{
messenger.Register<IServiceProvider, AsyncResponseEventArgs<TMessage, Unit>>(provider,
(provider, args) =>
{
IEnumerable<IAsyncHandler<TMessage>> handlers = provider.GetServices<IAsyncHandler<TMessage>>();
IEnumerable<IAsyncPipelineBehavior<TMessage>> behaviors = provider.GetServices<IAsyncPipelineBehavior<TMessage>>();
foreach (IAsyncHandler<TMessage> handler in handlers)
{
Task<Unit> 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);
args.Reply(ExecutePipeline(behaviors.Count() - 1));
}
});
}
}
}