This commit is contained in:
Dan Clark
2024-11-20 12:51:25 +00:00
parent 8963201480
commit 736fd2802b
17 changed files with 222 additions and 60 deletions
@@ -7,15 +7,38 @@ public class AsyncHandlerInitialization<TMessage, TResponse, THandler>(IServiceP
IInitialization where THandler : class, IAsyncHandler<TMessage, TResponse>
where TMessage : class
{
public void Initialize() => StrongReferenceMessenger.Default.Register<IServiceProvider, AsyncResponseEventArgs<TMessage, TResponse>>(provider,
(provider, args) => args.Reply(provider.GetRequiredService<THandler>().Handle(args.Message, args.CancellationToken)));
args.Reply(handler.Handle(args.Message, args.CancellationToken);
public void Initialize()
{
if (!StrongReferenceMessenger.Default.IsRegistered<ResponseEventArgs<TMessage, TResponse>>(provider))
{
StrongReferenceMessenger.Default.Register<IServiceProvider, AsyncResponseEventArgs<TMessage, TResponse>>(provider,
(provider, args) =>
{
foreach (IAsyncHandler<TMessage, TResponse> handler in provider.GetServices<IAsyncHandler<TMessage, TResponse>>())
{
args.Reply(handler.Handle(args.Message, args.CancellationToken));
}
});
}
}
}
public class AsyncHandlerInitialization<TMessage, THandler>(IServiceProvider provider) :
IInitialization where THandler : class, IAsyncHandler<TMessage>
where TMessage : class
{
public void Initialize() => StrongReferenceMessenger.Default.Register<IServiceProvider, AsyncResponseEventArgs<TMessage, Unit>>(provider,
(provider, args) => provider.GetRequiredService<THandler>().Handle(args.Message, args.CancellationToken));
}
public void Initialize()
{
if (!StrongReferenceMessenger.Default.IsRegistered<TMessage>(provider))
{
StrongReferenceMessenger.Default.Register<IServiceProvider, AsyncResponseEventArgs<TMessage, Unit>>(provider,
(provider, args) =>
{
foreach (IAsyncHandler<TMessage> handler in provider.GetServices<IAsyncHandler<TMessage>>())
{
handler.Handle(args.Message, args.CancellationToken);
}
});
}
}
}
@@ -0,0 +1,44 @@
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.DependencyInjection;
namespace Toolkit.Foundation;
public class AsyncHandlerKeyedInitialization<TMessage, THandler>(string key, IServiceProvider provider) :
IInitialization where THandler : class, IAsyncHandler<TMessage>
where TMessage : class
{
public void Initialize()
{
if (!StrongReferenceMessenger.Default.IsRegistered<TMessage, string>(provider, key))
{
StrongReferenceMessenger.Default.Register<IServiceProvider, TMessage, string>(provider, key,
(provider, args) =>
{
foreach (IAsyncHandler<TMessage> handler in provider.GetKeyedServices<IAsyncHandler<TMessage>>(key))
{
handler.Handle(args);
}
});
}
}
}
public class AsyncHandlerKeyedInitialization<TMessage, TResponse, THandler>(string key, IServiceProvider provider) :
IInitialization where THandler : class, IAsyncHandler<TMessage, TResponse>
where TMessage : class
{
public void Initialize()
{
if (!StrongReferenceMessenger.Default.IsRegistered<ResponseEventArgs<TMessage, TResponse>, string>(provider, key))
{
StrongReferenceMessenger.Default.Register<IServiceProvider, ResponseEventArgs<TMessage, TResponse>, string>(provider, key,
(provider, args) =>
{
foreach (IAsyncHandler<TMessage, TResponse> handler in provider.GetKeyedServices<IAsyncHandler<TMessage, TResponse>>(key))
{
handler.Handle(args.Message);
}
});
}
}
}
+3 -3
View File
@@ -2,9 +2,9 @@
public record Create
{
public static CreateEventArgs<TSender> As<TSender>(TSender sender, params object[] parameters) =>
new(sender, parameters);
public static CreateEventArgs<TSender> As<TSender>(TSender sender) =>
new(sender);
public static CreateEventArgs<TSender> As<TSender>(params object[] parameters) where TSender : new() =>
new(new TSender(), parameters);
new(new TSender());
}
+1 -2
View File
@@ -1,4 +1,3 @@
namespace Toolkit.Foundation;
public record CreateEventArgs<TSender>(TSender? Sender = default,
params object[] Parameters);
public record CreateEventArgs<TSender>(TSender? Sender = default);
+28 -12
View File
@@ -7,22 +7,38 @@ public class HandlerInitialization<TMessage, TResponse, THandler>(IServiceProvid
IInitialization where THandler : class, IHandler<TMessage, TResponse>
where TMessage : class
{
public void Initialize() => StrongReferenceMessenger.Default.Register<IServiceProvider, ResponseEventArgs<TMessage, TResponse>>(provider,
(provider, args) => args.Reply(provider.GetRequiredService<THandler>().Handle(args.Message)));
public void Initialize()
{
if (!StrongReferenceMessenger.Default.IsRegistered<ResponseEventArgs<TMessage, TResponse>>(provider))
{
StrongReferenceMessenger.Default.Register<IServiceProvider, ResponseEventArgs<TMessage, TResponse>>(provider,
(provider, args) =>
{
foreach (IHandler<TMessage, TResponse> handler in provider.GetServices<IHandler<TMessage, TResponse>>())
{
handler.Handle(args.Message);
}
});
}
}
}
public class HandlerInitialization<TMessage, THandler>(IServiceProvider provider) :
IInitialization where THandler : class, IHandler<TMessage>
where TMessage : class
{
public void Initialize() => StrongReferenceMessenger.Default.Register<IServiceProvider, TMessage>(provider,
(provider, args) => provider.GetRequiredService<THandler>().Handle(args));
public void Initialize()
{
if (!StrongReferenceMessenger.Default.IsRegistered<TMessage>(provider))
{
StrongReferenceMessenger.Default.Register<IServiceProvider, TMessage>(provider,
(provider, args) =>
{
foreach (IHandler<TMessage> handler in provider.GetServices<IHandler<TMessage>>())
{
handler.Handle(args);
}
});
}
}
}
public class HandlerKeyedInitialization<TMessage, THandler>(string key, IServiceProvider provider) :
IInitialization where THandler : class, IHandler<TMessage>
where TMessage : class
{
public void Initialize() => StrongReferenceMessenger.Default.Register<IServiceProvider, TMessage, string>(provider, key,
(provider, args) => provider.GetRequiredKeyedService<THandler>(key).Handle(args));
}
@@ -0,0 +1,44 @@
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.DependencyInjection;
namespace Toolkit.Foundation;
public class HandlerKeyedInitialization<TMessage, THandler>(string key, IServiceProvider provider) :
IInitialization where THandler : class, IHandler<TMessage>
where TMessage : class
{
public void Initialize()
{
if (!StrongReferenceMessenger.Default.IsRegistered<TMessage, string>(provider, key))
{
StrongReferenceMessenger.Default.Register<IServiceProvider, TMessage, string>(provider, key,
(provider, args) =>
{
foreach (IHandler<TMessage> handler in provider.GetKeyedServices<IHandler<TMessage>>(key))
{
handler.Handle(args);
}
});
}
}
}
public class HandlerKeyedInitialization<TMessage, TResponse, THandler>(string key, IServiceProvider provider) :
IInitialization where THandler : class, IHandler<TMessage, TResponse>
where TMessage : class
{
public void Initialize()
{
if (!StrongReferenceMessenger.Default.IsRegistered<ResponseEventArgs<TMessage, TResponse>, string>(provider, key))
{
StrongReferenceMessenger.Default.Register<IServiceProvider, ResponseEventArgs<TMessage, TResponse>, string>(provider, key,
(provider, args) =>
{
foreach (IHandler<TMessage, TResponse> handler in provider.GetKeyedServices<IHandler<TMessage, TResponse>>(key))
{
handler.Handle(args.Message);
}
});
}
}
}
@@ -1,6 +1,6 @@
namespace Toolkit.Foundation;
public interface IConfirmation
public interface IAsyncConfirmation
{
Task<bool> Confirm();
}
@@ -1,6 +1,6 @@
namespace Toolkit.Foundation;
public interface IPrimaryConfirmation
public interface IAsyncPrimaryConfirmation
{
Task<bool> ConfirmPrimary();
}
@@ -1,6 +1,6 @@
namespace Toolkit.Foundation;
public interface ISecondaryConfirmation
public interface IAsyncSecondaryConfirmation
{
Task<bool> ConfirmSecondary();
}
-6
View File
@@ -1,6 +0,0 @@
namespace Toolkit.Foundation;
public interface IIndexed
{
public int Index { get; }
}
@@ -5,23 +5,51 @@ namespace Toolkit.Foundation;
public static class IServiceCollectionExtensions
{
public static IServiceCollection AddAsyncHandler<TMessage, TResponse, THandler>(this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Transient)
string key)
where THandler : class, IAsyncHandler<TMessage, TResponse>
where TMessage : class => AddAsyncHandler<TMessage, TResponse, THandler>(services, ServiceLifetime.Transient, key);
public static IServiceCollection AddAsyncHandler<TMessage, TResponse, THandler>(this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Transient,
string? key = null)
where THandler : class, IAsyncHandler<TMessage, TResponse>
where TMessage : class
{
services.Add(new ServiceDescriptor(typeof(THandler), typeof(THandler), lifetime));
services.AddInitialization<AsyncHandlerInitialization<TMessage, TResponse, THandler>>();
if (key is { Length: > 0 })
{
services.Add(new ServiceDescriptor(typeof(IAsyncHandler<TMessage, TResponse>), key, typeof(THandler), lifetime));
services.AddInitialization<AsyncHandlerKeyedInitialization<TMessage, TResponse, IAsyncHandler<TMessage, TResponse>>>(key);
}
else
{
services.Add(new ServiceDescriptor(typeof(IAsyncHandler<TMessage, TResponse>), typeof(THandler), lifetime));
services.AddInitialization<AsyncHandlerInitialization<TMessage, TResponse, IAsyncHandler<TMessage, TResponse>>>();
}
return services;
}
public static IServiceCollection AddAsyncHandler<TMessage, THandler>(this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Transient)
string key)
where THandler : class, IAsyncHandler<TMessage>
where TMessage : class => AddAsyncHandler<TMessage, THandler>(services, ServiceLifetime.Transient, key);
public static IServiceCollection AddAsyncHandler<TMessage, THandler>(this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Transient,
string? key = null)
where THandler : class, IAsyncHandler<TMessage>
where TMessage : class
{
services.Add(new ServiceDescriptor(typeof(THandler), typeof(THandler), lifetime));
services.AddInitialization<AsyncHandlerInitialization<TMessage, THandler>>();
if (key is { Length: > 0 })
{
services.Add(new ServiceDescriptor(typeof(IAsyncHandler<TMessage>), key, typeof(THandler), lifetime));
services.AddInitialization<AsyncHandlerInitialization<TMessage, IAsyncHandler<TMessage>>>(key);
}
else
{
services.Add(new ServiceDescriptor(typeof(IAsyncHandler<TMessage>), typeof(THandler), lifetime));
services.AddInitialization<AsyncHandlerKeyedInitialization<TMessage, IAsyncHandler<TMessage>>>();
}
return services;
}
@@ -35,7 +63,7 @@ public static class IServiceCollectionExtensions
}
public static IServiceCollection AddCache<TKey, TValue>(this IServiceCollection services)
where TKey : notnull
where TKey : notnull
where TValue : notnull
{
services.AddScoped<ICache<TKey, TValue>, Cache<TKey, TValue>>();
@@ -61,12 +89,26 @@ public static class IServiceCollectionExtensions
}
public static IServiceCollection AddHandler<TMessage, TResponse, THandler>(this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Transient)
string key)
where THandler : class, IHandler<TMessage, TResponse>
where TMessage : class => AddHandler<TMessage, TResponse, THandler>(services, ServiceLifetime.Transient, key);
public static IServiceCollection AddHandler<TMessage, TResponse, THandler>(this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Transient,
string? key = null)
where THandler : class, IHandler<TMessage, TResponse>
where TMessage : class
{
services.Add(new ServiceDescriptor(typeof(THandler), typeof(THandler), lifetime));
services.AddInitialization<HandlerInitialization<TMessage, TResponse, THandler>>();
if (key is { Length: > 0 })
{
services.Add(new ServiceDescriptor(typeof(IHandler<TMessage, TResponse>), key, typeof(THandler), lifetime));
services.AddInitialization<HandlerKeyedInitialization<TMessage, TResponse, IHandler<TMessage, TResponse>>>(key);
}
else
{
services.Add(new ServiceDescriptor(typeof(IHandler<TMessage, TResponse>), typeof(THandler), lifetime));
services.AddInitialization<HandlerInitialization<TMessage, TResponse, IHandler<TMessage, TResponse>>>();
}
return services;
}
@@ -84,13 +126,13 @@ public static class IServiceCollectionExtensions
{
if (key is { Length: > 0})
{
services.Add(new ServiceDescriptor(typeof(THandler), key, typeof(THandler), lifetime));
services.AddInitialization<HandlerKeyedInitialization<TMessage, THandler>>(key);
services.Add(new ServiceDescriptor(typeof(IHandler<TMessage>), key, typeof(THandler), lifetime));
services.AddInitialization<HandlerKeyedInitialization<TMessage, IHandler<TMessage>>>(key);
}
else
{
services.Add(new ServiceDescriptor(typeof(THandler), typeof(THandler), lifetime));
services.AddInitialization<HandlerInitialization<TMessage, THandler>>();
services.Add(new ServiceDescriptor(typeof(IHandler<TMessage>), typeof(THandler), lifetime));
services.AddInitialization<HandlerInitialization<TMessage, IHandler<TMessage>>>();
}
return services;
@@ -147,7 +189,7 @@ public static class IServiceCollectionExtensions
public static IServiceCollection AddTemplate<TViewModel, TViewModelImplementation, TView>(this IServiceCollection services,
object? key = null,
ServiceLifetime serviceLifetime = ServiceLifetime.Transient,
params object[]? parameters)
params object?[]? parameters)
{
Type viewModelType = typeof(TViewModel);
Type viewModelImplementationType = typeof(TViewModelImplementation);
@@ -190,7 +232,7 @@ public static class IServiceCollectionExtensions
Action<TValue?, TConfiguration> writeDelegate,
object? key = null,
ServiceLifetime serviceLifetime = ServiceLifetime.Transient,
params object[]? parameters)
params object?[]? parameters)
{
parameters = [readDelegate, writeDelegate, .. parameters ?? Enumerable.Empty<object?>()];
return AddTemplate<TViewModel, TViewModel, TView>(services, key, serviceLifetime, parameters);
@@ -201,7 +243,7 @@ public static class IServiceCollectionExtensions
Action<TValue?, TConfiguration> writeDelegate,
object? key = null,
ServiceLifetime serviceLifetime = ServiceLifetime.Transient,
params object[]? parameters)
params object?[]? parameters)
{
parameters = [readDelegate, writeDelegate, .. parameters ?? Enumerable.Empty<object?>()];
return AddTemplate<TViewModel, TViewModelImplementation, TView>(services, key, serviceLifetime, parameters);