← Каталог
Clean Architecture на ASP.NET Core — Application — команда и обработчик
Фрагмент из «Clean Architecture на ASP.NET Core»: Application — команда и обработчик.
public record CreateTodoListCommand : IRequest<int>
{
public string? Title { get; init; }
public string? Colour { get; init; }
}
public class CreateTodoListCommandHandler : IRequestHandler<CreateTodoListCommand, int>
{
private readonly IApplicationDbContext _context;
public CreateTodoListCommandHandler(IApplicationDbContext context) => _context = context;
public async Task<int> Handle(CreateTodoListCommand request, CancellationToken cancellationToken)
{
var entity = new TodoList
{
Title = request.Title,
Colour = Colour.From(request.Colour ?? Colour.Grey)
};
_context.TodoLists.Add(entity);
await _context.SaveChangesAsync(cancellationToken);
return entity.Id;
}
} public record CreateTodoListCommand : IRequest<int>
{
public string? Title { get; init; }
public string? Colour { get; init; }
}
public class CreateTodoListCommandHandler : IRequestHandler<CreateTodoListCommand, int>
{
private readonly IApplicationDbContext _context;
public CreateTodoListCommandHandler(IApplicationDbContext context) => _context = context;
public async Task<int> Handle(CreateTodoListCommand request, CancellationToken cancellationToken)
{
var entity = new TodoList
{
Title = request.Title,
Colour = Colour.From(request.Colour ?? Colour.Grey)
};
_context.TodoLists.Add(entity);
await _context.SaveChangesAsync(cancellationToken);
return entity.Id;
}
}