-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #828 from jbogard/consolidate-registration
Consolidate registration to single configuration object and optimize registration
- Loading branch information
Showing
15 changed files
with
148 additions
and
116 deletions.
There are no files selected for viewing
This file contains 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
57 changes: 36 additions & 21 deletions
57
src/MediatR/MicrosoftExtensionsDI/MediatrServiceConfiguration.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,63 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using MediatR; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
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; | ||
|
||
public MediatRServiceConfiguration() | ||
{ | ||
MediatorImplementationType = typeof(Mediator); | ||
Lifetime = ServiceLifetime.Transient; | ||
} | ||
internal List<Assembly> AssembliesToRegister { get; } = new(); | ||
|
||
public MediatRServiceConfiguration Using<TMediator>() where TMediator : IMediator | ||
{ | ||
MediatorImplementationType = typeof(TMediator); | ||
return this; | ||
} | ||
public List<ServiceDescriptor> BehaviorsToRegister { get; } = new(); | ||
|
||
public MediatRServiceConfiguration RegisterServicesFromAssemblyContaining<T>() | ||
=> RegisterServicesFromAssemblyContaining(typeof(T)); | ||
|
||
public MediatRServiceConfiguration RegisterServicesFromAssemblyContaining(Type type) | ||
=> RegisterServicesFromAssembly(type.Assembly); | ||
|
||
public MediatRServiceConfiguration AsSingleton() | ||
public MediatRServiceConfiguration RegisterServicesFromAssembly(Assembly assembly) | ||
{ | ||
Lifetime = ServiceLifetime.Singleton; | ||
AssembliesToRegister.Add(assembly); | ||
|
||
return this; | ||
} | ||
|
||
public MediatRServiceConfiguration AsScoped() | ||
public MediatRServiceConfiguration RegisterServicesFromAssemblies( | ||
params Assembly[] assemblies) | ||
{ | ||
Lifetime = ServiceLifetime.Scoped; | ||
AssembliesToRegister.AddRange(assemblies); | ||
|
||
return this; | ||
} | ||
|
||
public MediatRServiceConfiguration AsTransient() | ||
public MediatRServiceConfiguration AddBehavior<TServiceType, TImplementationType>( | ||
ServiceLifetime serviceLifetime = ServiceLifetime.Transient) => | ||
AddBehavior(typeof(TServiceType), typeof(TImplementationType), serviceLifetime); | ||
|
||
public MediatRServiceConfiguration AddBehavior( | ||
Type serviceType, | ||
Type implementationType, | ||
ServiceLifetime serviceLifetime = ServiceLifetime.Transient) | ||
{ | ||
Lifetime = ServiceLifetime.Transient; | ||
BehaviorsToRegister.Add(new ServiceDescriptor(serviceType, implementationType, serviceLifetime)); | ||
|
||
return this; | ||
} | ||
|
||
public MediatRServiceConfiguration WithEvaluator(Func<Type, bool> evaluator) | ||
public MediatRServiceConfiguration AddOpenBehavior(Type openBehaviorType, ServiceLifetime serviceLifetime = ServiceLifetime.Transient) | ||
{ | ||
TypeEvaluator = evaluator; | ||
var serviceType = typeof(IPipelineBehavior<,>); | ||
|
||
BehaviorsToRegister.Add(new ServiceDescriptor(serviceType, openBehaviorType, serviceLifetime)); | ||
|
||
return this; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.