-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSoftawareCqsFluentValidationDecoratorBuilderExtensions.cs
More file actions
37 lines (32 loc) · 1.62 KB
/
SoftawareCqsFluentValidationDecoratorBuilderExtensions.cs
File metadata and controls
37 lines (32 loc) · 1.62 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
using FluentValidation;
using softaware.Cqs;
using softaware.Cqs.Decorators.FluentValidation;
namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
/// Provides extension methods to add FluentValidation decorators.
/// </summary>
public static class SoftawareCqsFluentValidationDecoratorBuilderExtensions
{
/// <summary>
/// Registers the fluent validation request handler decorators as well as all validators from the specified assemblies.
/// </summary>
/// <param name="decoratorBuilder">The CQS decorator builder.</param>
/// <param name="validatorTypesBuilderAction">The types builder for registering assemblies from where to find <see cref="IValidator{T}"/> instances.</param>
/// <returns>The CQS decorator builder.</returns>
public static SoftawareCqsDecoratorBuilder AddFluentValidationDecorators(
this SoftawareCqsDecoratorBuilder decoratorBuilder,
Action<SoftawareCqsTypesBuilder> validatorTypesBuilderAction)
{
var typesBuilder = new SoftawareCqsTypesBuilder();
validatorTypesBuilderAction.Invoke(typesBuilder);
// Register all fluent validators which are available in the specified assemblies.
decoratorBuilder.Services
.Scan(scan => scan
.FromAssemblies(typesBuilder.RegisteredAssemblies)
.AddClasses(classes => classes.AssignableTo(typeof(IValidator<>)))
.AsImplementedInterfaces()
.WithTransientLifetime());
decoratorBuilder.AddRequestHandlerDecorator(typeof(FluentValidationRequestHandlerDecorator<,>));
return decoratorBuilder;
}
}