From 2b0622ca476d7afeead72a5d48ecfdccf96ee098 Mon Sep 17 00:00:00 2001 From: litenova Date: Fri, 8 Dec 2023 12:13:49 +0100 Subject: [PATCH] refactor handler descriptors --- .../Descriptors/IDescriptor.cs | 8 ----- .../Descriptors/IErrorHandlerDescriptor.cs | 12 ------- .../Descriptors/IHandlerDescriptor.cs | 14 -------- .../Descriptors/IPostHandlerDescriptor.cs | 14 -------- .../Descriptors/IPreHandlerDescriptor.cs | 12 ------- .../ILazyHandlerCollection.cs | 2 +- .../IMessageDependencies.cs | 4 +-- ....Messaging.Abstractions.csproj.DotSettings | 4 ++- .../Descriptors/IErrorHandlerDescriptor.cs | 9 ++++++ .../Descriptors/IHandlerDescriptor.cs | 32 +++++++++++++++++++ .../Descriptors/IMainHandlerDescriptor.cs | 15 +++++++++ .../Descriptors/IMessageDescriptor.cs | 11 ++++--- .../Descriptors/IPostHandlerDescriptor.cs | 15 +++++++++ .../Descriptors/IPreHandlerDescriptor.cs | 9 ++++++ .../{ => Registry}/IMessageRegistry.cs | 0 .../ModuleRegistry.cs | 6 ++-- .../Extensions/EnumerableExtensions.cs | 1 + .../Internal/LazyReadOnlyCollection[T].cs | 2 +- .../Internal/Mediator/MessageDependencies.cs | 27 ++++------------ .../IDescriptorBuilder[TDescriptor].cs | 15 --------- ...uilder.cs => IHandlerDescriptorBuilder.cs} | 4 +-- .../Builders/ErrorHandlerDescriptorBuilder.cs | 15 +++++++-- .../Builders/HandlerDescriptorBuilder.cs | 11 +++++-- .../Builders/PostHandlerDescriptorBuilder.cs | 13 ++++++-- .../Builders/PreHandlerDescriptorBuilder.cs | 12 +++++-- .../Descriptors/ErrorHandlerDescriptor.cs | 18 ++--------- .../Registry/Descriptors/HandlerDescriptor.cs | 26 --------------- .../Descriptors/HandlerDescriptorBase.cs | 16 ++++++++++ .../Descriptors/MainHandlerDescriptor.cs | 9 ++++++ .../Registry/Descriptors/MessageDescriptor.cs | 22 ++++++------- .../Descriptors/PostHandlerDescriptor.cs | 21 ++---------- .../Descriptors/PreHandlerDescriptor.cs | 20 ++---------- .../Internal/Registry/MessageRegistry.cs | 4 +-- 33 files changed, 191 insertions(+), 212 deletions(-) delete mode 100644 src/LiteBus.Messaging.Abstractions/Descriptors/IDescriptor.cs delete mode 100644 src/LiteBus.Messaging.Abstractions/Descriptors/IErrorHandlerDescriptor.cs delete mode 100644 src/LiteBus.Messaging.Abstractions/Descriptors/IHandlerDescriptor.cs delete mode 100644 src/LiteBus.Messaging.Abstractions/Descriptors/IPostHandlerDescriptor.cs delete mode 100644 src/LiteBus.Messaging.Abstractions/Descriptors/IPreHandlerDescriptor.cs create mode 100644 src/LiteBus.Messaging.Abstractions/Registry/Descriptors/IErrorHandlerDescriptor.cs create mode 100644 src/LiteBus.Messaging.Abstractions/Registry/Descriptors/IHandlerDescriptor.cs create mode 100644 src/LiteBus.Messaging.Abstractions/Registry/Descriptors/IMainHandlerDescriptor.cs rename src/LiteBus.Messaging.Abstractions/{ => Registry}/Descriptors/IMessageDescriptor.cs (67%) create mode 100644 src/LiteBus.Messaging.Abstractions/Registry/Descriptors/IPostHandlerDescriptor.cs create mode 100644 src/LiteBus.Messaging.Abstractions/Registry/Descriptors/IPreHandlerDescriptor.cs rename src/LiteBus.Messaging.Abstractions/{ => Registry}/IMessageRegistry.cs (100%) delete mode 100644 src/LiteBus.Messaging/Internal/Registry/Abstractions/IDescriptorBuilder[TDescriptor].cs rename src/LiteBus.Messaging/Internal/Registry/Abstractions/{IDescriptorBuilder.cs => IHandlerDescriptorBuilder.cs} (64%) delete mode 100644 src/LiteBus.Messaging/Internal/Registry/Descriptors/HandlerDescriptor.cs create mode 100644 src/LiteBus.Messaging/Internal/Registry/Descriptors/HandlerDescriptorBase.cs create mode 100644 src/LiteBus.Messaging/Internal/Registry/Descriptors/MainHandlerDescriptor.cs diff --git a/src/LiteBus.Messaging.Abstractions/Descriptors/IDescriptor.cs b/src/LiteBus.Messaging.Abstractions/Descriptors/IDescriptor.cs deleted file mode 100644 index d09243d..0000000 --- a/src/LiteBus.Messaging.Abstractions/Descriptors/IDescriptor.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System; - -namespace LiteBus.Messaging.Abstractions; - -public interface IDescriptor -{ - Type MessageType { get; } -} \ No newline at end of file diff --git a/src/LiteBus.Messaging.Abstractions/Descriptors/IErrorHandlerDescriptor.cs b/src/LiteBus.Messaging.Abstractions/Descriptors/IErrorHandlerDescriptor.cs deleted file mode 100644 index 1b516e5..0000000 --- a/src/LiteBus.Messaging.Abstractions/Descriptors/IErrorHandlerDescriptor.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace LiteBus.Messaging.Abstractions; - -public interface IErrorHandlerDescriptor : IDescriptor -{ - Type ErrorHandlerType { get; } - - int Order { get; } - - bool IsGeneric { get; set; } -} \ No newline at end of file diff --git a/src/LiteBus.Messaging.Abstractions/Descriptors/IHandlerDescriptor.cs b/src/LiteBus.Messaging.Abstractions/Descriptors/IHandlerDescriptor.cs deleted file mode 100644 index 9b26a78..0000000 --- a/src/LiteBus.Messaging.Abstractions/Descriptors/IHandlerDescriptor.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; - -namespace LiteBus.Messaging.Abstractions; - -public interface IHandlerDescriptor : IDescriptor -{ - Type HandlerType { get; } - - Type MessageResultType { get; } - - bool IsGeneric { get; } - - int Order { get; } -} \ No newline at end of file diff --git a/src/LiteBus.Messaging.Abstractions/Descriptors/IPostHandlerDescriptor.cs b/src/LiteBus.Messaging.Abstractions/Descriptors/IPostHandlerDescriptor.cs deleted file mode 100644 index 9524e44..0000000 --- a/src/LiteBus.Messaging.Abstractions/Descriptors/IPostHandlerDescriptor.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; - -namespace LiteBus.Messaging.Abstractions; - -public interface IPostHandlerDescriptor : IDescriptor -{ - Type PostHandlerType { get; } - - int Order { get; } - - Type MessageResultType { get; } - - bool IsGeneric { get; } -} \ No newline at end of file diff --git a/src/LiteBus.Messaging.Abstractions/Descriptors/IPreHandlerDescriptor.cs b/src/LiteBus.Messaging.Abstractions/Descriptors/IPreHandlerDescriptor.cs deleted file mode 100644 index 22a25dc..0000000 --- a/src/LiteBus.Messaging.Abstractions/Descriptors/IPreHandlerDescriptor.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace LiteBus.Messaging.Abstractions; - -public interface IPreHandlerDescriptor : IDescriptor -{ - Type PreHandlerType { get; } - - int Order { get; } - - bool IsGeneric { get; } -} \ No newline at end of file diff --git a/src/LiteBus.Messaging.Abstractions/ILazyHandlerCollection.cs b/src/LiteBus.Messaging.Abstractions/ILazyHandlerCollection.cs index b3f825b..625b61b 100644 --- a/src/LiteBus.Messaging.Abstractions/ILazyHandlerCollection.cs +++ b/src/LiteBus.Messaging.Abstractions/ILazyHandlerCollection.cs @@ -2,6 +2,6 @@ namespace LiteBus.Messaging.Abstractions; -public interface ILazyHandlerCollection : IReadOnlyCollection> +public interface ILazyHandlerCollection : IReadOnlyCollection> where TDescriptor : IHandlerDescriptor { } \ No newline at end of file diff --git a/src/LiteBus.Messaging.Abstractions/IMessageDependencies.cs b/src/LiteBus.Messaging.Abstractions/IMessageDependencies.cs index 17f136c..a1cf307 100644 --- a/src/LiteBus.Messaging.Abstractions/IMessageDependencies.cs +++ b/src/LiteBus.Messaging.Abstractions/IMessageDependencies.cs @@ -11,7 +11,7 @@ public interface IMessageDependencies /// /// The collection of direct message handlers. /// - ILazyHandlerCollection Handlers { get; } + ILazyHandlerCollection Handlers { get; } /// /// Gets a lazy initialized read-only collection of indirect message handlers. These handlers can be used to handle messages in a more general manner, potentially handling a variety of different message types or performing logging or other cross-cutting concerns. @@ -19,7 +19,7 @@ public interface IMessageDependencies /// /// The collection of indirect message handlers. /// - ILazyHandlerCollection IndirectHandlers { get; } + ILazyHandlerCollection IndirectHandlers { get; } /// /// Gets a lazy initialized read-only collection of direct pre-message handlers. These handlers are invoked before the primary message handlers and can be used for tasks such as validation or logging. diff --git a/src/LiteBus.Messaging.Abstractions/LiteBus.Messaging.Abstractions.csproj.DotSettings b/src/LiteBus.Messaging.Abstractions/LiteBus.Messaging.Abstractions.csproj.DotSettings index 0f3e8f7..066b52d 100644 --- a/src/LiteBus.Messaging.Abstractions/LiteBus.Messaging.Abstractions.csproj.DotSettings +++ b/src/LiteBus.Messaging.Abstractions/LiteBus.Messaging.Abstractions.csproj.DotSettings @@ -14,4 +14,6 @@ True True True - True \ No newline at end of file + True + True + True \ No newline at end of file diff --git a/src/LiteBus.Messaging.Abstractions/Registry/Descriptors/IErrorHandlerDescriptor.cs b/src/LiteBus.Messaging.Abstractions/Registry/Descriptors/IErrorHandlerDescriptor.cs new file mode 100644 index 0000000..4a7b46e --- /dev/null +++ b/src/LiteBus.Messaging.Abstractions/Registry/Descriptors/IErrorHandlerDescriptor.cs @@ -0,0 +1,9 @@ +namespace LiteBus.Messaging.Abstractions; + +/// +/// Represents a descriptor for an error handler, providing metadata about the handler such as the message type it handles, +/// its execution order, and any associated tags. +/// +public interface IErrorHandlerDescriptor : IHandlerDescriptor +{ +} \ No newline at end of file diff --git a/src/LiteBus.Messaging.Abstractions/Registry/Descriptors/IHandlerDescriptor.cs b/src/LiteBus.Messaging.Abstractions/Registry/Descriptors/IHandlerDescriptor.cs new file mode 100644 index 0000000..30b547c --- /dev/null +++ b/src/LiteBus.Messaging.Abstractions/Registry/Descriptors/IHandlerDescriptor.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; + +namespace LiteBus.Messaging.Abstractions; + +/// +/// Represents a descriptor for a handler, providing metadata about the handler such as the message type it handles, +/// its execution order, and any associated tags. +/// +public interface IHandlerDescriptor +{ + /// + /// Gets the type of the message that the handler is associated with. If the message type is generic, + /// this property returns the generic type definition. + /// + Type MessageType { get; } + + /// + /// Gets the order in which the handler should be executed. Handlers with lower order values are executed first. + /// + int Order { get; } + + /// + /// Gets a collection of tags associated with the handler. Tags can be used to categorize or identify handlers in a flexible way. + /// + IReadOnlyCollection Tags { get; } + + /// + /// Gets the type of the handler. This represents the actual implementation type of the handler. + /// + Type HandlerType { get; } +} \ No newline at end of file diff --git a/src/LiteBus.Messaging.Abstractions/Registry/Descriptors/IMainHandlerDescriptor.cs b/src/LiteBus.Messaging.Abstractions/Registry/Descriptors/IMainHandlerDescriptor.cs new file mode 100644 index 0000000..d58b517 --- /dev/null +++ b/src/LiteBus.Messaging.Abstractions/Registry/Descriptors/IMainHandlerDescriptor.cs @@ -0,0 +1,15 @@ +using System; + +namespace LiteBus.Messaging.Abstractions; + +/// +/// Represents a descriptor for a main handler, providing metadata about the handler such as the message type it handles, +/// its execution order, any associated tags, and the type of the result produced by the handler. +/// +public interface IMainHandlerDescriptor : IHandlerDescriptor +{ + /// + /// Gets the type of the result produced by the main handler. + /// + Type MessageResultType { get; } +} diff --git a/src/LiteBus.Messaging.Abstractions/Descriptors/IMessageDescriptor.cs b/src/LiteBus.Messaging.Abstractions/Registry/Descriptors/IMessageDescriptor.cs similarity index 67% rename from src/LiteBus.Messaging.Abstractions/Descriptors/IMessageDescriptor.cs rename to src/LiteBus.Messaging.Abstractions/Registry/Descriptors/IMessageDescriptor.cs index 4293555..20d3dd7 100644 --- a/src/LiteBus.Messaging.Abstractions/Descriptors/IMessageDescriptor.cs +++ b/src/LiteBus.Messaging.Abstractions/Registry/Descriptors/IMessageDescriptor.cs @@ -1,14 +1,17 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; namespace LiteBus.Messaging.Abstractions; -public interface IMessageDescriptor : IDescriptor +public interface IMessageDescriptor { + Type MessageType { get; } + bool IsGeneric { get; } - IReadOnlyCollection Handlers { get; } + IReadOnlyCollection Handlers { get; } - IReadOnlyCollection IndirectHandlers { get; } + IReadOnlyCollection IndirectHandlers { get; } IReadOnlyCollection PostHandlers { get; } diff --git a/src/LiteBus.Messaging.Abstractions/Registry/Descriptors/IPostHandlerDescriptor.cs b/src/LiteBus.Messaging.Abstractions/Registry/Descriptors/IPostHandlerDescriptor.cs new file mode 100644 index 0000000..d43b587 --- /dev/null +++ b/src/LiteBus.Messaging.Abstractions/Registry/Descriptors/IPostHandlerDescriptor.cs @@ -0,0 +1,15 @@ +using System; + +namespace LiteBus.Messaging.Abstractions; + +/// +/// Represents a descriptor for a post-handler, providing metadata about the handler such as the message type it handles, +/// its execution order, any associated tags, and the type of the result that is expected by the user as an argument. +/// +public interface IPostHandlerDescriptor : IHandlerDescriptor +{ + /// + /// Gets the type of the result produced by the main handler that is associated with this post-handler. + /// + Type MessageResultType { get; } +} \ No newline at end of file diff --git a/src/LiteBus.Messaging.Abstractions/Registry/Descriptors/IPreHandlerDescriptor.cs b/src/LiteBus.Messaging.Abstractions/Registry/Descriptors/IPreHandlerDescriptor.cs new file mode 100644 index 0000000..b7568b9 --- /dev/null +++ b/src/LiteBus.Messaging.Abstractions/Registry/Descriptors/IPreHandlerDescriptor.cs @@ -0,0 +1,9 @@ +namespace LiteBus.Messaging.Abstractions; + +/// +/// Represents a descriptor for a pre-handler, providing metadata about the handler such as the message type it handles, +/// its execution order, and any associated tags. +/// +public interface IPreHandlerDescriptor : IHandlerDescriptor +{ +} \ No newline at end of file diff --git a/src/LiteBus.Messaging.Abstractions/IMessageRegistry.cs b/src/LiteBus.Messaging.Abstractions/Registry/IMessageRegistry.cs similarity index 100% rename from src/LiteBus.Messaging.Abstractions/IMessageRegistry.cs rename to src/LiteBus.Messaging.Abstractions/Registry/IMessageRegistry.cs diff --git a/src/LiteBus.Messaging.Extensions.MicrosoftDependencyInjection/ModuleRegistry.cs b/src/LiteBus.Messaging.Extensions.MicrosoftDependencyInjection/ModuleRegistry.cs index d087945..8b582ca 100644 --- a/src/LiteBus.Messaging.Extensions.MicrosoftDependencyInjection/ModuleRegistry.cs +++ b/src/LiteBus.Messaging.Extensions.MicrosoftDependencyInjection/ModuleRegistry.cs @@ -63,17 +63,17 @@ public void Initialize() foreach (var postHandleDescriptor in descriptor.PostHandlers.Concat(descriptor.IndirectPostHandlers)) { - _services.TryAddTransient(postHandleDescriptor.PostHandlerType); + _services.TryAddTransient(postHandleDescriptor.HandlerType); } foreach (var preHandleDescriptor in descriptor.PreHandlers.Concat(descriptor.IndirectPreHandlers)) { - _services.TryAddTransient(preHandleDescriptor.PreHandlerType); + _services.TryAddTransient(preHandleDescriptor.HandlerType); } foreach (var errorHandlerDescriptor in descriptor.ErrorHandlers.Concat(descriptor.IndirectErrorHandlers)) { - _services.TryAddTransient(errorHandlerDescriptor.ErrorHandlerType); + _services.TryAddTransient(errorHandlerDescriptor.HandlerType); } } } diff --git a/src/LiteBus.Messaging/Internal/Extensions/EnumerableExtensions.cs b/src/LiteBus.Messaging/Internal/Extensions/EnumerableExtensions.cs index 250fdf8..21a0893 100644 --- a/src/LiteBus.Messaging/Internal/Extensions/EnumerableExtensions.cs +++ b/src/LiteBus.Messaging/Internal/Extensions/EnumerableExtensions.cs @@ -6,6 +6,7 @@ namespace LiteBus.Messaging.Internal.Extensions; public static class EnumerableExtensions { public static ILazyHandlerCollection ToLazyReadOnlyCollection(this IEnumerable> source) + where TDescriptor : IHandlerDescriptor { return new LazyHandlerCollection(source); } diff --git a/src/LiteBus.Messaging/Internal/LazyReadOnlyCollection[T].cs b/src/LiteBus.Messaging/Internal/LazyReadOnlyCollection[T].cs index ffb5574..c268903 100644 --- a/src/LiteBus.Messaging/Internal/LazyReadOnlyCollection[T].cs +++ b/src/LiteBus.Messaging/Internal/LazyReadOnlyCollection[T].cs @@ -4,7 +4,7 @@ namespace LiteBus.Messaging.Internal; -public sealed class LazyHandlerCollection : ILazyHandlerCollection +public sealed class LazyHandlerCollection : ILazyHandlerCollection where TDescriptor : IHandlerDescriptor { private readonly List> _list; diff --git a/src/LiteBus.Messaging/Internal/Mediator/MessageDependencies.cs b/src/LiteBus.Messaging/Internal/Mediator/MessageDependencies.cs index c83637d..e74394c 100644 --- a/src/LiteBus.Messaging/Internal/Mediator/MessageDependencies.cs +++ b/src/LiteBus.Messaging/Internal/Mediator/MessageDependencies.cs @@ -28,9 +28,9 @@ public MessageDependencies(Type messageType, IMessageDescriptor descriptor, ISer IndirectErrorHandlers = ResolveHandlers(descriptor.IndirectErrorHandlers, (handlerType) => (IMessageErrorHandler) serviceProvider.GetService(handlerType)); } - public ILazyHandlerCollection Handlers { get; } + public ILazyHandlerCollection Handlers { get; } - public ILazyHandlerCollection IndirectHandlers { get; } + public ILazyHandlerCollection IndirectHandlers { get; } public ILazyHandlerCollection PreHandlers { get; } @@ -49,10 +49,10 @@ public MessageDependencies(Type messageType, IMessageDescriptor descriptor, ISer /// private ILazyHandlerCollection ResolveHandlers( IEnumerable descriptors, - Func resolveFunc) + Func resolveFunc) where TDescriptor : IHandlerDescriptor { return descriptors - .OrderBy(d => GetOrder(d)) + .OrderBy(d => d.Order) .Select(d => new LazyHandler { Handler = new Lazy(() => resolveFunc(GetHandlerType(d))), @@ -61,27 +61,14 @@ private ILazyHandlerCollection ResolveHandlers - /// Retrieves the order value from a descriptor. - /// - private static int GetOrder(object descriptor) - { - return (int) descriptor.GetType().GetProperty("Order").GetValue(descriptor); - } - /// /// Retrieves the handler type from a descriptor, adjusting for generic types as necessary. /// - private Type GetHandlerType(object descriptor) + private Type GetHandlerType(IHandlerDescriptor descriptor) { - var handlerTypeProp = descriptor.GetType().GetProperty("HandlerType") ?? - descriptor.GetType().GetProperty($"{descriptor.GetType().Name.Replace("Descriptor", string.Empty)}Type"); - - var isGenericProp = descriptor.GetType().GetProperty("IsGeneric"); - - var handlerType = (Type) handlerTypeProp.GetValue(descriptor); + var handlerType = descriptor.HandlerType; - if ((bool) isGenericProp.GetValue(descriptor)) + if (descriptor.MessageType.IsGenericType) { handlerType = handlerType.MakeGenericType(_messageType.GetGenericArguments()); } diff --git a/src/LiteBus.Messaging/Internal/Registry/Abstractions/IDescriptorBuilder[TDescriptor].cs b/src/LiteBus.Messaging/Internal/Registry/Abstractions/IDescriptorBuilder[TDescriptor].cs deleted file mode 100644 index 76acbed..0000000 --- a/src/LiteBus.Messaging/Internal/Registry/Abstractions/IDescriptorBuilder[TDescriptor].cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using LiteBus.Messaging.Abstractions; - -namespace LiteBus.Messaging.Internal.Registry.Abstractions; - -internal interface IDescriptorBuilder : IDescriptorBuilder where TDescriptor : IDescriptor -{ - IEnumerable IDescriptorBuilder.Build(Type type) - { - return (IEnumerable) Build(type); - } - - new IEnumerable Build(Type type); -} \ No newline at end of file diff --git a/src/LiteBus.Messaging/Internal/Registry/Abstractions/IDescriptorBuilder.cs b/src/LiteBus.Messaging/Internal/Registry/Abstractions/IHandlerDescriptorBuilder.cs similarity index 64% rename from src/LiteBus.Messaging/Internal/Registry/Abstractions/IDescriptorBuilder.cs rename to src/LiteBus.Messaging/Internal/Registry/Abstractions/IHandlerDescriptorBuilder.cs index 6b67ab3..30ef637 100644 --- a/src/LiteBus.Messaging/Internal/Registry/Abstractions/IDescriptorBuilder.cs +++ b/src/LiteBus.Messaging/Internal/Registry/Abstractions/IHandlerDescriptorBuilder.cs @@ -4,9 +4,9 @@ namespace LiteBus.Messaging.Internal.Registry.Abstractions; -internal interface IDescriptorBuilder +internal interface IHandlerDescriptorBuilder { bool CanBuild(Type type); - IEnumerable Build(Type type); + IEnumerable Build(Type type); } \ No newline at end of file diff --git a/src/LiteBus.Messaging/Internal/Registry/Builders/ErrorHandlerDescriptorBuilder.cs b/src/LiteBus.Messaging/Internal/Registry/Builders/ErrorHandlerDescriptorBuilder.cs index 498d729..3161d68 100644 --- a/src/LiteBus.Messaging/Internal/Registry/Builders/ErrorHandlerDescriptorBuilder.cs +++ b/src/LiteBus.Messaging/Internal/Registry/Builders/ErrorHandlerDescriptorBuilder.cs @@ -7,14 +7,14 @@ namespace LiteBus.Messaging.Internal.Registry.Builders; -internal sealed class ErrorHandlerDescriptorBuilder : IDescriptorBuilder +internal sealed class ErrorHandlerDescriptorBuilder : IHandlerDescriptorBuilder { public bool CanBuild(Type type) { return type.IsAssignableTo(typeof(IMessageErrorHandler)); } - public IEnumerable Build(Type handlerType) + public IEnumerable Build(Type handlerType) { var interfaces = handlerType.GetInterfacesEqualTo(typeof(IMessageErrorHandler<,>)); var order = handlerType.GetOrderFromAttribute(); @@ -22,7 +22,16 @@ public IEnumerable Build(Type handlerType) foreach (var @interface in interfaces) { var messageType = @interface.GetGenericArguments()[0]; - yield return new ErrorHandlerDescriptor(handlerType, messageType, order); + var messageResultType = @interface.GetGenericArguments()[1]; + + yield return new ErrorHandlerDescriptor + { + MessageType = messageType.IsGenericType ? messageType.GetGenericTypeDefinition() : messageType, + MessageResultType = messageResultType, + Order = order, + Tags = ArraySegment.Empty, + HandlerType = handlerType, + }; } } } \ No newline at end of file diff --git a/src/LiteBus.Messaging/Internal/Registry/Builders/HandlerDescriptorBuilder.cs b/src/LiteBus.Messaging/Internal/Registry/Builders/HandlerDescriptorBuilder.cs index 08147c5..056169a 100644 --- a/src/LiteBus.Messaging/Internal/Registry/Builders/HandlerDescriptorBuilder.cs +++ b/src/LiteBus.Messaging/Internal/Registry/Builders/HandlerDescriptorBuilder.cs @@ -7,7 +7,7 @@ namespace LiteBus.Messaging.Internal.Registry.Builders; -public sealed class HandlerDescriptorBuilder : IDescriptorBuilder +public sealed class HandlerDescriptorBuilder : IHandlerDescriptorBuilder { public bool CanBuild(Type type) { @@ -25,7 +25,14 @@ public IEnumerable Build(Type handlerType) var messageType = @interface.GetGenericArguments()[0]; var messageResultType = @interface.GetGenericArguments()[1]; - yield return new HandlerDescriptor(handlerType, messageType, messageResultType, order); + yield return new MainHandlerDescriptor + { + MessageType = messageType.IsGenericType ? messageType.GetGenericTypeDefinition() : messageType, + MessageResultType = messageResultType, + Order = order, + Tags = ArraySegment.Empty, + HandlerType = handlerType + }; } } } \ No newline at end of file diff --git a/src/LiteBus.Messaging/Internal/Registry/Builders/PostHandlerDescriptorBuilder.cs b/src/LiteBus.Messaging/Internal/Registry/Builders/PostHandlerDescriptorBuilder.cs index d8d3812..a1d26d0 100644 --- a/src/LiteBus.Messaging/Internal/Registry/Builders/PostHandlerDescriptorBuilder.cs +++ b/src/LiteBus.Messaging/Internal/Registry/Builders/PostHandlerDescriptorBuilder.cs @@ -7,14 +7,14 @@ namespace LiteBus.Messaging.Internal.Registry.Builders; -public sealed class PostHandlerDescriptorBuilder : IDescriptorBuilder +public sealed class PostHandlerDescriptorBuilder : IHandlerDescriptorBuilder { public bool CanBuild(Type type) { return type.IsAssignableTo(typeof(IMessagePostHandler)); } - public IEnumerable Build(Type handlerType) + public IEnumerable Build(Type handlerType) { var interfaces = handlerType.GetInterfacesEqualTo(typeof(IMessagePostHandler<,>)); var order = handlerType.GetOrderFromAttribute(); @@ -24,7 +24,14 @@ public IEnumerable Build(Type handlerType) var messageType = @interface.GetGenericArguments()[0]; var messageResultType = @interface.GetGenericArguments()[1]; - yield return new PostHandlerDescriptor(handlerType, messageType, messageResultType, order); + yield return new PostHandlerDescriptor + { + MessageType = messageType.IsGenericType ? messageType.GetGenericTypeDefinition() : messageType, + MessageResultType = messageResultType, + Order = order, + Tags = ArraySegment.Empty, + HandlerType = handlerType + }; } } } \ No newline at end of file diff --git a/src/LiteBus.Messaging/Internal/Registry/Builders/PreHandlerDescriptorBuilder.cs b/src/LiteBus.Messaging/Internal/Registry/Builders/PreHandlerDescriptorBuilder.cs index 69feda6..1e0ab75 100644 --- a/src/LiteBus.Messaging/Internal/Registry/Builders/PreHandlerDescriptorBuilder.cs +++ b/src/LiteBus.Messaging/Internal/Registry/Builders/PreHandlerDescriptorBuilder.cs @@ -7,14 +7,14 @@ namespace LiteBus.Messaging.Internal.Registry.Builders; -public sealed class PreHandlerDescriptorBuilder : IDescriptorBuilder +public sealed class PreHandlerDescriptorBuilder : IHandlerDescriptorBuilder { public bool CanBuild(Type type) { return type.IsAssignableTo(typeof(IMessagePreHandler)); } - public IEnumerable Build(Type handlerType) + public IEnumerable Build(Type handlerType) { var interfaces = handlerType.GetInterfacesEqualTo(typeof(IMessagePreHandler<>)); var order = handlerType.GetOrderFromAttribute(); @@ -23,7 +23,13 @@ public IEnumerable Build(Type handlerType) { var messageType = @interface.GetGenericArguments()[0]; - yield return new PreHandlerDescriptor(handlerType, messageType, order); + yield return new PreHandlerDescriptor + { + MessageType = messageType.IsGenericType ? messageType.GetGenericTypeDefinition() : messageType, + Order = order, + Tags = ArraySegment.Empty, + HandlerType = handlerType + }; } } } \ No newline at end of file diff --git a/src/LiteBus.Messaging/Internal/Registry/Descriptors/ErrorHandlerDescriptor.cs b/src/LiteBus.Messaging/Internal/Registry/Descriptors/ErrorHandlerDescriptor.cs index 617ee3d..a048c11 100644 --- a/src/LiteBus.Messaging/Internal/Registry/Descriptors/ErrorHandlerDescriptor.cs +++ b/src/LiteBus.Messaging/Internal/Registry/Descriptors/ErrorHandlerDescriptor.cs @@ -3,21 +3,7 @@ namespace LiteBus.Messaging.Internal.Registry.Descriptors; -internal sealed class ErrorHandlerDescriptor : IErrorHandlerDescriptor +internal sealed class ErrorHandlerDescriptor : HandlerDescriptorBase, IErrorHandlerDescriptor { - public ErrorHandlerDescriptor(Type errorHandlerType, Type messageType, int order) - { - ErrorHandlerType = errorHandlerType; - Order = order; - IsGeneric = messageType.IsGenericType; - MessageType = IsGeneric ? messageType.GetGenericTypeDefinition() : messageType; - } - - public Type ErrorHandlerType { get; } - - public int Order { get; } - - public bool IsGeneric { get; set; } - - public Type MessageType { get; } + public required Type MessageResultType { get; init; } } \ No newline at end of file diff --git a/src/LiteBus.Messaging/Internal/Registry/Descriptors/HandlerDescriptor.cs b/src/LiteBus.Messaging/Internal/Registry/Descriptors/HandlerDescriptor.cs deleted file mode 100644 index ecee4fc..0000000 --- a/src/LiteBus.Messaging/Internal/Registry/Descriptors/HandlerDescriptor.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using LiteBus.Messaging.Abstractions; - -namespace LiteBus.Messaging.Internal.Registry.Descriptors; - -internal class HandlerDescriptor : IHandlerDescriptor -{ - public HandlerDescriptor(Type handlerType, Type messageType, Type messageResultType, int order) - { - HandlerType = handlerType; - IsGeneric = messageType.IsGenericType; - MessageType = IsGeneric ? messageType.GetGenericTypeDefinition() : messageType; - MessageResultType = messageResultType; - Order = order; - } - - public int Order { get; } - - public Type HandlerType { get; } - - public Type MessageType { get; } - - public Type MessageResultType { get; } - - public bool IsGeneric { get; } -} \ No newline at end of file diff --git a/src/LiteBus.Messaging/Internal/Registry/Descriptors/HandlerDescriptorBase.cs b/src/LiteBus.Messaging/Internal/Registry/Descriptors/HandlerDescriptorBase.cs new file mode 100644 index 0000000..f231974 --- /dev/null +++ b/src/LiteBus.Messaging/Internal/Registry/Descriptors/HandlerDescriptorBase.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using LiteBus.Messaging.Abstractions; + +namespace LiteBus.Messaging.Internal.Registry.Descriptors; + +internal abstract class HandlerDescriptorBase : IHandlerDescriptor +{ + public required Type MessageType { get; init; } + + public required int Order { get; init; } + + public required IReadOnlyCollection Tags { get; init; } + + public required Type HandlerType { get; init; } +} \ No newline at end of file diff --git a/src/LiteBus.Messaging/Internal/Registry/Descriptors/MainHandlerDescriptor.cs b/src/LiteBus.Messaging/Internal/Registry/Descriptors/MainHandlerDescriptor.cs new file mode 100644 index 0000000..75fc723 --- /dev/null +++ b/src/LiteBus.Messaging/Internal/Registry/Descriptors/MainHandlerDescriptor.cs @@ -0,0 +1,9 @@ +using System; +using LiteBus.Messaging.Abstractions; + +namespace LiteBus.Messaging.Internal.Registry.Descriptors; + +internal sealed class MainHandlerDescriptor : HandlerDescriptorBase, IMainHandlerDescriptor +{ + public required Type MessageResultType { get; init; } +} \ No newline at end of file diff --git a/src/LiteBus.Messaging/Internal/Registry/Descriptors/MessageDescriptor.cs b/src/LiteBus.Messaging/Internal/Registry/Descriptors/MessageDescriptor.cs index 544a808..9f46a2a 100644 --- a/src/LiteBus.Messaging/Internal/Registry/Descriptors/MessageDescriptor.cs +++ b/src/LiteBus.Messaging/Internal/Registry/Descriptors/MessageDescriptor.cs @@ -4,13 +4,13 @@ namespace LiteBus.Messaging.Internal.Registry.Descriptors; -internal class MessageDescriptor : IMessageDescriptor +internal sealed class MessageDescriptor : IMessageDescriptor { private readonly List _errorHandlers = new(); - private readonly List _handlers = new(); + private readonly List _handlers = new(); private readonly List _postHandlers = new(); private readonly List _preHandlers = new(); - private readonly List _indirectHandlers = new(); + private readonly List _indirectHandlers = new(); private readonly List _indirectPostHandlers = new(); private readonly List _indirectPreHandlers = new(); private readonly List _indirectErrorHandlers = new(); @@ -25,9 +25,9 @@ public MessageDescriptor(Type messageType) public bool IsGeneric { get; } - public IReadOnlyCollection Handlers => _handlers; + public IReadOnlyCollection Handlers => _handlers; - public IReadOnlyCollection IndirectHandlers => _indirectHandlers; + public IReadOnlyCollection IndirectHandlers => _indirectHandlers; public IReadOnlyCollection PostHandlers => _postHandlers; @@ -41,7 +41,7 @@ public MessageDescriptor(Type messageType) public IReadOnlyCollection IndirectErrorHandlers => _indirectErrorHandlers; - public void AddDescriptors(IEnumerable descriptors) + public void AddDescriptors(IEnumerable descriptors) { foreach (var descriptor in descriptors) { @@ -49,7 +49,7 @@ public void AddDescriptors(IEnumerable descriptors) } } - public void AddDescriptor(IDescriptor descriptor) + public void AddDescriptor(IHandlerDescriptor descriptor) { if (MessageType == descriptor.MessageType) { @@ -58,8 +58,8 @@ public void AddDescriptor(IDescriptor descriptor) case IErrorHandlerDescriptor errorHandlerDescriptor: _errorHandlers.Add(errorHandlerDescriptor); break; - case IHandlerDescriptor handlerDescriptor: - _handlers.Add(handlerDescriptor); + case IMainHandlerDescriptor mainHandlerDescriptor: + _handlers.Add(mainHandlerDescriptor); break; case IPostHandlerDescriptor postHandlerDescriptor: _postHandlers.Add(postHandlerDescriptor); @@ -76,8 +76,8 @@ public void AddDescriptor(IDescriptor descriptor) case IErrorHandlerDescriptor errorHandlerDescriptor: _indirectErrorHandlers.Add(errorHandlerDescriptor); break; - case IHandlerDescriptor handlerDescriptor: - _indirectHandlers.Add(handlerDescriptor); + case IMainHandlerDescriptor mainHandlerDescriptor: + _indirectHandlers.Add(mainHandlerDescriptor); break; case IPostHandlerDescriptor postHandlerDescriptor: _indirectPostHandlers.Add(postHandlerDescriptor); diff --git a/src/LiteBus.Messaging/Internal/Registry/Descriptors/PostHandlerDescriptor.cs b/src/LiteBus.Messaging/Internal/Registry/Descriptors/PostHandlerDescriptor.cs index 7782050..6d6bbb7 100644 --- a/src/LiteBus.Messaging/Internal/Registry/Descriptors/PostHandlerDescriptor.cs +++ b/src/LiteBus.Messaging/Internal/Registry/Descriptors/PostHandlerDescriptor.cs @@ -3,24 +3,7 @@ namespace LiteBus.Messaging.Internal.Registry.Descriptors; -internal class PostHandlerDescriptor : IPostHandlerDescriptor +internal sealed class PostHandlerDescriptor : HandlerDescriptorBase, IPostHandlerDescriptor { - public PostHandlerDescriptor(Type postHandlerType, Type messageType, Type messageResultType, int order) - { - PostHandlerType = postHandlerType; - IsGeneric = messageType.IsGenericType; - MessageType = IsGeneric ? messageType.GetGenericTypeDefinition() : messageType; - MessageResultType = messageResultType; - Order = order; - } - - public Type PostHandlerType { get; } - - public Type MessageType { get; } - - public Type MessageResultType { get; } - - public bool IsGeneric { get; } - - public int Order { get; } + public required Type MessageResultType { get; init; } } \ No newline at end of file diff --git a/src/LiteBus.Messaging/Internal/Registry/Descriptors/PreHandlerDescriptor.cs b/src/LiteBus.Messaging/Internal/Registry/Descriptors/PreHandlerDescriptor.cs index 354749d..66ac2e7 100644 --- a/src/LiteBus.Messaging/Internal/Registry/Descriptors/PreHandlerDescriptor.cs +++ b/src/LiteBus.Messaging/Internal/Registry/Descriptors/PreHandlerDescriptor.cs @@ -1,23 +1,7 @@ -using System; -using LiteBus.Messaging.Abstractions; +using LiteBus.Messaging.Abstractions; namespace LiteBus.Messaging.Internal.Registry.Descriptors; -internal class PreHandlerDescriptor : IPreHandlerDescriptor +internal sealed class PreHandlerDescriptor : HandlerDescriptorBase, IPreHandlerDescriptor { - public PreHandlerDescriptor(Type preHandlerType, Type messageType, int order) - { - PreHandlerType = preHandlerType; - Order = order; - IsGeneric = messageType.IsGenericType; - MessageType = IsGeneric ? messageType.GetGenericTypeDefinition() : messageType; - } - - public Type PreHandlerType { get; } - - public int Order { get; } - - public bool IsGeneric { get; } - - public Type MessageType { get; } } \ No newline at end of file diff --git a/src/LiteBus.Messaging/Internal/Registry/MessageRegistry.cs b/src/LiteBus.Messaging/Internal/Registry/MessageRegistry.cs index 164b043..14a68c2 100644 --- a/src/LiteBus.Messaging/Internal/Registry/MessageRegistry.cs +++ b/src/LiteBus.Messaging/Internal/Registry/MessageRegistry.cs @@ -12,7 +12,7 @@ namespace LiteBus.Messaging.Internal.Registry; internal sealed class MessageRegistry : IMessageRegistry { - private readonly List _descriptorBuilders = new() + private readonly List _descriptorBuilders = new() { new HandlerDescriptorBuilder(), new ErrorHandlerDescriptorBuilder(), @@ -20,7 +20,7 @@ internal sealed class MessageRegistry : IMessageRegistry new PreHandlerDescriptorBuilder() }; private readonly List _messages = new(); - private readonly List _descriptors = new(); + private readonly List _descriptors = new(); private readonly ConcurrentDictionary _processedTypes = new(); private readonly List _newMessages = new();