Files
PnvPanel/backend/src/PnvPanel.Application/DependencyInjection.cs
T
Leonid Pershin 14b64a3140
CI / Backend (build + test) (push) Successful in 1m26s
CI / Frontend (lint + typecheck + build) (push) Successful in 30s
Implement activation checks across various commands and queries
- Introduced `IRequiresActivation` interface to enforce activation requirements for multiple commands and queries, ensuring that only activated users can create, edit, or access configurations, news, and applications.
- Updated the `RequireActivationBehavior` to handle activation checks uniformly, returning appropriate errors for unauthenticated or inactive users.
- Enhanced error handling by adding `NotActivated` error to provide clear feedback for users attempting to access restricted features.
- Updated documentation to reflect the new activation requirements and their implications on user access and functionality.
2026-07-13 18:51:03 +03:00

46 lines
2.1 KiB
C#

using System.Reflection;
using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
using PnvPanel.Application.Common.Behaviors;
using PnvPanel.Application.Common.Messaging;
namespace PnvPanel.Application;
/// <summary>
/// Точка регистрации сервисов слоя Application: собственный CQRS-диспетчер (ISender),
/// хендлеры/валидаторы (авто-сканирование сборки) и pipeline behaviors — в порядке выполнения.
/// </summary>
public static class DependencyInjection
{
public static IServiceCollection AddApplication(this IServiceCollection services)
{
var assembly = typeof(DependencyInjection).Assembly;
services.AddScoped<ISender, Sender>();
RegisterClosedGeneric(services, assembly, typeof(ICommandHandler<,>));
RegisterClosedGeneric(services, assembly, typeof(IQueryHandler<,>));
RegisterClosedGeneric(services, assembly, typeof(IValidator<>));
// Порядок важен: Logging (снаружи) -> Validation -> RequireActivation -> UnitOfWork (ближе к хендлеру).
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>));
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequireActivationBehavior<,>));
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(UnitOfWorkBehavior<,>));
return services;
}
private static void RegisterClosedGeneric(IServiceCollection services, Assembly assembly, Type openInterface)
{
var implementations = assembly.GetTypes()
.Where(t => t is { IsClass: true, IsAbstract: false })
.SelectMany(t => t.GetInterfaces()
.Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == openInterface)
.Select(i => (Service: i, Implementation: t)));
foreach (var (service, implementation) in implementations)
services.AddScoped(service, implementation);
}
}