classlib/ExpenseTracker.Application/DependencyInjection.cs
2024-08-07 21:12:02 +03:00

55 lines
1.9 KiB
C#

using System.Reflection;
using FluentValidation;
using MediatR;
using MediatR.Behaviors.Authorization.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using ExpenseTracker.Application.Common.Authorization;
using ExpenseTracker.Application.Common.Behaviours;
namespace ExpenseTracker.Application;
public static class DependencyInjection
{
public static IServiceCollection AddApplication(this IServiceCollection services)
{
services
.AddFluentValidation()
.AddAutoMapper()
.AddMediatR();
return services;
}
private static IServiceCollection AddFluentValidation(this IServiceCollection services)
{
services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());
return services;
}
private static IServiceCollection AddAutoMapper(this IServiceCollection services)
{
services.AddAutoMapper(Assembly.GetExecutingAssembly());
return services;
}
private static IServiceCollection AddMediatR(this IServiceCollection services)
{
// Adds the transient pipeline behavior and additionally registers all `IAuthorizationHandlers` for a given assembly
services.AddMediatorAuthorization(
Assembly.GetExecutingAssembly(),
options => options.UseUnauthorizedResultHandlerStrategy(new CustomUnauthorizedResultHandler())
);
// Register all `IAuthorizer` implementations for a given assembly
services.AddAuthorizersFromAssembly(Assembly.GetExecutingAssembly());
services.AddMediatR(configuration =>
{
configuration.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly());
configuration.AddBehavior(typeof(IPipelineBehavior<,>), typeof(LoggingBehaviour<,>));
configuration.AddBehavior(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>));
});
return services;
}
}