|
| 1 | +using Snowberry.Mediator.Abstractions; |
| 2 | +using Snowberry.Mediator.Abstractions.Handler; |
| 3 | +using Snowberry.Mediator.Abstractions.Pipeline; |
| 4 | +using Snowberry.Mediator.DependencyInjection.Shared.Contracts; |
| 5 | +using Snowberry.Mediator.Extensions; |
| 6 | +using Snowberry.Mediator.Models; |
| 7 | +using Snowberry.Mediator.Registries; |
| 8 | +using Snowberry.Mediator.Registries.Contracts; |
| 9 | + |
| 10 | +namespace Snowberry.Mediator.DependencyInjection.Shared; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Helper type for adding Mediator services to a service context. |
| 14 | +/// </summary> |
| 15 | +public static class DependencyInjectionHelper |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Adds Mediator services to the specified service context. |
| 19 | + /// </summary> |
| 20 | + /// <param name="serviceContext">The service context.</param> |
| 21 | + /// <param name="options">The options.</param> |
| 22 | + /// <param name="serviceLifetime">The service lifetime.</param> |
| 23 | + /// <param name="append">Whether to append to existing registrations or replace them.</param> |
| 24 | + public static void AddSnowberryMediator( |
| 25 | + IServiceContext serviceContext, |
| 26 | + MediatorOptions options, |
| 27 | + RegistrationServiceLifetime serviceLifetime, |
| 28 | + bool append) |
| 29 | + { |
| 30 | + if (!append || !serviceContext.IsServiceRegistered<IMediator>()) |
| 31 | + serviceContext.Register(typeof(IMediator), typeof(Mediator), serviceLifetime); |
| 32 | + |
| 33 | + var allHandlers = new List<RequestHandlerInfo>(); |
| 34 | + var allStreamHandlers = new List<StreamRequestHandlerInfo>(); |
| 35 | + var allPipelineBehaviorHandlers = new List<PipelineBehaviorHandlerInfo>(); |
| 36 | + var allStreamPipelineBehaviorHandlers = new List<StreamPipelineBehaviorHandlerInfo>(); |
| 37 | + var allNotificationHandlers = new List<NotificationHandlerInfo>(); |
| 38 | + |
| 39 | + if (options.Assemblies != null && options.Assemblies.Count > 0) |
| 40 | + { |
| 41 | + for (int i = 0; i < options.Assemblies.Count; i++) |
| 42 | + { |
| 43 | + var assembly = options.Assemblies[i]; |
| 44 | + |
| 45 | + var scanResult = MediatorAssemblyHelper.ScanAssembly(assembly); |
| 46 | + |
| 47 | + if (scanResult.RequestHandlerTypes != null) |
| 48 | + for (int j = 0; j < scanResult.RequestHandlerTypes.Count; j++) |
| 49 | + allHandlers.Add(scanResult.RequestHandlerTypes[j]); |
| 50 | + |
| 51 | + if (scanResult.StreamRequestHandlerTypes != null) |
| 52 | + for (int j = 0; j < scanResult.StreamRequestHandlerTypes.Count; j++) |
| 53 | + allStreamHandlers.Add(scanResult.StreamRequestHandlerTypes[j]); |
| 54 | + |
| 55 | + if (options.RegisterPipelineBehaviors && options.ScanPipelineBehaviors && scanResult.PipelineBehaviorTypes != null) |
| 56 | + for (int j = 0; j < scanResult.PipelineBehaviorTypes.Count; j++) |
| 57 | + allPipelineBehaviorHandlers.Add(scanResult.PipelineBehaviorTypes[j]); |
| 58 | + |
| 59 | + if (options.RegisterStreamPipelineBehaviors && options.ScanStreamPipelineBehaviors && scanResult.StreamPipelineBehaviorTypes != null) |
| 60 | + for (int j = 0; j < scanResult.StreamPipelineBehaviorTypes.Count; j++) |
| 61 | + allStreamPipelineBehaviorHandlers.Add(scanResult.StreamPipelineBehaviorTypes[j]); |
| 62 | + |
| 63 | + if (options.RegisterNotificationHandlers && options.ScanNotificationHandlers && scanResult.NotificationHandlerTypes != null) |
| 64 | + for (int j = 0; j < scanResult.NotificationHandlerTypes.Count; j++) |
| 65 | + allNotificationHandlers.Add(scanResult.NotificationHandlerTypes[j]); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + var pipelineBehaviorType = typeof(IPipelineBehavior<,>); |
| 70 | + var streamPipelineBehaviorType = typeof(IStreamPipelineBehavior<,>); |
| 71 | + var requestHandlerType = typeof(IRequestHandler<,>); |
| 72 | + var streamRequestHandlerType = typeof(IStreamRequestHandler<,>); |
| 73 | + |
| 74 | + if (options.PipelineBehaviorTypes != null && options.RegisterPipelineBehaviors) |
| 75 | + MediatorAssemblyHelper.ParseHandlerInfo(pipelineBehaviorType, options.PipelineBehaviorTypes, allPipelineBehaviorHandlers); |
| 76 | + |
| 77 | + if (options.RequestHandlerTypes != null && options.RegisterRequestHandlers) |
| 78 | + MediatorAssemblyHelper.ParseHandlerInfo(requestHandlerType, options.RequestHandlerTypes, allHandlers); |
| 79 | + |
| 80 | + if (options.StreamRequestHandlerTypes != null && options.RegisterStreamRequestHandlers) |
| 81 | + MediatorAssemblyHelper.ParseHandlerInfo(streamRequestHandlerType, options.StreamRequestHandlerTypes, allStreamHandlers); |
| 82 | + |
| 83 | + if (options.StreamPipelineBehaviorTypes != null && options.RegisterStreamPipelineBehaviors) |
| 84 | + MediatorAssemblyHelper.ParseHandlerInfo(streamPipelineBehaviorType, options.StreamPipelineBehaviorTypes, allStreamPipelineBehaviorHandlers); |
| 85 | + |
| 86 | + if (options.NotificationHandlerTypes != null && options.RegisterNotificationHandlers) |
| 87 | + MediatorAssemblyHelper.ParseNotificationHandlers(options.NotificationHandlerTypes, allNotificationHandlers); |
| 88 | + |
| 89 | + for (int i = 0; i < allHandlers.Count; i++) |
| 90 | + { |
| 91 | + var handlerInfo = allHandlers[i]; |
| 92 | + serviceContext.Register(handlerInfo.CreateRequestHandlerInterfaceType(), handlerInfo.HandlerType, serviceLifetime); |
| 93 | + } |
| 94 | + |
| 95 | + for (int i = 0; i < allStreamHandlers.Count; i++) |
| 96 | + { |
| 97 | + var handlerInfo = allStreamHandlers[i]; |
| 98 | + serviceContext.Register(handlerInfo.CreateStreamRequestHandlerInterfaceType(), handlerInfo.HandlerType, serviceLifetime); |
| 99 | + } |
| 100 | + |
| 101 | + if (options.RegisterPipelineBehaviors && allPipelineBehaviorHandlers.Count > 0) |
| 102 | + AddPipelineBehaviors<IGlobalPipelineRegistry, GlobalPipelineRegistry, PipelineBehaviorHandlerInfo>( |
| 103 | + serviceContext, |
| 104 | + serviceLifetime, |
| 105 | + allPipelineBehaviorHandlers, |
| 106 | + append); |
| 107 | + |
| 108 | + if (options.RegisterStreamPipelineBehaviors && allStreamPipelineBehaviorHandlers.Count > 0) |
| 109 | + AddPipelineBehaviors<IGlobalStreamPipelineRegistry, GlobalStreamPipelineRegistry, StreamPipelineBehaviorHandlerInfo>( |
| 110 | + serviceContext, |
| 111 | + serviceLifetime, |
| 112 | + allStreamPipelineBehaviorHandlers, |
| 113 | + append); |
| 114 | + |
| 115 | + if (options.RegisterNotificationHandlers && allNotificationHandlers.Count > 0) |
| 116 | + AddNotificationHandlers(serviceContext, serviceLifetime, allNotificationHandlers, append); |
| 117 | + } |
| 118 | + |
| 119 | + private static void AddPipelineBehaviors<TGlobalPipelineInterface, TGlobalPipelineRegistry, THandlerInfo>( |
| 120 | + IServiceContext serviceContext, |
| 121 | + RegistrationServiceLifetime serviceLifetime, |
| 122 | + IList<THandlerInfo> pipelineBehaviorHandlers, |
| 123 | + bool append |
| 124 | + ) |
| 125 | + where TGlobalPipelineRegistry : TGlobalPipelineInterface, new() |
| 126 | + where TGlobalPipelineInterface : IBaseGlobalPipelineRegistry<THandlerInfo> |
| 127 | + where THandlerInfo : PipelineBehaviorHandlerInfo |
| 128 | + { |
| 129 | + if (pipelineBehaviorHandlers.Count == 0) |
| 130 | + return; |
| 131 | + |
| 132 | + TGlobalPipelineInterface? globalPipelineRegistry = default; |
| 133 | + if (!append || !serviceContext.IsServiceRegistered<TGlobalPipelineInterface>()) |
| 134 | + { |
| 135 | + globalPipelineRegistry = new TGlobalPipelineRegistry(); |
| 136 | + serviceContext.Register(serviceType: typeof(TGlobalPipelineInterface), instance: globalPipelineRegistry); |
| 137 | + } |
| 138 | + else |
| 139 | + { |
| 140 | + globalPipelineRegistry = serviceContext.TryToGetSingleton<TGlobalPipelineInterface>(out bool foundSingleton); |
| 141 | + |
| 142 | + if (!foundSingleton) |
| 143 | + { |
| 144 | + globalPipelineRegistry = new TGlobalPipelineRegistry(); |
| 145 | + serviceContext.Register(serviceType: typeof(TGlobalPipelineInterface), instance: globalPipelineRegistry); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + for (int i = 0; i < pipelineBehaviorHandlers.Count; i++) |
| 150 | + { |
| 151 | + var handler = pipelineBehaviorHandlers[i]; |
| 152 | + globalPipelineRegistry!.Register(handler); |
| 153 | + |
| 154 | + serviceContext.Register(handler.HandlerType, handler.HandlerType, serviceLifetime); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private static void AddNotificationHandlers( |
| 159 | + IServiceContext serviceContext, |
| 160 | + RegistrationServiceLifetime serviceLifetime, |
| 161 | + IList<NotificationHandlerInfo> notificationHandlers, |
| 162 | + bool append |
| 163 | + ) |
| 164 | + { |
| 165 | + if (notificationHandlers.Count == 0) |
| 166 | + return; |
| 167 | + |
| 168 | + IGlobalNotificationHandlerRegistry<NotificationHandlerInfo>? globalNotificationHandlerRegistry = null; |
| 169 | + |
| 170 | + if (!append || !serviceContext.IsServiceRegistered<IGlobalNotificationHandlerRegistry<NotificationHandlerInfo>>()) |
| 171 | + { |
| 172 | + globalNotificationHandlerRegistry = new GlobalNotificationHandlerRegistry(); |
| 173 | + serviceContext.Register(serviceType: typeof(IGlobalNotificationHandlerRegistry<NotificationHandlerInfo>), instance: globalNotificationHandlerRegistry); |
| 174 | + } |
| 175 | + else |
| 176 | + { |
| 177 | + globalNotificationHandlerRegistry = serviceContext.TryToGetSingleton<IGlobalNotificationHandlerRegistry<NotificationHandlerInfo>>(out bool foundSingleton); |
| 178 | + |
| 179 | + if (!foundSingleton) |
| 180 | + { |
| 181 | + globalNotificationHandlerRegistry = new GlobalNotificationHandlerRegistry(); |
| 182 | + serviceContext.Register(serviceType: typeof(IGlobalNotificationHandlerRegistry<NotificationHandlerInfo>), instance: globalNotificationHandlerRegistry); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + for (int i = 0; i < notificationHandlers.Count; i++) |
| 187 | + { |
| 188 | + var handler = notificationHandlers[i]; |
| 189 | + globalNotificationHandlerRegistry!.Register(handler); |
| 190 | + |
| 191 | + serviceContext.Register(handler.HandlerType, handler.HandlerType, serviceLifetime); |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments