Add validation

This commit is contained in:
TheXamlGuy
2024-07-07 20:09:03 +01:00
parent a4e77ef293
commit 807dc3c40e
2 changed files with 106 additions and 96 deletions
@@ -41,43 +41,43 @@ public static class IServiceCollectionExtensions
string? key = null) string? key = null)
where THandler : IHandler where THandler : IHandler
{ {
if (typeof(THandler).GetInterfaces() is Type[] contracts) if (typeof(THandler).GetInterfaces() is Type[] handlerTypes)
{ {
foreach (Type contract in contracts) foreach (Type handlerType in handlerTypes)
{ {
if (contract.Name == typeof(INotificationHandler<>).Name && if (handlerType.Name == typeof(INotificationHandler<>).Name &&
contract.GetGenericArguments() is { Length: 1 } notificationHandlerArguments) handlerType.GetGenericArguments() is { Length: 1 } notificationHandlerArguments)
{ {
Type notificationType = notificationHandlerArguments[0]; Type notificationType = notificationHandlerArguments[0];
Type wrapperType = typeof(NotificationHandlerWrapper<>).MakeGenericType(notificationType); Type wrapperType = typeof(NotificationHandlerWrapper<>).MakeGenericType(notificationType);
string actualKey = $"{(key is not null ? $"{key}:" : "")}{notificationType}"; string preferredKey = $"{(key is not null ? $"{key}:" : "")}{notificationType}";
services.Add(new ServiceDescriptor(typeof(INotificationHandler<>) services.Add(new ServiceDescriptor(typeof(INotificationHandler<>)
.MakeGenericType(notificationType), actualKey, typeof(THandler), lifetime)); .MakeGenericType(notificationType), preferredKey, typeof(THandler), lifetime));
services.Add(new ServiceDescriptor(wrapperType, actualKey, (provider, registeredKey) => services.Add(new ServiceDescriptor(wrapperType, preferredKey, (provider, registeredKey) =>
provider.GetService<IServiceFactory>()?.Create(wrapperType, provider.GetService<IServiceFactory>()?.Create(wrapperType,
provider.GetRequiredKeyedService(typeof(INotificationHandler<>).MakeGenericType(notificationType), registeredKey), provider.GetRequiredKeyedService(typeof(INotificationHandler<>).MakeGenericType(notificationType), registeredKey),
provider.GetServices(typeof(IPipelineBehaviour<>) provider.GetServices(typeof(IPipelineBehaviour<>)
.MakeGenericType(notificationType)))!, lifetime)); .MakeGenericType(notificationType)))!, lifetime));
} }
if (contract.Name == typeof(IHandler<,>).Name && if (handlerType.Name == typeof(IHandler<,>).Name &&
contract.GetGenericArguments() is { Length: 2 } handlerArguments) handlerType.GetGenericArguments() is { Length: 2 } handlerArguments)
{ {
Type requestType = handlerArguments[0]; Type requestType = handlerArguments[0];
Type responseType = handlerArguments[1]; Type responseType = handlerArguments[1];
Type wrapperType = typeof(HandlerWrapper<,>).MakeGenericType(requestType, responseType); Type wrapperType = typeof(HandlerWrapper<,>).MakeGenericType(requestType, responseType);
string actualKey = $"{(key is not null ? $"{key}:" : "")}{wrapperType}"; string preferredKey = $"{(key is not null ? $"{key}:" : "")}{wrapperType}";
services.Add(new ServiceDescriptor(typeof(THandler), actualKey, services.Add(new ServiceDescriptor(typeof(THandler), preferredKey,
typeof(THandler), lifetime)); typeof(THandler), lifetime));
services.Add(new ServiceDescriptor(wrapperType, actualKey, (provider, actualKey) => services.Add(new ServiceDescriptor(wrapperType, preferredKey, (provider, actualKey) =>
provider.GetService<IServiceFactory>()?.Create(wrapperType, provider.GetService<IServiceFactory>()?.Create(wrapperType,
provider.GetRequiredKeyedService<THandler>(actualKey), provider.GetRequiredKeyedService<THandler>(preferredKey),
provider.GetServices(typeof(IPipelineBehaviour<,>) provider.GetServices(typeof(IPipelineBehaviour<,>)
.MakeGenericType(requestType, responseType)))!, lifetime)); .MakeGenericType(requestType, responseType)))!, lifetime));
} }
+93 -83
View File
@@ -8,107 +8,89 @@ public class Subscriber(SubscriptionCollection subscriptions,
{ {
public void Subscribe(object subscriber) public void Subscribe(object subscriber)
{ {
Type handlerType = subscriber.GetType();
IDictionary<Type, List<object>> subscribers = GetSubscriptionKeys(subscriber); IDictionary<Type, List<object>> subscribers = GetSubscriptionKeys(subscriber);
foreach (Type interfaceType in GetHandlerInterfaces(handlerType))
foreach (Type handlerType in GetHandlerInterfaces(subscriber.GetType()))
{ {
if (interfaceType.GetGenericArguments().FirstOrDefault() is Type argumentType) if (handlerType.Name == typeof(INotificationHandler<>).Name &&
handlerType.GetGenericArguments() is { Length: 1 } notificationHandlerArguments)
{ {
subscribers.TryGetValue(argumentType, out List<object>? keys); Type notificationType = notificationHandlerArguments[0];
if (keys is not null) AddSubscriptions(subscriber, subscribers, notificationType);
{ }
foreach (object key in keys)
{
string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{argumentType}";
subscriptions.AddOrUpdate(subscriptionKey, _ => new List<WeakReference> { new(subscriber) }, (_, collection) =>
{
collection.Add(new WeakReference(subscriber));
return collection;
});
disposer.Add(subscriber, Disposable.Create(() => RemoveSubscriber(subscriber, subscriptionKey))); if (handlerType.Name == typeof(IHandler<,>).Name &&
} handlerType.GetGenericArguments() is { Length: 2 } handlerArguments)
} {
else Type requestType = handlerArguments[0];
{ Type responseType = handlerArguments[1];
string subscriptionKey = $"{argumentType}"; Type wrapperType = typeof(HandlerWrapper<,>).MakeGenericType(requestType, responseType);
subscriptions.AddOrUpdate(subscriptionKey, _ => new List<WeakReference> { new(subscriber) }, (_, collection) => AddSubscriptions(subscriber, subscribers, wrapperType);
{
collection.Add(new WeakReference(subscriber));
return collection;
});
disposer.Add(subscriber, Disposable.Create(() => RemoveSubscriber(subscriber, subscriptionKey)));
}
} }
} }
} }
public void Unsubscribe(object subscriber) public void Unsubscribe(object subscriber)
{ {
Type handlerType = subscriber.GetType();
IDictionary<Type, List<object>> subscribers = GetSubscriptionKeys(subscriber); IDictionary<Type, List<object>> subscribers = GetSubscriptionKeys(subscriber);
foreach (Type interfaceType in GetHandlerInterfaces(handlerType))
foreach (Type handlerType in GetHandlerInterfaces(subscriber.GetType()))
{ {
if (interfaceType.GetGenericArguments().FirstOrDefault() is Type argumentType) if (handlerType.Name == typeof(INotificationHandler<>).Name &&
handlerType.GetGenericArguments() is { Length: 1 } notificationHandlerArguments)
{ {
subscribers.TryGetValue(argumentType, out List<object>? keys); Type notificationType = notificationHandlerArguments[0];
if (keys is not null) RemoveSubscriptions(subscriber, subscribers, notificationType);
{ }
foreach (object key in keys)
{ if (handlerType.Name == typeof(IHandler<,>).Name &&
string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{argumentType}"; handlerType.GetGenericArguments() is { Length: 2 } handlerArguments)
if (subscriptions.TryGetValue(subscriptionKey, out List<WeakReference>? existing)) {
{ Type requestType = handlerArguments[0];
for (int i = existing.Count - 1; i >= 0; i--) Type responseType = handlerArguments[1];
{ Type wrapperType = typeof(HandlerWrapper<,>).MakeGenericType(requestType, responseType);
if (!existing[i].IsAlive || existing[i].Target == subscriber) RemoveSubscriptions(subscriber, subscribers, wrapperType);
{
existing.RemoveAt(i);
}
}
}
}
}
else
{
string subscriptionKey = $"{argumentType}";
if (subscriptions.TryGetValue(subscriptionKey, out List<WeakReference>? existing))
{
for (int i = existing.Count - 1; i >= 0; i--)
{
if (!existing[i].IsAlive || existing[i].Target == subscriber)
{
existing.RemoveAt(i);
}
}
}
}
} }
} }
} }
private void RemoveSubscriber(object subscriber, private void AddOrUpdateSubscription(object subscriber, string preferredKey)
string key)
{ {
if (subscriptions.TryGetValue(key, out List<WeakReference>? subscribers)) subscriptions.AddOrUpdate(preferredKey, _ => new List<WeakReference> { new(subscriber) }, (_, collection) =>
{ {
for (int i = subscribers.Count - 1; i >= 0; i--) collection.Add(new WeakReference(subscriber));
{ return collection;
if (subscribers[i].Target == subscriber) });
{
subscribers.RemoveAt(i);
}
}
if (subscribers.Count == 0) disposer.Add(subscriber, Disposable.Create(() => RemoveSubscription(subscriber, preferredKey)));
}
private void AddSubscriptions(object subscriber, IDictionary<Type, List<object>> subscribers, Type handlerType)
{
if (subscribers.TryGetValue(handlerType, out List<object>? keys))
{
foreach (object key in keys)
{ {
subscriptions.TryRemove(key, out _); string preferredKey = $"{(key is not null ? $"{key}:" : "")}{handlerType}";
AddOrUpdateSubscription(subscriber, preferredKey);
} }
} }
else
{
string preferredKey = $"{handlerType}";
AddOrUpdateSubscription(subscriber, preferredKey);
}
} }
private IEnumerable<Type> GetHandlerInterfaces(Type handlerType) =>
handlerType.GetInterfaces().Where(interfaceType =>
{
Type? definition = interfaceType.IsGenericType ? interfaceType.GetGenericTypeDefinition() : null;
return definition == typeof(INotificationHandler<>) ||
definition == typeof(IHandler<>) ||
definition == typeof(IHandler<,>);
});
private IDictionary<Type, List<object>> GetSubscriptionKeys(object subscriber) private IDictionary<Type, List<object>> GetSubscriptionKeys(object subscriber)
{ {
Dictionary<Type, List<object>> keys = []; Dictionary<Type, List<object>> keys = [];
@@ -133,12 +115,40 @@ public class Subscriber(SubscriptionCollection subscriptions,
return keys; return keys;
} }
private IEnumerable<Type> GetHandlerInterfaces(Type handlerType) => private void RemoveSubscription(object subscriber,
handlerType.GetInterfaces().Where(interfaceType => string key)
{
if (subscriptions.TryGetValue(key, out List<WeakReference>? subscribers))
{ {
Type? definition = interfaceType.IsGenericType ? interfaceType.GetGenericTypeDefinition() : null; for (int i = subscribers.Count - 1; i >= 0; i--)
return definition == typeof(INotificationHandler<>) || {
definition == typeof(IHandler<>) || if (subscribers[i].Target == subscriber)
definition == typeof(IHandler<,>); {
}); subscribers.RemoveAt(i);
}
}
if (subscribers.Count == 0)
{
subscriptions.TryRemove(key, out _);
}
}
}
private void RemoveSubscriptions(object subscriber, IDictionary<Type, List<object>> subscribers, Type handlerType)
{
if (subscribers.TryGetValue(handlerType, out List<object>? keys))
{
foreach (object key in keys)
{
string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{handlerType}";
RemoveSubscription(subscriber, subscriptionKey);
}
}
else
{
string subscriptionKey = $"{handlerType}";
RemoveSubscription(subscriber, subscriptionKey);
}
}
} }