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

发布时间:2021-12-28 10:39:34 作者:小新
来源:亿速云 阅读:139

这篇文章将为大家详细讲解有关.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<TodoItem>
{
    public Guid Id { get; set; }
    public string? Title { get; set; }
    public bool Done { get; set; }
}

public class UpdateTodoItemCommandHandler : IRequestHandler<UpdateTodoItemCommand, TodoItem>
{
    private readonly IRepository<TodoItem> _repository;

    public UpdateTodoItemCommandHandler(IRepository<TodoItem> repository)
    {
        _repository = repository;
    }

    public async Task<TodoItem> 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<ApiResponse<TodoItem>> Update(Guid id, [FromBody] UpdateTodoItemCommand command)
{
    if (id != command.Id)
    {
        return ApiResponse<TodoItem>.Fail("Query id not match witch body");
    }

    return ApiResponse<TodoItem>.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<TDomainEvent> : 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<DomainEventNotification<TodoItemCompletedEvent>>
{
    private readonly ILogger<TodoItemCompletedEventHandler> _logger;

    public TodoItemCompletedEventHandler(ILogger<TodoItemCompletedEventHandler> logger)
    {
        _logger = logger;
    }

    public Task Handle(DomainEventNotification<TodoItemCompletedEvent> 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<DomainEventService> _logger;

    public DomainEventService(IMediator mediator, ILogger<DomainEventService> 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请求

响应

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

领域事件发布

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

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

推荐阅读:
  1. springMVC将post请求转为delete/put请求
  2. Django无法处理HTTP PUT/DELETE请求

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

put todolist

上一篇:SpringBoot中Log日志集成的示例分析

下一篇:EMC VMAX的架构难点有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》