using AutoMapper; using Kean.Domain.Basic.Commands; using Kean.Domain.Basic.Events; using Kean.Domain.Basic.Repositories; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace Kean.Domain.Basic.CommandHandlers { /// /// 删除角色命令处理程序 /// public sealed class DeleteRoleCommandHandler : CommandHandler { private readonly ICommandBus _commandBus; // 命令总线 private readonly IMapper _mapper; // 模型映射 private readonly IRoleRepository _roleRepository; // 角色仓库 /// /// 依赖注入 /// public DeleteRoleCommandHandler( ICommandBus commandBus, IMapper mapper, IRoleRepository roleRepository) { _commandBus = commandBus; _mapper = mapper; _roleRepository = roleRepository; } /// /// 处理程序 /// public override async Task Handle(DeleteRoleCommand command, CancellationToken cancellationToken) { if (command.ValidationResult.IsValid) { List result = new(); foreach (var item in command.Id) { await _roleRepository.Delete(item); result.Add(item); } command.Id = result; await _commandBus.Trigger(_mapper.Map(command), cancellationToken); } else { await _commandBus.Notify(command.ValidationResult, cancellationToken: cancellationToken); } } } }