You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.4 KiB
46 lines
1.4 KiB
using Microsoft.Extensions.DependencyInjection;
|
|
using MediatR;
|
|
using FluentValidation;
|
|
using CellularManagement.Application.Behaviours;
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
namespace CellularManagement.Application;
|
|
|
|
/// <summary>
|
|
/// 应用层依赖注入扩展
|
|
/// 负责注册应用层所需的所有服务
|
|
/// </summary>
|
|
public static class DependencyInjection
|
|
{
|
|
/// <summary>
|
|
/// 添加应用层服务
|
|
/// 包括命令/查询处理、验证等服务
|
|
/// </summary>
|
|
/// <param name="services">服务集合</param>
|
|
/// <param name="configuration">配置</param>
|
|
/// <returns>服务集合</returns>
|
|
public static IServiceCollection AddApplication(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(services);
|
|
ArgumentNullException.ThrowIfNull(configuration);
|
|
|
|
// 获取当前程序集
|
|
var assembly = typeof(DependencyInjection).Assembly;
|
|
|
|
// 配置 MediatR 服务
|
|
// 1. 注册程序集中的所有处理器
|
|
// 2. 添加验证行为中间件,用于自动验证请求
|
|
services.AddMediatR(configuration =>
|
|
configuration
|
|
.RegisterServicesFromAssembly(assembly)
|
|
.AddOpenBehavior(typeof(ValidationBehaviour<,>)));
|
|
|
|
// 注册验证器
|
|
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>));
|
|
|
|
return services;
|
|
}
|
|
}
|