Skip to content

Commit

Permalink
Merge pull request #1068 from zachpainter77/master
Browse files Browse the repository at this point in the history
Remove Generic Handler Support / Implementation (Leaving Failing Tests)
  • Loading branch information
jbogard authored Sep 11, 2024
2 parents fb30902 + 447792f commit cf5bdf5
Show file tree
Hide file tree
Showing 10 changed files with 379 additions and 443 deletions.
1 change: 0 additions & 1 deletion src/MediatR.Contracts/MediatR.Contracts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<Description>Contracts package for requests, responses, and notifications</Description>
<Copyright>Copyright Jimmy Bogard</Copyright>
<TargetFramework>netstandard2.0</TargetFramework>
<Nullable>enable</Nullable>
<Features>strict</Features>
<PackageTags>mediator;request;response;queries;commands;notifications</PackageTags>
<SignAssembly>true</SignAssembly>
Expand Down
20 changes: 20 additions & 0 deletions src/MediatR/Entities/OpenBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using Microsoft.Extensions.DependencyInjection;

namespace MediatR.Entities;
/// <summary>
/// Creates open behavior entity.
/// </summary>
public class OpenBehavior
{
public OpenBehavior(Type openBehaviorType, ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
{
OpenBehaviorType = openBehaviorType;
ServiceLifetime = serviceLifetime;
}

public Type? OpenBehaviorType { get; }
public ServiceLifetime ServiceLifetime { get; }


}
2 changes: 1 addition & 1 deletion src/MediatR/IPipelineBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace MediatR;
/// </summary>
/// <typeparam name="TResponse">Response type</typeparam>
/// <returns>Awaitable task returning a <typeparamref name="TResponse"/></returns>
public delegate Task<TResponse> RequestHandlerDelegate<TResponse>();
public delegate Task<TResponse> RequestHandlerDelegate<TResponse>(CancellationToken t = default);

/// <summary>
/// Pipeline behavior to surround the inner handler.
Expand Down
83 changes: 45 additions & 38 deletions src/MediatR/MicrosoftExtensionsDI/MediatrServiceConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Reflection;
using MediatR;
using MediatR.Entities;
using MediatR.NotificationPublishers;
using MediatR.Pipeline;
using MediatR.Registration;
Expand All @@ -15,7 +16,7 @@ public class MediatRServiceConfiguration
/// Optional filter for types to register. Default value is a function returning true.
/// </summary>
public Func<Type, bool> TypeEvaluator { get; set; } = t => true;

/// <summary>
/// Mediator implementation type to register. Default is <see cref="Mediator"/>
/// </summary>
Expand Down Expand Up @@ -69,31 +70,6 @@ public class MediatRServiceConfiguration
/// </summary>
public bool AutoRegisterRequestProcessors { get; set; }

/// <summary>
/// Configure the maximum number of type parameters that a generic request handler can have. To Disable this constraint, set the value to 0.
/// </summary>
public int MaxGenericTypeParameters { get; set; } = 10;

/// <summary>
/// Configure the maximum number of types that can close a generic request type parameter constraint. To Disable this constraint, set the value to 0.
/// </summary>
public int MaxTypesClosing { get; set; } = 100;

/// <summary>
/// Configure the Maximum Amount of Generic RequestHandler Types MediatR will try to register. To Disable this constraint, set the value to 0.
/// </summary>
public int MaxGenericTypeRegistrations { get; set; } = 125000;

/// <summary>
/// Configure the Timeout in Milliseconds that the GenericHandler Registration Process will exit with error. To Disable this constraint, set the value to 0.
/// </summary>
public int RegistrationTimeout { get; set; } = 15000;

/// <summary>
/// Flag that controlls whether MediatR will attempt to register handlers that containg generic type parameters.
/// </summary>
public bool RegisterGenericHandlers { get; set; } = false;

/// <summary>
/// Register various handlers from assembly containing given type
/// </summary>
Expand Down Expand Up @@ -222,6 +198,37 @@ public MediatRServiceConfiguration AddOpenBehavior(Type openBehaviorType, Servic
return this;
}

/// <summary>
/// Registers multiple open behavior types against the <see cref="IPipelineBehavior{TRequest,TResponse}"/> open generic interface type
/// </summary>
/// <param name="openBehaviorTypes">An open generic behavior type list includes multiple open generic behavior types.</param>
/// <param name="serviceLifetime">Optional service lifetime, defaults to <see cref="ServiceLifetime.Transient"/>.</param>
/// <returns>This</returns>
public MediatRServiceConfiguration AddOpenBehaviors(IEnumerable<Type> openBehaviorTypes, ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
{
foreach (var openBehaviorType in openBehaviorTypes)
{
AddOpenBehavior(openBehaviorType, serviceLifetime);
}

return this;
}

/// <summary>
/// Registers open behaviors against the <see cref="IPipelineBehavior{TRequest,TResponse}"/> open generic interface type
/// </summary>
/// <param name="openBehaviors">An open generic behavior list includes multiple <see cref="OpenBehavior"/> open generic behaviors.</param>
/// <returns>This</returns>
public MediatRServiceConfiguration AddOpenBehaviors(IEnumerable<OpenBehavior> openBehaviors)
{
foreach (var openBehavior in openBehaviors)
{
AddOpenBehavior(openBehavior.OpenBehaviorType!, openBehavior.ServiceLifetime);
}

return this;
}

/// <summary>
/// Register a closed stream behavior type
/// </summary>
Expand All @@ -231,7 +238,7 @@ public MediatRServiceConfiguration AddOpenBehavior(Type openBehaviorType, Servic
/// <returns>This</returns>
public MediatRServiceConfiguration AddStreamBehavior<TServiceType, TImplementationType>(ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
=> AddStreamBehavior(typeof(TServiceType), typeof(TImplementationType), serviceLifetime);

/// <summary>
/// Register a closed stream behavior type
/// </summary>
Expand All @@ -245,7 +252,7 @@ public MediatRServiceConfiguration AddStreamBehavior(Type serviceType, Type impl

return this;
}

/// <summary>
/// Register a closed stream behavior type against all <see cref="IStreamPipelineBehavior{TRequest,TResponse}"/> implementations
/// </summary>
Expand All @@ -254,7 +261,7 @@ public MediatRServiceConfiguration AddStreamBehavior(Type serviceType, Type impl
/// <returns>This</returns>
public MediatRServiceConfiguration AddStreamBehavior<TImplementationType>(ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
=> AddStreamBehavior(typeof(TImplementationType), serviceLifetime);

/// <summary>
/// Register a closed stream behavior type against all <see cref="IStreamPipelineBehavior{TRequest,TResponse}"/> implementations
/// </summary>
Expand All @@ -277,7 +284,7 @@ public MediatRServiceConfiguration AddStreamBehavior(Type implementationType, Se

return this;
}

/// <summary>
/// Registers an open stream behavior type against the <see cref="IStreamPipelineBehavior{TRequest,TResponse}"/> open generic interface type
/// </summary>
Expand Down Expand Up @@ -316,7 +323,7 @@ public MediatRServiceConfiguration AddOpenStreamBehavior(Type openBehaviorType,
/// <returns>This</returns>
public MediatRServiceConfiguration AddRequestPreProcessor<TServiceType, TImplementationType>(ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
=> AddRequestPreProcessor(typeof(TServiceType), typeof(TImplementationType), serviceLifetime);

/// <summary>
/// Register a closed request pre processor type
/// </summary>
Expand Down Expand Up @@ -360,10 +367,10 @@ public MediatRServiceConfiguration AddRequestPreProcessor(Type implementationTyp
{
RequestPreProcessorsToRegister.Add(new ServiceDescriptor(implementedPreProcessorType, implementationType, serviceLifetime));
}

return this;
}

/// <summary>
/// Registers an open request pre processor type against the <see cref="IRequestPreProcessor{TRequest}"/> open generic interface type
/// </summary>
Expand Down Expand Up @@ -392,7 +399,7 @@ public MediatRServiceConfiguration AddOpenRequestPreProcessor(Type openBehaviorT

return this;
}

/// <summary>
/// Register a closed request post processor type
/// </summary>
Expand All @@ -402,7 +409,7 @@ public MediatRServiceConfiguration AddOpenRequestPreProcessor(Type openBehaviorT
/// <returns>This</returns>
public MediatRServiceConfiguration AddRequestPostProcessor<TServiceType, TImplementationType>(ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
=> AddRequestPostProcessor(typeof(TServiceType), typeof(TImplementationType), serviceLifetime);

/// <summary>
/// Register a closed request post processor type
/// </summary>
Expand All @@ -416,7 +423,7 @@ public MediatRServiceConfiguration AddRequestPostProcessor(Type serviceType, Typ

return this;
}

/// <summary>
/// Register a closed request post processor type against all <see cref="IRequestPostProcessor{TRequest,TResponse}"/> implementations
/// </summary>
Expand All @@ -425,7 +432,7 @@ public MediatRServiceConfiguration AddRequestPostProcessor(Type serviceType, Typ
/// <returns>This</returns>
public MediatRServiceConfiguration AddRequestPostProcessor<TImplementationType>(ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
=> AddRequestPostProcessor(typeof(TImplementationType), serviceLifetime);

/// <summary>
/// Register a closed request post processor type against all <see cref="IRequestPostProcessor{TRequest,TResponse}"/> implementations
/// </summary>
Expand All @@ -447,7 +454,7 @@ public MediatRServiceConfiguration AddRequestPostProcessor(Type implementationTy
}
return this;
}

/// <summary>
/// Registers an open request post processor type against the <see cref="IRequestPostProcessor{TRequest,TResponse}"/> open generic interface type
/// </summary>
Expand Down
12 changes: 5 additions & 7 deletions src/MediatR/MicrosoftExtensionsDI/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,31 @@ public static class ServiceCollectionExtensions
/// <param name="services">Service collection</param>
/// <param name="configuration">The action used to configure the options</param>
/// <returns>Service collection</returns>
public static IServiceCollection AddMediatR(this IServiceCollection services,
public static IServiceCollection AddMediatR(this IServiceCollection services,
Action<MediatRServiceConfiguration> configuration)
{
var serviceConfig = new MediatRServiceConfiguration();

configuration.Invoke(serviceConfig);

return services.AddMediatR(serviceConfig);
}
}

/// <summary>
/// Registers handlers and mediator types from the specified assemblies
/// </summary>
/// <param name="services">Service collection</param>
/// <param name="configuration">Configuration options</param>
/// <returns>Service collection</returns>
public static IServiceCollection AddMediatR(this IServiceCollection services,
public static IServiceCollection AddMediatR(this IServiceCollection services,
MediatRServiceConfiguration configuration)
{
if (!configuration.AssembliesToRegister.Any())
{
throw new ArgumentException("No assemblies found to scan. Supply at least one assembly to scan for handlers.");
}

ServiceRegistrar.SetGenericRequestHandlerRegistrationLimitations(configuration);

ServiceRegistrar.AddMediatRClassesWithTimeout(services, configuration);
ServiceRegistrar.AddMediatRClasses(services, configuration);

ServiceRegistrar.AddRequiredServices(services, configuration);

Expand Down
Loading

0 comments on commit cf5bdf5

Please sign in to comment.