mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 12:11:10 +00:00
Defines base implementation for mediatr and fluentvalidation with samples
This commit is contained in:
1 parent
76db0c1597
commit
9a5c7473f9
33 files changed
+1123
-131
No files matched your search
@@ -3,7 +3,11 @@ using FluentValidation;
|
||||
using Lumina.Application.Common.Behaviors;
|
||||
using Mediator;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.DependencyInjection;
|
||||
@@ -33,6 +37,32 @@ public static class ApplicationLayerServices
|
||||
// register fluent validators
|
||||
services.AddValidatorsFromAssembly(typeof(ApplicationLayerServices).Assembly);
|
||||
|
||||
Type[] handlers =
|
||||
[
|
||||
typeof(CQRS.ICommandHandler<,>),
|
||||
typeof(CQRS.IQueryHandler<,>),
|
||||
typeof(Infrastructure.Validation.IValidator<>),
|
||||
];
|
||||
|
||||
IEnumerable<Type> types = Assembly.GetExecutingAssembly()
|
||||
.GetTypes()
|
||||
.Where(type => !type.IsInterface && !type.IsAbstract);
|
||||
|
||||
var registrations = types
|
||||
.SelectMany(type => type.GetInterfaces(),
|
||||
(implementation, service) => new { Service = service, Implmentation = implementation })
|
||||
.Where(type => type.Service.IsGenericType && handlers.Contains(type.Service.GetGenericTypeDefinition()));
|
||||
|
||||
foreach (var registration in registrations)
|
||||
{
|
||||
if (!services.Any(service => service.ServiceType == registration.Service))
|
||||
{
|
||||
if (registration.Service.GetGenericTypeDefinition() == typeof(Infrastructure.Validation.IValidator<>))
|
||||
services.AddSingleton(registration.Service, registration.Implmentation);
|
||||
else
|
||||
services.AddScoped(registration.Service, registration.Implmentation);
|
||||
}
|
||||
}
|
||||
return services;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user