Added subscription to Mediator to allow later subscriptions

This commit is contained in:
Daniel Clark
2022-12-18 19:22:02 +00:00
parent 44883eaef7
commit 06ca2fbc8b
5 changed files with 50 additions and 26 deletions
@@ -11,4 +11,6 @@ public interface IMediator
ValueTask<TResponse> Send<TResponse>(IQuery<TResponse> query, CancellationToken cancellationToken = default);
ValueTask<object?> Send(object message, CancellationToken cancellationToken = default);
void Subscribe(object subscriber);
}
@@ -1,6 +1,4 @@
using System.Diagnostics;
namespace Toolkit.Framework.Foundation;
namespace Toolkit.Framework.Foundation;
public class Initialization : IInitialization
{
@@ -17,9 +15,7 @@ public class Initialization : IInitialization
{
if (initializer is not null)
{
Trace.WriteLine(initializer.GetType());
await initializer.InitializeAsync();
Trace.WriteLine("Done");
}
}
}
+35 -7
View File
@@ -1,10 +1,12 @@
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Concurrent;
namespace Toolkit.Framework.Foundation;
public class Mediator : IMediator
{
private readonly IServiceProvider factory;
private readonly ConcurrentDictionary<Type, HashSet<dynamic>> subscriptions = new();
public Mediator(IServiceProvider factory)
{
@@ -15,6 +17,11 @@ public class Mediator : IMediator
{
List<INotificationHandler<TNotification>> handlers = factory.GetServices<INotificationHandler<TNotification>>().ToList();
foreach (dynamic handler in subscriptions[typeof(TNotification)])
{
handlers.Add(handler);
}
if (handlers.Count == 0)
{
return default;
@@ -29,7 +36,7 @@ public class Mediator : IMediator
public ValueTask<TResponse> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default)
{
dynamic? handler = factory.GetService(typeof(RequestClassHandlerWrapper<,>).MakeGenericType(request.GetType(), typeof(TResponse)));
dynamic? handler = factory.GetService(typeof(RequestClassHandlerWrapper<,>).MakeGenericType(request.GetType(), typeof(TResponse)));
if (handler is not null)
{
return handler.Handle((dynamic)request, cancellationToken);
@@ -62,9 +69,9 @@ public class Mediator : IMediator
public ValueTask<object?> Send(object message, CancellationToken cancellationToken = default)
{
if (message.GetType().GetInterface(typeof(IRequest<>).Name) is { } requestInterface)
if (message.GetType().GetInterface(typeof(IRequest<>).Name) is { } requestType)
{
if (requestInterface.GetGenericArguments() is { Length: 1 } arguments)
if (requestType.GetGenericArguments() is { Length: 1 } arguments)
{
Type responseType = arguments[0];
@@ -76,9 +83,9 @@ public class Mediator : IMediator
}
}
if (message.GetType().GetInterface(typeof(ICommand<>).Name) is { } commandInterface)
if (message.GetType().GetInterface(typeof(ICommand<>).Name) is { } commandType)
{
if (commandInterface.GetGenericArguments() is { Length: 1 } arguments)
if (commandType.GetGenericArguments() is { Length: 1 } arguments)
{
Type responseType = arguments[0];
@@ -90,9 +97,9 @@ public class Mediator : IMediator
}
}
if (message.GetType().GetInterface(typeof(IQuery<>).Name) is { } queryInterface)
if (message.GetType().GetInterface(typeof(IQuery<>).Name) is { } queryType)
{
if (queryInterface.GetGenericArguments() is { Length: 1 } arguments)
if (queryType.GetGenericArguments() is { Length: 1 } arguments)
{
Type responseType = arguments[0];
@@ -106,4 +113,25 @@ public class Mediator : IMediator
return default;
}
public void Subscribe(object subscriber)
{
Type[] interfaceTypes = subscriber.GetType().GetInterfaces();
foreach (Type interfaceType in interfaceTypes.Where(x => x.IsGenericType))
{
if (interfaceType.GetGenericTypeDefinition() == typeof(INotificationHandler<>))
{
if (interfaceType.GetGenericArguments() is { Length: 1 } arguments)
{
Type notificationType = arguments[0];
subscriptions.AddOrUpdate(notificationType, new HashSet<dynamic> { subscriber }, (type, hashSet) =>
{
hashSet.Add(subscriber);
return hashSet;
});
}
}
}
}
}