-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathMediatorOptions.cs
More file actions
225 lines (182 loc) · 9.6 KB
/
Copy pathMediatorOptions.cs
File metadata and controls
225 lines (182 loc) · 9.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using Cortex.Mediator.Commands;
using Cortex.Mediator.Notifications;
using Cortex.Mediator.Queries;
using Cortex.Mediator.Streaming;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Cortex.Mediator.DependencyInjection
{
public class MediatorOptions
{
internal List<Type> CommandBehaviors { get; } = new();
internal List<Type> VoidCommandBehaviors { get; } = new();
internal List<Type> QueryBehaviors { get; } = new();
internal List<Type> NotificationBehaviors { get; } = new();
internal List<Type> StreamQueryBehaviors { get; } = new();
public bool OnlyPublicClasses { get; set; } = true;
/// <summary>
/// Gets or sets the service lifetime for handler registrations
/// (command handlers, query handlers, notification handlers, and stream query handlers).
/// Defaults to <see cref="ServiceLifetime.Scoped"/>.
/// </summary>
public ServiceLifetime HandlerLifetime { get; set; } = ServiceLifetime.Scoped;
/// <summary>
/// Gets the type of notification publish strategy to use.
/// Defaults to <see cref="ParallelNotificationStrategy"/>.
/// Use <see cref="UseNotificationPublishStrategy{TStrategy}"/> to change.
/// </summary>
internal Type NotificationPublishStrategyType { get; private set; } = typeof(ParallelNotificationStrategy);
/// <summary>
/// Sets the strategy used to publish notifications to multiple handlers.
/// </summary>
/// <typeparam name="TStrategy">
/// The strategy implementation. Built-in options:
/// <see cref="ParallelNotificationStrategy"/> (default) — all handlers run in parallel via Task.WhenAll,
/// <see cref="SequentialNotificationStrategy"/> — handlers run one at a time in registration order,
/// <see cref="StopOnFirstFailureNotificationStrategy"/> — sequential, stops on first exception.
/// </typeparam>
public MediatorOptions UseNotificationPublishStrategy<TStrategy>()
where TStrategy : class, INotificationPublishStrategy
{
NotificationPublishStrategyType = typeof(TStrategy);
return this;
}
/// <summary>
/// Register a *closed* command pipeline behavior.
/// </summary>
public MediatorOptions AddCommandPipelineBehavior<TBehavior>()
where TBehavior : class // Add constraint
{
var behaviorType = typeof(TBehavior);
if (behaviorType.IsGenericTypeDefinition)
throw new ArgumentException("Open generic types must be registered using AddOpenCommandPipelineBehavior");
var implementsReturning =
behaviorType.GetInterfaces().Any(i => i.IsGenericType &&
i.GetGenericTypeDefinition() == typeof(ICommandPipelineBehavior<,>));
var implementsNonReturning =
behaviorType.GetInterfaces().Any(i => i.IsGenericType &&
i.GetGenericTypeDefinition() == typeof(ICommandPipelineBehavior<>));
if (!implementsReturning && !implementsNonReturning)
throw new ArgumentException("Type must implement ICommandPipelineBehavior<,> or ICommandPipelineBehavior<>");
if (implementsReturning)
CommandBehaviors.Add(behaviorType);
if (implementsNonReturning)
VoidCommandBehaviors.Add(behaviorType);
return this;
}
/// <summary>
/// Register an *open generic* command pipeline behavior, e.g. typeof(LoggingCommandBehavior<,>).
/// </summary>
public MediatorOptions AddOpenCommandPipelineBehavior(Type openGenericBehaviorType)
{
if (!openGenericBehaviorType.IsGenericTypeDefinition)
throw new ArgumentException("Type must be an open generic type definition");
var implementsReturning =
openGenericBehaviorType.GetInterfaces().Any(i => i.IsGenericType &&
i.GetGenericTypeDefinition() == typeof(ICommandPipelineBehavior<,>));
var implementsNonReturning =
openGenericBehaviorType.GetInterfaces().Any(i => i.IsGenericType &&
i.GetGenericTypeDefinition() == typeof(ICommandPipelineBehavior<>));
if (!implementsReturning && !implementsNonReturning)
throw new ArgumentException("Type must implement ICommandPipelineBehavior<,> or ICommandPipelineBehavior<>");
if (implementsReturning)
CommandBehaviors.Add(openGenericBehaviorType);
if (implementsNonReturning)
VoidCommandBehaviors.Add(openGenericBehaviorType);
return this;
}
/// <summary>
/// Register a *closed* query pipeline behavior.
/// </summary>
public MediatorOptions AddQueryPipelineBehavior<TBehavior>()
where TBehavior : class
{
var behaviorType = typeof(TBehavior);
if (behaviorType.IsGenericTypeDefinition)
throw new ArgumentException("Open generic types must be registered using AddOpenQueryPipelineBehavior");
var implementsQueryBehavior =
behaviorType.GetInterfaces().Any(i => i.IsGenericType &&
i.GetGenericTypeDefinition() == typeof(IQueryPipelineBehavior<,>));
if (!implementsQueryBehavior)
throw new ArgumentException("Type must implement IQueryPipelineBehavior<,>");
QueryBehaviors.Add(behaviorType);
return this;
}
/// <summary>
/// Register an *open generic* query pipeline behavior, e.g. typeof(CachingQueryBehavior<,>).
/// </summary>
public MediatorOptions AddOpenQueryPipelineBehavior(Type openGenericBehaviorType)
{
if (!openGenericBehaviorType.IsGenericTypeDefinition)
{
throw new ArgumentException("Type must be an open generic type definition");
}
var queryBehaviorInterface = openGenericBehaviorType.GetInterfaces()
.FirstOrDefault(i => i.IsGenericType &&
i.GetGenericTypeDefinition() == typeof(IQueryPipelineBehavior<,>));
if (queryBehaviorInterface == null)
{
throw new ArgumentException("Type must implement IQueryPipelineBehavior<,>");
}
QueryBehaviors.Add(openGenericBehaviorType);
return this;
}
/// <summary>
/// Register a *closed* notification pipeline behavior.
/// </summary>
public MediatorOptions AddNotificationPipelineBehavior<TBehavior>()
where TBehavior : class
{
var behaviorType = typeof(TBehavior);
if (behaviorType.IsGenericTypeDefinition)
throw new ArgumentException("Open generic types must be registered using AddOpenNotificationPipelineBehavior");
var implementsNotificationBehavior =
behaviorType.GetInterfaces().Any(i => i.IsGenericType &&
i.GetGenericTypeDefinition() == typeof(INotificationPipelineBehavior<>));
if (!implementsNotificationBehavior)
throw new ArgumentException("Type must implement INotificationPipelineBehavior<>");
NotificationBehaviors.Add(behaviorType);
return this;
}
/// <summary>
/// Register an *open generic* notification pipeline behavior, e.g. typeof(LoggingNotificationBehavior<>).
/// </summary>
public MediatorOptions AddOpenNotificationPipelineBehavior(Type openGenericBehaviorType)
{
if (!openGenericBehaviorType.IsGenericTypeDefinition)
{
throw new ArgumentException("Type must be an open generic type definition");
}
var notificationBehaviorInterface = openGenericBehaviorType.GetInterfaces()
.FirstOrDefault(i => i.IsGenericType &&
i.GetGenericTypeDefinition() == typeof(INotificationPipelineBehavior<>));
if (notificationBehaviorInterface == null)
{
throw new ArgumentException("Type must implement INotificationPipelineBehavior<>");
}
NotificationBehaviors.Add(openGenericBehaviorType);
return this;
}
/// <summary>
/// Register an *open generic* streaming query pipeline behavior, e.g. typeof(LoggingStreamQueryBehavior<,>).
/// </summary>
public MediatorOptions AddOpenStreamQueryPipelineBehavior(Type openGenericBehaviorType)
{
if (!openGenericBehaviorType.IsGenericTypeDefinition)
{
throw new ArgumentException("Type must be an open generic type definition");
}
var streamBehaviorInterface = openGenericBehaviorType.GetInterfaces()
.FirstOrDefault(i => i.IsGenericType &&
i.GetGenericTypeDefinition() == typeof(IStreamQueryPipelineBehavior<,>));
if (streamBehaviorInterface == null)
{
throw new ArgumentException("Type must implement IStreamQueryPipelineBehavior<,>");
}
StreamQueryBehaviors.Add(openGenericBehaviorType);
return this;
}
}
}