千家信息网

.NET 6开发TodoList应用之如何实现PUT请求

发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,这篇文章将为大家详细讲解有关.NET 6开发TodoList应用之如何实现PUT请求,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。需求PUT请求本身其实可说的并不多
千家信息网最后更新 2025年02月01日.NET 6开发TodoList应用之如何实现PUT请求

这篇文章将为大家详细讲解有关.NET 6开发TodoList应用之如何实现PUT请求,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

需求

PUT请求本身其实可说的并不多,过程也和创建基本类似。在这篇文章中,重点是填上之前文章里留的一个坑,我们曾经给TodoItem定义过一个标记完成的领域事件:TodoItemCompletedEvent,在SaveChangesAsync方法里做了一个DispatchEvents的操作。并且在DomainEventService实现IDomainEventService的Publish方法中暂时以下面的代码代替了:

DomainEventService.cs

public async Task Publish(DomainEvent domainEvent){    // 在这里暂时什么都不做,到CQRS那一篇的时候再回来补充这里的逻辑    _logger.LogInformation("Publishing domain event. Event - {event}", domainEvent.GetType().Name);}

在前几篇应用MediatR实现CQRS的过程中,我们主要是和IRequest/IRequestHandler打的交道。那么本文将会涉及到另外一对常用的接口:INotification/INotificationHandler,来实现领域事件的处理。

目标

1.实现PUT请求;

2.实现领域事件的响应处理;

原理与思路

实现PUT请求的原理和思路与实现POST请求类似,就不展开了。关于实现领域事件响应的部分,我们需要实现INotification/INotificationHandler接口,并改写Publish的实现,让它能发布领域事件通知。

实现

PUT请求

我们拿更新TodoItem的完成状态来举例,首先来自定义一个领域异常NotFoundException,位于Application/Common/Exceptions里:

NotFoundException.cs

namespace TodoList.Application.Common.Exceptions;public class NotFoundException : Exception{    public NotFoundException() : base() { }    public NotFoundException(string message) : base(message) { }    public NotFoundException(string message, Exception innerException) : base(message, innerException) { }    public NotFoundException(string name, object key) : base($"Entity \"{name}\" ({key}) was not found.") { }}

创建对应的Command:

UpdateTodoItemCommand.cs

using MediatR;using TodoList.Application.Common.Exceptions;using TodoList.Application.Common.Interfaces;using TodoList.Domain.Entities;namespace TodoList.Application.TodoItems.Commands.UpdateTodoItem;public class UpdateTodoItemCommand : IRequest{    public Guid Id { get; set; }    public string? Title { get; set; }    public bool Done { get; set; }}public class UpdateTodoItemCommandHandler : IRequestHandler{    private readonly IRepository _repository;    public UpdateTodoItemCommandHandler(IRepository repository)    {        _repository = repository;    }    public async Task Handle(UpdateTodoItemCommand request, CancellationToken cancellationToken)    {        var entity = await _repository.GetAsync(request.Id);        if (entity == null)        {            throw new NotFoundException(nameof(TodoItem), request.Id);        }        entity.Title = request.Title ?? entity.Title;        entity.Done = request.Done;        await _repository.UpdateAsync(entity, cancellationToken);        return entity;    }}

实现Controller:

TodoItemController.cs

[HttpPut("{id:Guid}")]public async Task> Update(Guid id, [FromBody] UpdateTodoItemCommand command){    if (id != command.Id)    {        return ApiResponse.Fail("Query id not match witch body");    }    return ApiResponse.Success(await _mediator.Send(command));}

领域事件的发布和响应

首先需要在Application/Common/Models定义一个泛型类,实现INotification接口,用于发布领域事件:

DomainEventNotification.cs

using MediatR;using TodoList.Domain.Base;namespace TodoList.Application.Common.Models;public class DomainEventNotification : INotification where TDomainEvent : DomainEvent{    public DomainEventNotification(TDomainEvent domainEvent)    {        DomainEvent = domainEvent;    }    public TDomainEvent DomainEvent { get; }}

接下来在Application/TodoItems/EventHandlers中创建对应的Handler:

TodoItemCompletedEventHandler.cs

using MediatR;using Microsoft.Extensions.Logging;using TodoList.Application.Common.Models;using TodoList.Domain.Events;namespace TodoList.Application.TodoItems.EventHandlers;public class TodoItemCompletedEventHandler : INotificationHandler>{    private readonly ILogger _logger;    public TodoItemCompletedEventHandler(ILogger logger)    {        _logger = logger;    }    public Task Handle(DomainEventNotification notification, CancellationToken cancellationToken)    {        var domainEvent = notification.DomainEvent;        // 这里我们还是只做日志输出,实际使用中根据需要进行业务逻辑处理,但是在Handler中不建议继续Send其他Command或Notification        _logger.LogInformation("TodoList Domain Event: {DomainEvent}", domainEvent.GetType().Name);        return Task.CompletedTask;    }}

最后去修改我们之前创建的DomainEventService,注入IMediator并发布领域事件,这样就可以在Handler中进行响应了。

DomainEventService.cs

using MediatR;using Microsoft.Extensions.Logging;using TodoList.Application.Common.Interfaces;using TodoList.Application.Common.Models;using TodoList.Domain.Base;namespace TodoList.Infrastructure.Services;public class DomainEventService : IDomainEventService{    private readonly IMediator _mediator;    private readonly ILogger _logger;    public DomainEventService(IMediator mediator, ILogger logger)    {        _mediator = mediator;        _logger = logger;    }    public async Task Publish(DomainEvent domainEvent)    {        _logger.LogInformation("Publishing domain event. Event - {event}", domainEvent.GetType().Name);        await _mediator.Publish(GetNotificationCorrespondingToDomainEvent(domainEvent));    }    private INotification GetNotificationCorrespondingToDomainEvent(DomainEvent domainEvent)    {        return (INotification)Activator.CreateInstance(typeof(DomainEventNotification<>).MakeGenericType(domainEvent.GetType()), domainEvent)!;    }}

验证

启动Api项目,更新TodoItem的完成状态。

请求

响应

领域事件发布

关于".NET 6开发TodoList应用之如何实现PUT请求"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

0