| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 3 | | using NexMediator.Abstractions.Context; |
| | | 4 | | using NexMediator.Abstractions.Interfaces; |
| | | 5 | | using NexMediator.Pipeline.Behaviors; |
| | | 6 | | |
| | | 7 | | namespace NexMediator.Core; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Configuration options for registering and ordering pipeline behaviors. |
| | | 11 | | /// </summary> |
| | | 12 | | public class NexMediatorOptions |
| | | 13 | | { |
| | | 14 | | private readonly IServiceCollection _services; |
| | 28 | 15 | | private readonly Dictionary<Type, BehaviorRegistration> _behaviorMap = new(); |
| | | 16 | | |
| | 3 | 17 | | private static readonly Type LoggingBehaviorType = typeof(LoggingBehavior<,>); |
| | 3 | 18 | | private static readonly Type FluentValidationBehaviorType = typeof(FluentValidationBehavior<,>); |
| | 3 | 19 | | private static readonly Type CachingBehaviorType = typeof(CachingBehavior<,>); |
| | 3 | 20 | | private static readonly Type TransactionBehaviorType = typeof(TransactionBehavior<,>); |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Initializes a new instance of <see cref="NexMediatorOptions"/>. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="services"> |
| | | 26 | | /// Service collection to register behaviors and context. |
| | | 27 | | /// </param> |
| | 28 | 28 | | public NexMediatorOptions(IServiceCollection services) |
| | 28 | 29 | | { |
| | 28 | 30 | | _services = services ?? throw new ArgumentNullException(nameof(services)); |
| | 28 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Adds a pipeline behavior with a specific execution order. |
| | | 35 | | /// Registers both interface and concrete type for DI. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="behaviorType">Open-generic behavior type.</param> |
| | | 38 | | /// <param name="order">Execution order (lower runs first).</param> |
| | | 39 | | /// <returns>Same <see cref="NexMediatorOptions"/> for chaining.</returns> |
| | | 40 | | public NexMediatorOptions AddBehavior(Type behaviorType, int order) |
| | 14 | 41 | | { |
| | 14 | 42 | | if (behaviorType == null) |
| | 0 | 43 | | throw new ArgumentNullException(nameof(behaviorType)); |
| | 14 | 44 | | if (!behaviorType.IsGenericTypeDefinition) |
| | 1 | 45 | | throw new ArgumentException("Type must be open generic.", nameof(behaviorType)); |
| | 13 | 46 | | if (!ImplementsPipelineBehavior(behaviorType)) |
| | 1 | 47 | | throw new ArgumentException("Type must implement INexPipelineBehavior<,>.", nameof(behaviorType)); |
| | | 48 | | |
| | | 49 | | // Track execution order |
| | 12 | 50 | | _behaviorMap[behaviorType] = new BehaviorRegistration(behaviorType, order); |
| | | 51 | | |
| | | 52 | | // Register open-generic interface and concrete type |
| | 12 | 53 | | _services.AddTransient(typeof(INexPipelineBehavior<,>), behaviorType); |
| | 12 | 54 | | _services.AddTransient(behaviorType); |
| | | 55 | | |
| | 12 | 56 | | return this; |
| | 12 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Adds a pipeline behavior, optionally enabling correlation support. |
| | | 61 | | /// Registers with scoped lifetime if logging, otherwise transient. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <param name="behaviorType">Open-generic behavior type.</param> |
| | | 64 | | /// <param name="order">Execution order.</param> |
| | | 65 | | /// <param name="enableCorrelation"> |
| | | 66 | | /// If true and logging behavior, registers correlation context and scoped behavior. |
| | | 67 | | /// </param> |
| | | 68 | | /// <returns>Same <see cref="NexMediatorOptions"/> for chaining.</returns> |
| | | 69 | | public NexMediatorOptions AddBehavior(Type behaviorType, int order, bool enableCorrelation) |
| | 0 | 70 | | { |
| | 0 | 71 | | if (behaviorType == null) |
| | 0 | 72 | | throw new ArgumentNullException(nameof(behaviorType)); |
| | | 73 | | |
| | 0 | 74 | | if (enableCorrelation && behaviorType == LoggingBehaviorType) |
| | 0 | 75 | | { |
| | 0 | 76 | | _services.TryAddScoped<ICorrelationContext, CorrelationContext>(); |
| | 0 | 77 | | _services.AddScoped(behaviorType); |
| | 0 | 78 | | } |
| | | 79 | | else |
| | 0 | 80 | | { |
| | 0 | 81 | | _services.AddTransient(behaviorType); |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | // Track execution order |
| | 0 | 85 | | _behaviorMap[behaviorType] = new BehaviorRegistration(behaviorType, order); |
| | | 86 | | // Register open-generic interface |
| | 0 | 87 | | _services.AddTransient(typeof(INexPipelineBehavior<,>), behaviorType); |
| | | 88 | | |
| | 0 | 89 | | return this; |
| | 0 | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Adds a pipeline behavior of the specified generic type. |
| | | 94 | | /// </summary> |
| | | 95 | | /// <typeparam name="TBehavior">Open-generic behavior type.</typeparam> |
| | | 96 | | /// <param name="order">Execution order.</param> |
| | | 97 | | /// <returns>Same <see cref="NexMediatorOptions"/> for chaining.</returns> |
| | | 98 | | public NexMediatorOptions AddBehavior<TBehavior>(int order) |
| | | 99 | | where TBehavior : class |
| | 0 | 100 | | => AddBehavior(typeof(TBehavior), order); |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Removes a previously added behavior and its DI registrations. |
| | | 104 | | /// </summary> |
| | | 105 | | /// <param name="behaviorType">Open-generic behavior type to remove.</param> |
| | | 106 | | /// <returns>Same <see cref="NexMediatorOptions"/> for chaining.</returns> |
| | | 107 | | public NexMediatorOptions RemoveBehavior(Type behaviorType) |
| | 2 | 108 | | { |
| | 2 | 109 | | if (behaviorType == null) |
| | 0 | 110 | | throw new ArgumentNullException(nameof(behaviorType)); |
| | | 111 | | |
| | 2 | 112 | | if (_behaviorMap.Remove(behaviorType)) |
| | 1 | 113 | | { |
| | | 114 | | // Remove all service descriptors for this behavior |
| | 1 | 115 | | var descriptors = _services |
| | 2 | 116 | | .Where(d => d.ImplementationType == behaviorType) |
| | 1 | 117 | | .ToList(); |
| | | 118 | | |
| | 7 | 119 | | foreach (var descriptor in descriptors) |
| | 2 | 120 | | { |
| | 2 | 121 | | _services.Remove(descriptor); |
| | 2 | 122 | | } |
| | 1 | 123 | | } |
| | | 124 | | |
| | 2 | 125 | | return this; |
| | 2 | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Validates the current behavior configuration and returns warnings. |
| | | 130 | | /// </summary> |
| | | 131 | | /// <returns>List of warning messages.</returns> |
| | | 132 | | public IReadOnlyList<string> Validate() |
| | 20 | 133 | | { |
| | 20 | 134 | | var messages = new List<string>(); |
| | | 135 | | |
| | | 136 | | // Check for duplicate execution orders |
| | 20 | 137 | | var duplicateGroups = _behaviorMap.Values |
| | 8 | 138 | | .GroupBy(b => b.Order) |
| | 26 | 139 | | .Where(g => g.Count() > 1); |
| | 64 | 140 | | foreach (var group in duplicateGroups) |
| | 2 | 141 | | { |
| | 6 | 142 | | var names = string.Join(", ", group.Select(b => b.BehaviorType.Name)); |
| | 2 | 143 | | messages.Add($"Multiple behaviors with order {group.Key}: {names}"); |
| | 2 | 144 | | } |
| | | 145 | | |
| | | 146 | | // Warn if built-in behaviors are out of recommended order |
| | 20 | 147 | | if (HasBehaviorOutOfOrder(LoggingBehaviorType, 1) |
| | 20 | 148 | | || HasBehaviorOutOfOrder(FluentValidationBehaviorType, 2) |
| | 20 | 149 | | || HasBehaviorOutOfOrder(CachingBehaviorType, 3) |
| | 20 | 150 | | || HasBehaviorOutOfOrder(TransactionBehaviorType, 4)) |
| | 2 | 151 | | { |
| | 2 | 152 | | messages.Add( |
| | 2 | 153 | | "Built-in behaviors are not in recommended order: " + |
| | 2 | 154 | | "Logging (1), FluentValidation (2), Caching (3), Transaction (4)"); |
| | 2 | 155 | | } |
| | | 156 | | |
| | 20 | 157 | | return messages; |
| | 20 | 158 | | } |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Orders a list of behavior instances by their configured execution order. |
| | | 162 | | /// </summary> |
| | | 163 | | /// <param name="behaviors">Unordered behavior instances.</param> |
| | | 164 | | /// <returns>Ordered behaviors.</returns> |
| | | 165 | | public IEnumerable<object> OrderBehaviors(IEnumerable<object> behaviors) |
| | 8 | 166 | | => behaviors.OrderBy(b => |
| | 8 | 167 | | { |
| | 8 | 168 | | var type = b.GetType().IsGenericType |
| | 8 | 169 | | ? b.GetType().GetGenericTypeDefinition() |
| | 8 | 170 | | : b.GetType(); |
| | 8 | 171 | | return _behaviorMap.TryGetValue(type, out var reg) |
| | 8 | 172 | | ? reg.Order |
| | 8 | 173 | | : int.MaxValue; |
| | 16 | 174 | | }); |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// Checks if an open-generic behavior type is registered. |
| | | 178 | | /// </summary> |
| | | 179 | | public bool HasBehavior(Type behaviorType) |
| | 18 | 180 | | { |
| | 18 | 181 | | if (behaviorType == null) |
| | 0 | 182 | | throw new ArgumentNullException(nameof(behaviorType)); |
| | 18 | 183 | | return _behaviorMap.ContainsKey(behaviorType); |
| | 18 | 184 | | } |
| | | 185 | | |
| | | 186 | | private bool ImplementsPipelineBehavior(Type type) |
| | 13 | 187 | | => type.GetInterfaces() |
| | 25 | 188 | | .Any(i => i.IsGenericType && |
| | 25 | 189 | | i.GetGenericTypeDefinition() == typeof(INexPipelineBehavior<,>)); |
| | | 190 | | |
| | | 191 | | private bool HasBehaviorOutOfOrder(Type behaviorType, int expectedOrder) |
| | 78 | 192 | | => _behaviorMap.TryGetValue(behaviorType, out var reg) |
| | 78 | 193 | | && reg.Order != expectedOrder; |
| | | 194 | | |
| | 28 | 195 | | private record BehaviorRegistration(Type BehaviorType, int Order); |
| | | 196 | | } |