最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

.NET 6開發(fā)TodoList應(yīng)用之實(shí)現(xiàn)PUT請求

 更新時(shí)間:2021年12月28日 08:34:48   作者:CODE4NOTHING  
PUT請求本身其實(shí)可說的并不多,過程也和創(chuàng)建基本類似。這篇文章主要為大家介紹了.NET6實(shí)現(xiàn)PUT請求的示例詳解,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

需求

PUT請求本身其實(shí)可說的并不多,過程也和創(chuàng)建基本類似。在這篇文章中,重點(diǎn)是填上之前文章里留的一個(gè)坑,我們曾經(jīng)給TodoItem定義過一個(gè)標(biāo)記完成的領(lǐng)域事件:TodoItemCompletedEvent,在SaveChangesAsync方法里做了一個(gè)DispatchEvents的操作。并且在DomainEventService實(shí)現(xiàn)IDomainEventService的Publish方法中暫時(shí)以下面的代碼代替了:

DomainEventService.cs

public async Task Publish(DomainEvent domainEvent)
{
    // 在這里暫時(shí)什么都不做,到CQRS那一篇的時(shí)候再回來補(bǔ)充這里的邏輯
    _logger.LogInformation("Publishing domain event. Event - {event}", domainEvent.GetType().Name);
}

在前幾篇應(yīng)用MediatR實(shí)現(xiàn)CQRS的過程中,我們主要是和IRequest/IRequestHandler打的交道。那么本文將會涉及到另外一對常用的接口:INotification/INotificationHandler,來實(shí)現(xiàn)領(lǐng)域事件的處理。

目標(biāo)

1.實(shí)現(xiàn)PUT請求;

2.實(shí)現(xiàn)領(lǐng)域事件的響應(yīng)處理;

原理與思路

實(shí)現(xiàn)PUT請求的原理和思路與實(shí)現(xiàn)POST請求類似,就不展開了。關(guān)于實(shí)現(xiàn)領(lǐng)域事件響應(yīng)的部分,我們需要實(shí)現(xiàn)INotification/INotificationHandler接口,并改寫Publish的實(shí)現(xiàn),讓它能發(fā)布領(lǐng)域事件通知。

實(shí)現(xiàn)

PUT請求

我們拿更新TodoItem的完成狀態(tài)來舉例,首先來自定義一個(gè)領(lǐng)域異常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.") { }
}

創(chuàng)建對應(yīng)的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;
    }
}

實(shí)現(xiàn)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));
}

領(lǐng)域事件的發(fā)布和響應(yīng)

首先需要在Application/Common/Models定義一個(gè)泛型類,實(shí)現(xiàn)INotification接口,用于發(fā)布領(lǐng)域事件:

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中創(chuàng)建對應(yīng)的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;

        // 這里我們還是只做日志輸出,實(shí)際使用中根據(jù)需要進(jìn)行業(yè)務(wù)邏輯處理,但是在Handler中不建議繼續(xù)Send其他Command或Notification
        _logger.LogInformation("TodoList Domain Event: {DomainEvent}", domainEvent.GetType().Name);

        return Task.CompletedTask;
    }
}

最后去修改我們之前創(chuàng)建的DomainEventService,注入IMediator并發(fā)布領(lǐng)域事件,這樣就可以在Handler中進(jìn)行響應(yīng)了。

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)!;
    }
}

驗(yàn)證

啟動Api項(xiàng)目,更新TodoItem的完成狀態(tài)。

請求

響應(yīng)

領(lǐng)域事件發(fā)布

總結(jié)

這篇文章主要在實(shí)現(xiàn)PUT請求的過程中介紹了如何通過MediatR去響應(yīng)領(lǐng)域事件,我們用的示例代碼中類似“創(chuàng)建TodoList”,包括后面會講到的“刪除TodoItem”之類的領(lǐng)域事件,都是相同的處理方式,我就不一一演示了。

可以看出來,在我們這個(gè)示例應(yīng)用程序的框架基本搭建完畢以后,進(jìn)行領(lǐng)域業(yè)務(wù)的開發(fā)的思路是比較清晰的,模塊之間的耦合也處在一個(gè)理想的情況。

在我們來完成CRUD的最后一個(gè)請求之前,下一篇會簡單地介紹一下PATCH請求的相關(guān)內(nèi)容,這個(gè)請求實(shí)際應(yīng)用比較少,但是為了保持知識樹的完整性,還是會過一下。?

到此這篇關(guān)于.NET 6開發(fā)TodoList應(yīng)用之實(shí)現(xiàn)PUT請求的文章就介紹到這了,更多相關(guān).NET 6 實(shí)現(xiàn)PUT請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • asp.net System.Net.Mail 發(fā)送郵件

    asp.net System.Net.Mail 發(fā)送郵件

    一個(gè)師弟發(fā)了段代碼給我,說調(diào)試了很久發(fā)送郵件都沒有成功。自己使用過程中,也發(fā)現(xiàn)了很多問題,但最簡單的問題是“發(fā)件方”地址根本不支持smtp發(fā)送郵件。
    2009-04-04
  • ASP.NET MVC3模板頁的使用(2)

    ASP.NET MVC3模板頁的使用(2)

    這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC3模板頁的使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2015-08-08
  • 在ASP.NET Core中應(yīng)用HttpClient獲取數(shù)據(jù)和內(nèi)容

    在ASP.NET Core中應(yīng)用HttpClient獲取數(shù)據(jù)和內(nèi)容

    這篇文章主要介紹了在ASP.NET Core中集成和使用HttpClient獲取數(shù)據(jù)和內(nèi)容,幫助大家更好的理解和學(xué)習(xí)使用ASP.NET Core,感興趣的朋友可以了解下
    2021-03-03
  • asp.net 文件下載的通用方法

    asp.net 文件下載的通用方法

    一則雕蟲小技,記下備忘,以使同學(xué)們少走彎路。
    2009-06-06
  • .NETCore添加區(qū)域Area代碼實(shí)例解析

    .NETCore添加區(qū)域Area代碼實(shí)例解析

    這篇文章主要介紹了.NETCore添加區(qū)域Area代碼實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • 使用 .NET MAUI 開發(fā) ChatGPT 客戶端的流程

    使用 .NET MAUI 開發(fā) ChatGPT 客戶端的流程

    最近?chatgpt?很火,由于網(wǎng)頁版本限制了 ip,還得必須開代理,用起來比較麻煩,所以我嘗試用 maui 開發(fā)一個(gè)聊天小應(yīng)用,結(jié)合 chatgpt 的開放 api 來實(shí)現(xiàn),這篇文章主要介紹了使用 .NET MAUI 開發(fā) ChatGPT 客戶端,需要的朋友可以參考下
    2022-12-12
  • 詳解ASP.NET Core端點(diǎn)路由的作用原理

    詳解ASP.NET Core端點(diǎn)路由的作用原理

    這篇文章主要介紹了詳解ASP.NET Core端點(diǎn)路由的作用原理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Asp.Net MVC中配置Serilog的方法

    Asp.Net MVC中配置Serilog的方法

    Serilog是一款比較優(yōu)秀的logging framework,Serilog只支持.NET 4.5以上的版本。下面這篇文章將會通過圖文及示例代碼的形式給大家介紹Asp.Net MVC中配置Serilog的方法,有需要的朋友們可以參考借鑒,下面來跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。
    2016-12-12
  • 獲取App.config配置文件中的參數(shù)值

    獲取App.config配置文件中的參數(shù)值

    這篇文章介紹了獲取app.config配置文件中的參數(shù)值方法,首先是要添加System.Configuration引用,其次類文件中必須有 using System.Configuration;再次App.config添加,最后向App.config配置文件添加參數(shù),下面通過列子給大家講解下,需要的朋友可以參考下
    2015-07-07
  • MVC4制作網(wǎng)站教程第二章 用戶登陸2.2

    MVC4制作網(wǎng)站教程第二章 用戶登陸2.2

    這篇文章主要為大家詳細(xì)介紹了MVC4制作網(wǎng)站教程,用戶登陸功能的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-08-08

最新評論

呈贡县| 宿迁市| 涿鹿县| 云林县| 吴江市| 简阳市| 乐东| 界首市| 邢台市| 屯门区| 黑龙江省| 湛江市| 资溪县| 岳阳市| 共和县| 临澧县| 库伦旗| 怀化市| 旌德县| 土默特左旗| 博乐市| 合川市| 吕梁市| 嵊州市| 商城县| 平昌县| 岑溪市| 溧阳市| 股票| 庄河市| 南通市| 乌兰浩特市| 绥宁县| 宜春市| 巨鹿县| 苍南县| 青州市| 朝阳市| 呼伦贝尔市| 克山县| 修文县|