Skip to content

Commit cf5bdf5

Browse files
authored
Merge pull request #1068 from zachpainter77/master
Remove Generic Handler Support / Implementation (Leaving Failing Tests)
2 parents fb30902 + 447792f commit cf5bdf5

File tree

10 files changed

+379
-443
lines changed

10 files changed

+379
-443
lines changed

src/MediatR.Contracts/MediatR.Contracts.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
<Description>Contracts package for requests, responses, and notifications</Description>
77
<Copyright>Copyright Jimmy Bogard</Copyright>
88
<TargetFramework>netstandard2.0</TargetFramework>
9-
<Nullable>enable</Nullable>
109
<Features>strict</Features>
1110
<PackageTags>mediator;request;response;queries;commands;notifications</PackageTags>
1211
<SignAssembly>true</SignAssembly>

src/MediatR/Entities/OpenBehavior.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using Microsoft.Extensions.DependencyInjection;
3+
4+
namespace MediatR.Entities;
5+
/// <summary>
6+
/// Creates open behavior entity.
7+
/// </summary>
8+
public class OpenBehavior
9+
{
10+
public OpenBehavior(Type openBehaviorType, ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
11+
{
12+
OpenBehaviorType = openBehaviorType;
13+
ServiceLifetime = serviceLifetime;
14+
}
15+
16+
public Type? OpenBehaviorType { get; }
17+
public ServiceLifetime ServiceLifetime { get; }
18+
19+
20+
}

src/MediatR/IPipelineBehavior.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace MediatR;
99
/// </summary>
1010
/// <typeparam name="TResponse">Response type</typeparam>
1111
/// <returns>Awaitable task returning a <typeparamref name="TResponse"/></returns>
12-
public delegate Task<TResponse> RequestHandlerDelegate<TResponse>();
12+
public delegate Task<TResponse> RequestHandlerDelegate<TResponse>(CancellationToken t = default);
1313

1414
/// <summary>
1515
/// Pipeline behavior to surround the inner handler.

src/MediatR/MicrosoftExtensionsDI/MediatrServiceConfiguration.cs

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Reflection;
55
using MediatR;
6+
using MediatR.Entities;
67
using MediatR.NotificationPublishers;
78
using MediatR.Pipeline;
89
using MediatR.Registration;
@@ -15,7 +16,7 @@ public class MediatRServiceConfiguration
1516
/// Optional filter for types to register. Default value is a function returning true.
1617
/// </summary>
1718
public Func<Type, bool> TypeEvaluator { get; set; } = t => true;
18-
19+
1920
/// <summary>
2021
/// Mediator implementation type to register. Default is <see cref="Mediator"/>
2122
/// </summary>
@@ -69,31 +70,6 @@ public class MediatRServiceConfiguration
6970
/// </summary>
7071
public bool AutoRegisterRequestProcessors { get; set; }
7172

72-
/// <summary>
73-
/// Configure the maximum number of type parameters that a generic request handler can have. To Disable this constraint, set the value to 0.
74-
/// </summary>
75-
public int MaxGenericTypeParameters { get; set; } = 10;
76-
77-
/// <summary>
78-
/// Configure the maximum number of types that can close a generic request type parameter constraint. To Disable this constraint, set the value to 0.
79-
/// </summary>
80-
public int MaxTypesClosing { get; set; } = 100;
81-
82-
/// <summary>
83-
/// Configure the Maximum Amount of Generic RequestHandler Types MediatR will try to register. To Disable this constraint, set the value to 0.
84-
/// </summary>
85-
public int MaxGenericTypeRegistrations { get; set; } = 125000;
86-
87-
/// <summary>
88-
/// Configure the Timeout in Milliseconds that the GenericHandler Registration Process will exit with error. To Disable this constraint, set the value to 0.
89-
/// </summary>
90-
public int RegistrationTimeout { get; set; } = 15000;
91-
92-
/// <summary>
93-
/// Flag that controlls whether MediatR will attempt to register handlers that containg generic type parameters.
94-
/// </summary>
95-
public bool RegisterGenericHandlers { get; set; } = false;
96-
9773
/// <summary>
9874
/// Register various handlers from assembly containing given type
9975
/// </summary>
@@ -222,6 +198,37 @@ public MediatRServiceConfiguration AddOpenBehavior(Type openBehaviorType, Servic
222198
return this;
223199
}
224200

201+
/// <summary>
202+
/// Registers multiple open behavior types against the <see cref="IPipelineBehavior{TRequest,TResponse}"/> open generic interface type
203+
/// </summary>
204+
/// <param name="openBehaviorTypes">An open generic behavior type list includes multiple open generic behavior types.</param>
205+
/// <param name="serviceLifetime">Optional service lifetime, defaults to <see cref="ServiceLifetime.Transient"/>.</param>
206+
/// <returns>This</returns>
207+
public MediatRServiceConfiguration AddOpenBehaviors(IEnumerable<Type> openBehaviorTypes, ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
208+
{
209+
foreach (var openBehaviorType in openBehaviorTypes)
210+
{
211+
AddOpenBehavior(openBehaviorType, serviceLifetime);
212+
}
213+
214+
return this;
215+
}
216+
217+
/// <summary>
218+
/// Registers open behaviors against the <see cref="IPipelineBehavior{TRequest,TResponse}"/> open generic interface type
219+
/// </summary>
220+
/// <param name="openBehaviors">An open generic behavior list includes multiple <see cref="OpenBehavior"/> open generic behaviors.</param>
221+
/// <returns>This</returns>
222+
public MediatRServiceConfiguration AddOpenBehaviors(IEnumerable<OpenBehavior> openBehaviors)
223+
{
224+
foreach (var openBehavior in openBehaviors)
225+
{
226+
AddOpenBehavior(openBehavior.OpenBehaviorType!, openBehavior.ServiceLifetime);
227+
}
228+
229+
return this;
230+
}
231+
225232
/// <summary>
226233
/// Register a closed stream behavior type
227234
/// </summary>
@@ -231,7 +238,7 @@ public MediatRServiceConfiguration AddOpenBehavior(Type openBehaviorType, Servic
231238
/// <returns>This</returns>
232239
public MediatRServiceConfiguration AddStreamBehavior<TServiceType, TImplementationType>(ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
233240
=> AddStreamBehavior(typeof(TServiceType), typeof(TImplementationType), serviceLifetime);
234-
241+
235242
/// <summary>
236243
/// Register a closed stream behavior type
237244
/// </summary>
@@ -245,7 +252,7 @@ public MediatRServiceConfiguration AddStreamBehavior(Type serviceType, Type impl
245252

246253
return this;
247254
}
248-
255+
249256
/// <summary>
250257
/// Register a closed stream behavior type against all <see cref="IStreamPipelineBehavior{TRequest,TResponse}"/> implementations
251258
/// </summary>
@@ -254,7 +261,7 @@ public MediatRServiceConfiguration AddStreamBehavior(Type serviceType, Type impl
254261
/// <returns>This</returns>
255262
public MediatRServiceConfiguration AddStreamBehavior<TImplementationType>(ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
256263
=> AddStreamBehavior(typeof(TImplementationType), serviceLifetime);
257-
264+
258265
/// <summary>
259266
/// Register a closed stream behavior type against all <see cref="IStreamPipelineBehavior{TRequest,TResponse}"/> implementations
260267
/// </summary>
@@ -277,7 +284,7 @@ public MediatRServiceConfiguration AddStreamBehavior(Type implementationType, Se
277284

278285
return this;
279286
}
280-
287+
281288
/// <summary>
282289
/// Registers an open stream behavior type against the <see cref="IStreamPipelineBehavior{TRequest,TResponse}"/> open generic interface type
283290
/// </summary>
@@ -316,7 +323,7 @@ public MediatRServiceConfiguration AddOpenStreamBehavior(Type openBehaviorType,
316323
/// <returns>This</returns>
317324
public MediatRServiceConfiguration AddRequestPreProcessor<TServiceType, TImplementationType>(ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
318325
=> AddRequestPreProcessor(typeof(TServiceType), typeof(TImplementationType), serviceLifetime);
319-
326+
320327
/// <summary>
321328
/// Register a closed request pre processor type
322329
/// </summary>
@@ -360,10 +367,10 @@ public MediatRServiceConfiguration AddRequestPreProcessor(Type implementationTyp
360367
{
361368
RequestPreProcessorsToRegister.Add(new ServiceDescriptor(implementedPreProcessorType, implementationType, serviceLifetime));
362369
}
363-
370+
364371
return this;
365372
}
366-
373+
367374
/// <summary>
368375
/// Registers an open request pre processor type against the <see cref="IRequestPreProcessor{TRequest}"/> open generic interface type
369376
/// </summary>
@@ -392,7 +399,7 @@ public MediatRServiceConfiguration AddOpenRequestPreProcessor(Type openBehaviorT
392399

393400
return this;
394401
}
395-
402+
396403
/// <summary>
397404
/// Register a closed request post processor type
398405
/// </summary>
@@ -402,7 +409,7 @@ public MediatRServiceConfiguration AddOpenRequestPreProcessor(Type openBehaviorT
402409
/// <returns>This</returns>
403410
public MediatRServiceConfiguration AddRequestPostProcessor<TServiceType, TImplementationType>(ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
404411
=> AddRequestPostProcessor(typeof(TServiceType), typeof(TImplementationType), serviceLifetime);
405-
412+
406413
/// <summary>
407414
/// Register a closed request post processor type
408415
/// </summary>
@@ -416,7 +423,7 @@ public MediatRServiceConfiguration AddRequestPostProcessor(Type serviceType, Typ
416423

417424
return this;
418425
}
419-
426+
420427
/// <summary>
421428
/// Register a closed request post processor type against all <see cref="IRequestPostProcessor{TRequest,TResponse}"/> implementations
422429
/// </summary>
@@ -425,7 +432,7 @@ public MediatRServiceConfiguration AddRequestPostProcessor(Type serviceType, Typ
425432
/// <returns>This</returns>
426433
public MediatRServiceConfiguration AddRequestPostProcessor<TImplementationType>(ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
427434
=> AddRequestPostProcessor(typeof(TImplementationType), serviceLifetime);
428-
435+
429436
/// <summary>
430437
/// Register a closed request post processor type against all <see cref="IRequestPostProcessor{TRequest,TResponse}"/> implementations
431438
/// </summary>
@@ -447,7 +454,7 @@ public MediatRServiceConfiguration AddRequestPostProcessor(Type implementationTy
447454
}
448455
return this;
449456
}
450-
457+
451458
/// <summary>
452459
/// Registers an open request post processor type against the <see cref="IRequestPostProcessor{TRequest,TResponse}"/> open generic interface type
453460
/// </summary>

src/MediatR/MicrosoftExtensionsDI/ServiceCollectionExtensions.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,31 @@ public static class ServiceCollectionExtensions
2323
/// <param name="services">Service collection</param>
2424
/// <param name="configuration">The action used to configure the options</param>
2525
/// <returns>Service collection</returns>
26-
public static IServiceCollection AddMediatR(this IServiceCollection services,
26+
public static IServiceCollection AddMediatR(this IServiceCollection services,
2727
Action<MediatRServiceConfiguration> configuration)
2828
{
2929
var serviceConfig = new MediatRServiceConfiguration();
3030

3131
configuration.Invoke(serviceConfig);
3232

3333
return services.AddMediatR(serviceConfig);
34-
}
35-
34+
}
35+
3636
/// <summary>
3737
/// Registers handlers and mediator types from the specified assemblies
3838
/// </summary>
3939
/// <param name="services">Service collection</param>
4040
/// <param name="configuration">Configuration options</param>
4141
/// <returns>Service collection</returns>
42-
public static IServiceCollection AddMediatR(this IServiceCollection services,
42+
public static IServiceCollection AddMediatR(this IServiceCollection services,
4343
MediatRServiceConfiguration configuration)
4444
{
4545
if (!configuration.AssembliesToRegister.Any())
4646
{
4747
throw new ArgumentException("No assemblies found to scan. Supply at least one assembly to scan for handlers.");
4848
}
4949

50-
ServiceRegistrar.SetGenericRequestHandlerRegistrationLimitations(configuration);
51-
52-
ServiceRegistrar.AddMediatRClassesWithTimeout(services, configuration);
50+
ServiceRegistrar.AddMediatRClasses(services, configuration);
5351

5452
ServiceRegistrar.AddRequiredServices(services, configuration);
5553

0 commit comments

Comments
 (0)