Consolidate registration to single configuration object and optimize registration#828
Merged
Consolidate registration to single configuration object and optimize registration#828
Conversation
Collaborator
Author
|
Fixes #829 |
no1melman
approved these changes
Feb 2, 2023
This was referenced Sep 28, 2025
This was referenced Oct 27, 2025
This was referenced Nov 10, 2025
This was referenced Dec 1, 2025
This was referenced Dec 16, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Consolidates registration and scanning into a single configuration object; Only registers extra behaviors (pre/post processor, etc.) if there are any available.
API changes
All configuration would be consolidated into a single method (instead of parameters in an overload):
public static class ServiceCollectionExtensions { - public static IServiceCollection AddMediatR(this IServiceCollection services, params Assembly[] assemblies) - public static IServiceCollection AddMediatR(this IServiceCollection services, Action<MediatRServiceConfiguration>? configuration, params Assembly[] assemblies) - public static IServiceCollection AddMediatR(this IServiceCollection services, IEnumerable<Assembly> assemblies, Action<MediatRServiceConfiguration>? configuration) - public static IServiceCollection AddMediatR(this IServiceCollection services, params Type[] handlerAssemblyMarkerTypes) - public static IServiceCollection AddMediatR(this IServiceCollection services, Action<MediatRServiceConfiguration>? configuration, params Type[] handlerAssemblyMarkerTypes) - public static IServiceCollection AddMediatR(this IServiceCollection services, IEnumerable<Type> handlerAssemblyMarkerTypes, Action<MediatRServiceConfiguration>? configuration) + public static IServiceCollection AddMediatR(this IServiceCollection services, Action<MediatRServiceConfiguration> configuration) }All the parameters are now moved to the
MediatRServiceConfigurationobject, including a pass-through method to add behaviors:public class MediatRServiceConfiguration { - public Func<Type, bool> TypeEvaluator { get; private set; } = t => true; - public Type MediatorImplementationType { get; private set; } - public ServiceLifetime Lifetime { get; private set; } + public Func<Type, bool> TypeEvaluator { get; set; } = t => true; + public Type MediatorImplementationType { get; set; } = typeof(Mediator); + public ServiceLifetime Lifetime { get; set; } = ServiceLifetime.Transient; + public RequestExceptionActionProcessorStrategy RequestExceptionActionProcessorStrategy { get; set; } = RequestExceptionActionProcessorStrategy.ApplyForUnhandledExceptions; + internal List<Assembly> AssembliesToRegister { get; } = new(); + public List<ServiceDescriptor> BehaviorsToRegister { get; } = new(); // These methods removed to instead use the properties directly - public MediatRServiceConfiguration Using<TMediator>() where TMediator : IMediator - public MediatRServiceConfiguration AsSingleton() - public MediatRServiceConfiguration AsScoped() - public MediatRServiceConfiguration AsTransient() - public MediatRServiceConfiguration WithEvaluator(Func<Type, bool> evaluator) // These replace the overloads for passing in types or assemblies to scan + public MediatRServiceConfiguration RegisterServicesFromAssemblyContaining<T>() + public MediatRServiceConfiguration RegisterServicesFromAssemblyContaining(Type type) + public MediatRServiceConfiguration RegisterServicesFromAssembly(Assembly assembly) + public MediatRServiceConfiguration RegisterServicesFromAssemblies(params Assembly[] assemblies) // These are new methods to add concrete behaviors but merely passthrough the configuration to the underlying `IServiceCollection` + public MediatRServiceConfiguration AddBehavior<TServiceType, TImplementationType>(ServiceLifetime serviceLifetime = ServiceLifetime.Transient) + public MediatRServiceConfiguration AddBehavior(Type serviceType, Type implementationType, ServiceLifetime serviceLifetime = ServiceLifetime.Transient) // This method is for registering an open behavior still passing through the configuration + public MediatRServiceConfiguration AddOpenBehavior(Type openBehaviorType, ServiceLifetime serviceLifetime = ServiceLifetime.Transient)Behavior changes
The service scanning was modified to only register with the container the "built-in" behaviors of:
RequestPreProcessorBehavior<,>RequestPostProcessorBehavior<,>RequestExceptionProcessorBehavior<,>RequestExceptionActionProcessorBehavior<,>When "Sub-behaviors" of those types are found in the
IServiceCollection.There are performance implications to resolving those sub-behaviors, so if they don't exist, the built-in behavior won't be registered.