当前位置: 首页 > news >正文

(一)微服务(垂直API)


文章目录

  • 项目地址
  • 一、创建第一个垂直API
    • 1.1 创建Common层
      • 1. ICommand接口
      • 2. IQuery接口
    • 1.2 创建API
      • 1. 实体
      • 2. Handler
      • 3. endpoint
    • 1.3 使用Marten作为ORM


项目地址

  • 教程作者:
  • 教程地址:
  • 代码仓库地址:
  • 所用到的框架和插件:
dbt 
airflow

一、创建第一个垂直API

1.1 创建Common层

在这里插入图片描述

1. ICommand接口

  1. ICommand.cs
using MediatR;
namespace BuildingBlocks.CQRS;
public interface ICommand : ICommand<Unit>; 
public interface ICommand<out TResponse> : IRequest<TResponse>;
  1. ICommandHandler.cs
using MediatR;
namespace BuildingBlocks.CQRS;
public interface ICommandHandler<in TCommand>  //无返回值: ICommandHandler<TCommand, Unit>where TCommand : ICommand<Unit>;public interface ICommandHandler<in TCommand, TResponse> //有返回值: IRequestHandler<TCommand, TResponse>where TCommand : ICommand<TResponse>where TResponse : notnull;

in(Contravariant):只在参数中使用的泛型类型

2. IQuery接口

  1. IQuery.cs
using MediatR;
namespace BuildingBlocks.CQRS;
public interface IQuery<out TResponse> : IRequest<TResponse>  where TResponse : notnull;
  1. IQueryHandler:
namespace BuildingBlocks.CQRS;
public interface IQueryHandler<in TQuery, TResponse>: IRequestHandler<TQuery, TResponse>where TQuery : IQuery<TResponse>where TResponse : notnull;

1.2 创建API

在这里插入图片描述

1. 实体

namespace Catalog.API.Models;
public class Product
{public Guid Id { get; set; }public string Name { get; set; } = default!;public List<string> Category { get; set; } = new();public string Description { get; set; } = default!;public string ImageFile { get; set; } = default!;public decimal Price { get; set; }
}

2. Handler

namespace Catalog.API.Products.CreateProduct;
public record CreateProductCommand(string Name, List<string> Category, string Description, string ImageFile, decimal Price): ICommand<CreateProductResult>;
public record CreateProductResult(Guid Id);internal class CreateProductCommandHandler : ICommandHandler<CreateProductCommand, CreateProductResult>
{public async Task<CreateProductResult> Handle(CreateProductCommand command, CancellationToken cancellationToken){var product = new Product{Name = command.Name,Category = command.Category,Description = command.Description,ImageFile = command.ImageFile,Price = command.Price};return new CreateProductResult(Guid.NewGuid());        }
}

3. endpoint

namespace Catalog.API.Products.CreateProduct;
public record CreateProductRequest(string Name, List<string> Category, string Description, string ImageFile, decimal Price);
public record CreateProductResponse(Guid Id);
public class CreateProductEndpoint : ICarterModule
{public void AddRoutes(IEndpointRouteBuilder app){app.MapPost("/products",async (CreateProductRequest request, ISender sender) =>{var command = request.Adapt<CreateProductCommand>();var result = await sender.Send(command);var response = result.Adapt<CreateProductResponse>();return Results.Created($"/products/{response.Id}", response);}).WithName("CreateProduct").Produces<CreateProductResponse>(StatusCodes.Status201Created).ProducesProblem(StatusCodes.Status400BadRequest).WithSummary("Create Product").WithDescription("Create Product");}
}

1.3 使用Marten作为ORM

  • Marten只能用于postgresql的ORM使用

相关文章:

  • 大疆上云API+流媒体服务器部署实现直播功能
  • 中国移动咪咕助力第五届全国人工智能大赛“AI+数智创新”专项赛道开展
  • JAVA单商户易联云小票打印替换模板
  • java对接bacnet ip协议(跨网段方式)
  • ⭐ Unity AVProVideo插件自带播放器 脚本重构 实现视频激活重置功能
  • azure web app创建分步指南
  • WPF响应式UI的基础:INotifyPropertyChanged
  • WPF的交互核心:命令系统(ICommand)
  • 工控机安装lubuntu系统
  • 跨平台浏览器集成库JxBrowser 支持 Chrome 扩展程序,高效赋能 Java 桌面应用
  • 玛哈特校平机实战指南:选型、应用痛点
  • Java String的使用续 -- StringBuilder类和StringBuffer
  • OpenCV---minAreaRect
  • Java八股-数据类型转换有哪些?类型互转会有什么问题?为什么用bigDecimal 不用double ?自动装箱和拆箱?包装类?
  • QML 滑动与翻转效果(Flickable与Flipable)
  • 随记 配置服务器的ssl整个过程
  • 华为云Flexus+DeepSeek征文 | 基于Dify和DeepSeek-R1开发企业级AI Agent全流程指南
  • Python训练营打卡Day40
  • 《系统集成项目管理工程师(第三版)》高效学习方法
  • Spring,SpringMVC,SpringBoot
  • 网站主编 做啥/网站怎么快速被百度收录
  • 下载的asp网站怎么打开/域名怎么注册
  • 邯郸网站建设包括哪些/直销的八大课程
  • 做外贸网站注意什么/上海网站seo策划
  • 外贸soho网站制作/优化网站的软件下载
  • 做招聘网站需要多少钱/企业营销策划书范文