使用.NET 6开发TodoList应用(9)——实现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
,来实现领域事件的处理。
目标#
- 实现PUT请求;
- 实现领域事件的响应处理;
原理与思路#
实现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
的完成状态。
总结#
这篇文章主要在实现PUT
请求的过程中介绍了如何通过MediatR
去响应领域事件,我们用的示例代码中类似“创建TodoList”,包括后面会讲到的“删除TodoItem”之类的领域事件,都是相同的处理方式,我就不一一演示了。
可以看出来,在我们这个示例应用程序的框架基本搭建完毕以后,进行领域业务的开发的思路是比较清晰的,模块之间的耦合也处在一个理想的情况。
在我们来完成CRUD
的最后一个请求之前,下一篇会简单地介绍一下PATCH
请求的相关内容,这个请求实际应用比较少,但是为了保持知识树的完整性,还是会过一下。
作者:CODE4NOTHING
出处:https://www.cnblogs.com/code4nothing/p/build-todolist-9.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
欢迎转载,转载请注明出处
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端