11using Snowberry . Mediator . Abstractions ;
2+ using Snowberry . Mediator . Abstractions . Handler ;
23using Snowberry . Mediator . Abstractions . Pipeline ;
4+ using Snowberry . Mediator . DependencyInjection . Shared . Contracts ;
35using Snowberry . Mediator . Extensions ;
46using Snowberry . Mediator . Models ;
57using Snowberry . Mediator . Registries ;
68using Snowberry . Mediator . Registries . Contracts ;
79
810namespace Snowberry . Mediator . DependencyInjection . Shared ;
911
12+ /// <summary>
13+ /// Helper type for adding Mediator services to a service context.
14+ /// </summary>
1015public static class DependencyInjectionHelper
1116{
12- public static void AddSnowberryMediator ( IServiceContext serviceContext , MediatorOptions options , RegistrationServiceLifetime serviceLifetime )
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 )
1329 {
14- serviceContext . Register ( typeof ( IMediator ) , typeof ( Mediator ) , serviceLifetime ) ;
15-
16- if ( options . Assemblies == null || options . Assemblies . Count == 0 )
17- return ;
30+ if ( ! append || ! serviceContext . IsServiceRegistered < IMediator > ( ) )
31+ serviceContext . Register ( typeof ( IMediator ) , typeof ( Mediator ) , serviceLifetime ) ;
1832
1933 var allHandlers = new List < RequestHandlerInfo > ( ) ;
2034 var allStreamHandlers = new List < StreamRequestHandlerInfo > ( ) ;
2135 var allPipelineBehaviorHandlers = new List < PipelineBehaviorHandlerInfo > ( ) ;
2236 var allStreamPipelineBehaviorHandlers = new List < StreamPipelineBehaviorHandlerInfo > ( ) ;
2337 var allNotificationHandlers = new List < NotificationHandlerInfo > ( ) ;
2438
25- for ( int i = 0 ; i < options . Assemblies . Count ; i ++ )
39+ if ( options . Assemblies != null && options . Assemblies . Count > 0 )
2640 {
27- var assembly = options . Assemblies [ i ] ;
41+ for ( int i = 0 ; i < options . Assemblies . Count ; i ++ )
42+ {
43+ var assembly = options . Assemblies [ i ] ;
2844
29- var scanResult = MediatorAssemblyHelper . ScanAssembly ( assembly ) ;
45+ var scanResult = MediatorAssemblyHelper . ScanAssembly ( assembly ) ;
3046
31- if ( scanResult . RequestHandlerTypes != null )
32- for ( int j = 0 ; j < scanResult . RequestHandlerTypes . Count ; j ++ )
33- allHandlers . Add ( scanResult . RequestHandlerTypes [ j ] ) ;
47+ if ( scanResult . RequestHandlerTypes != null )
48+ for ( int j = 0 ; j < scanResult . RequestHandlerTypes . Count ; j ++ )
49+ allHandlers . Add ( scanResult . RequestHandlerTypes [ j ] ) ;
3450
35- if ( scanResult . StreamRequestHandlerTypes != null )
36- for ( int j = 0 ; j < scanResult . StreamRequestHandlerTypes . Count ; j ++ )
37- allStreamHandlers . Add ( scanResult . StreamRequestHandlerTypes [ j ] ) ;
51+ if ( scanResult . StreamRequestHandlerTypes != null )
52+ for ( int j = 0 ; j < scanResult . StreamRequestHandlerTypes . Count ; j ++ )
53+ allStreamHandlers . Add ( scanResult . StreamRequestHandlerTypes [ j ] ) ;
3854
39- if ( options . RegisterPipelineBehaviors && options . ScanPipelineBehaviors && scanResult . PipelineBehaviorTypes != null )
40- for ( int j = 0 ; j < scanResult . PipelineBehaviorTypes . Count ; j ++ )
41- allPipelineBehaviorHandlers . Add ( scanResult . PipelineBehaviorTypes [ j ] ) ;
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 ] ) ;
4258
43- if ( options . RegisterStreamPipelineBehaviors && options . ScanStreamPipelineBehaviors && scanResult . StreamPipelineBehaviorTypes != null )
44- for ( int j = 0 ; j < scanResult . StreamPipelineBehaviorTypes . Count ; j ++ )
45- allStreamPipelineBehaviorHandlers . Add ( scanResult . StreamPipelineBehaviorTypes [ j ] ) ;
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 ] ) ;
4662
47- if ( options . RegisterNotificationHandlers && options . ScanNotificationHandlers && scanResult . NotificationHandlerTypes != null )
48- for ( int j = 0 ; j < scanResult . NotificationHandlerTypes . Count ; j ++ )
49- allNotificationHandlers . Add ( scanResult . NotificationHandlerTypes [ j ] ) ;
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+ }
5067 }
5168
5269 var pipelineBehaviorType = typeof ( IPipelineBehavior < , > ) ;
5370 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 ) ;
5479
55- if ( options . PipelineBehaviorTypes != null )
56- MediatorAssemblyHelper . ParsePipelineBehaviors ( pipelineBehaviorType , options . PipelineBehaviorTypes , allPipelineBehaviorHandlers ) ;
80+ if ( options . StreamRequestHandlerTypes != null && options . RegisterStreamRequestHandlers )
81+ MediatorAssemblyHelper . ParseHandlerInfo ( streamRequestHandlerType , options . StreamRequestHandlerTypes , allStreamHandlers ) ;
5782
58- if ( options . StreamPipelineBehaviorTypes != null )
59- MediatorAssemblyHelper . ParsePipelineBehaviors ( streamPipelineBehaviorType , options . StreamPipelineBehaviorTypes , allStreamPipelineBehaviorHandlers ) ;
83+ if ( options . StreamPipelineBehaviorTypes != null && options . RegisterStreamPipelineBehaviors )
84+ MediatorAssemblyHelper . ParseHandlerInfo ( streamPipelineBehaviorType , options . StreamPipelineBehaviorTypes , allStreamPipelineBehaviorHandlers ) ;
6085
61- if ( options . NotificationHandlerTypes != null )
86+ if ( options . NotificationHandlerTypes != null && options . RegisterNotificationHandlers )
6287 MediatorAssemblyHelper . ParseNotificationHandlers ( options . NotificationHandlerTypes , allNotificationHandlers ) ;
6388
6489 for ( int i = 0 ; i < allHandlers . Count ; i ++ )
@@ -77,49 +102,91 @@ public static void AddSnowberryMediator(IServiceContext serviceContext, Mediator
77102 AddPipelineBehaviors < IGlobalPipelineRegistry , GlobalPipelineRegistry , PipelineBehaviorHandlerInfo > (
78103 serviceContext ,
79104 serviceLifetime ,
80- allPipelineBehaviorHandlers ) ;
105+ allPipelineBehaviorHandlers ,
106+ append ) ;
81107
82108 if ( options . RegisterStreamPipelineBehaviors && allStreamPipelineBehaviorHandlers . Count > 0 )
83109 AddPipelineBehaviors < IGlobalStreamPipelineRegistry , GlobalStreamPipelineRegistry , StreamPipelineBehaviorHandlerInfo > (
84110 serviceContext ,
85111 serviceLifetime ,
86- allStreamPipelineBehaviorHandlers ) ;
112+ allStreamPipelineBehaviorHandlers ,
113+ append ) ;
87114
88115 if ( options . RegisterNotificationHandlers && allNotificationHandlers . Count > 0 )
89- AddNotificationHandlers ( serviceContext , serviceLifetime , allNotificationHandlers ) ;
116+ AddNotificationHandlers ( serviceContext , serviceLifetime , allNotificationHandlers , append ) ;
90117 }
91118
92- private static void AddPipelineBehaviors < TGlobalPipelineInterface , TGlobalPipelineRegistry , THandlerInfo > ( IServiceContext serviceContext , RegistrationServiceLifetime serviceLifetime , IList < THandlerInfo > pipelineBehaviorHandlers )
93- where TGlobalPipelineRegistry : IBaseGlobalPipelineRegistry < THandlerInfo > , new ( )
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 >
94127 where THandlerInfo : PipelineBehaviorHandlerInfo
95128 {
96129 if ( pipelineBehaviorHandlers . Count == 0 )
97130 return ;
98131
99- var globalPipelineRegistry = new TGlobalPipelineRegistry ( ) ;
100- serviceContext . Register ( serviceType : typeof ( TGlobalPipelineInterface ) , instance : globalPipelineRegistry ) ;
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+ }
101148
102149 for ( int i = 0 ; i < pipelineBehaviorHandlers . Count ; i ++ )
103150 {
104151 var handler = pipelineBehaviorHandlers [ i ] ;
105- globalPipelineRegistry . Register ( handler ) ;
152+ globalPipelineRegistry ! . Register ( handler ) ;
106153
107154 serviceContext . Register ( handler . HandlerType , handler . HandlerType , serviceLifetime ) ;
108155 }
109156 }
110157
111- private static void AddNotificationHandlers ( IServiceContext serviceContext , RegistrationServiceLifetime serviceLifetime , IList < NotificationHandlerInfo > notificationHandlers )
158+ private static void AddNotificationHandlers (
159+ IServiceContext serviceContext ,
160+ RegistrationServiceLifetime serviceLifetime ,
161+ IList < NotificationHandlerInfo > notificationHandlers ,
162+ bool append
163+ )
112164 {
113165 if ( notificationHandlers . Count == 0 )
114166 return ;
115167
116- var globalNotificationHandlerRegistry = new GlobalNotificationHandlerRegistry ( ) ;
117- serviceContext . Register ( serviceType : typeof ( IGlobalNotificationHandlerRegistry < NotificationHandlerInfo > ) , instance : globalNotificationHandlerRegistry ) ;
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+ }
118185
119186 for ( int i = 0 ; i < notificationHandlers . Count ; i ++ )
120187 {
121188 var handler = notificationHandlers [ i ] ;
122- globalNotificationHandlerRegistry . Register ( handler ) ;
189+ globalNotificationHandlerRegistry ! . Register ( handler ) ;
123190
124191 serviceContext . Register ( handler . HandlerType , handler . HandlerType , serviceLifetime ) ;
125192 }
0 commit comments