1+ using System . Collections . Generic ;
2+ using System . Reflection ;
3+ using Microsoft . Extensions . DependencyInjection ;
4+ using Xer . Cqrs . CommandStack ;
5+ using Xer . Cqrs . CommandStack . Resolvers ;
6+ using Xer . Delegator . Registrations ;
7+ using Xer . Delegator . Resolvers ;
8+
9+ namespace Xer . Cqrs . Extensions . Microsoft . DependencyInjection
10+ {
11+ internal class CqrsCommandHandlerSelector : ICqrsCommandHandlerSelector
12+ {
13+ private readonly IServiceCollection _serviceCollection ;
14+
15+ internal CqrsCommandHandlerSelector ( IServiceCollection serviceCollection )
16+ {
17+ _serviceCollection = serviceCollection ;
18+ }
19+
20+ public ICqrsCommandHandlerSelector ByInterface ( Assembly assembly )
21+ {
22+ return ByInterface ( assembly , ServiceLifetime . Transient ) ;
23+ }
24+
25+ public ICqrsCommandHandlerSelector ByInterface ( Assembly assembly , ServiceLifetime lifetime )
26+ {
27+ return ByInterface ( new [ ] { assembly } , lifetime ) ;
28+ }
29+
30+ public ICqrsCommandHandlerSelector ByInterface ( IEnumerable < Assembly > assemblies )
31+ {
32+ return ByInterface ( assemblies , ServiceLifetime . Transient ) ;
33+ }
34+
35+ public ICqrsCommandHandlerSelector ByInterface ( IEnumerable < Assembly > assemblies , ServiceLifetime lifetime )
36+ {
37+ if ( assemblies == null )
38+ {
39+ throw new System . ArgumentNullException ( nameof ( assemblies ) ) ;
40+ }
41+
42+ _serviceCollection . Scan ( scan => scan
43+ . FromAssemblies ( assemblies )
44+ // Register async and sync command handlers
45+ . AddClasses ( classes => classes . AssignableToAny ( typeof ( ICommandAsyncHandler < > ) , typeof ( ICommandHandler < > ) ) )
46+ . AsImplementedInterfaces ( )
47+ . WithLifetime ( lifetime ) ) ;
48+
49+ _serviceCollection . AddSingleton < CommandHandlerDelegateResolver > ( serviceProvider =>
50+ {
51+ return new CommandHandlerDelegateResolver (
52+ CompositeMessageHandlerResolver . Compose (
53+ new ContainerCommandAsyncHandlerResolver ( new ServiceProviderAdapter ( serviceProvider ) ) ,
54+ new ContainerCommandHandlerResolver ( new ServiceProviderAdapter ( serviceProvider ) ) ) ) ;
55+ } ) ;
56+
57+ return this ;
58+ }
59+
60+ public ICqrsCommandHandlerSelector ByAttribute ( Assembly assembly )
61+ {
62+ return ByAttribute ( assembly , ServiceLifetime . Transient ) ;
63+ }
64+
65+ public ICqrsCommandHandlerSelector ByAttribute ( Assembly assembly , ServiceLifetime lifetime )
66+ {
67+ return ByAttribute ( new [ ] { assembly } , lifetime ) ;
68+ }
69+
70+ public ICqrsCommandHandlerSelector ByAttribute ( IEnumerable < Assembly > assemblies )
71+ {
72+ return ByAttribute ( assemblies , ServiceLifetime . Transient ) ;
73+ }
74+
75+ public ICqrsCommandHandlerSelector ByAttribute ( IEnumerable < Assembly > assemblies , ServiceLifetime lifetime )
76+ {
77+ if ( assemblies == null )
78+ {
79+ throw new System . ArgumentNullException ( nameof ( assemblies ) ) ;
80+ }
81+
82+ _serviceCollection . Scan ( scan => scan
83+ . FromAssemblies ( assemblies )
84+ // Register classes that has a method marked with [CommandHandler]
85+ . AddClasses ( classes => classes . Where ( type => CommandHandlerAttributeMethod . IsFoundInType ( type ) ) )
86+ . AsSelf ( )
87+ . WithLifetime ( lifetime ) ) ;
88+
89+ _serviceCollection . AddSingleton < CommandHandlerDelegateResolver > ( serviceProvider =>
90+ {
91+ var singleMessageHandlerRegistration = new SingleMessageHandlerRegistration ( ) ;
92+ singleMessageHandlerRegistration . RegisterCommandHandlerAttributes ( assemblies , serviceProvider . GetRequiredService ) ;
93+
94+ return new CommandHandlerDelegateResolver ( singleMessageHandlerRegistration . BuildMessageHandlerResolver ( ) ) ;
95+ } ) ;
96+
97+ return this ;
98+ }
99+ }
100+ }
0 commit comments