Development
API
Each endpoint is mapped to a handler. Let's create a handler first:
ShipDotnet.Handlers/Handlers/Products/Queries/GetProductById.cs :
namespace ShipDotnet.Handlers;
public static class GetProductById
{
public class Query : IQuery<IHandlerResponse<Response>> // your input
{
public int Id { get; set; }
}
public record Response(ProductModel Product); // your output
public record ProductModel(int Id, string Name, string? Description);
public class Handler : QueryHandler<Query, Response>// your executing code
{
public Handler(IHandlerContext context) : base(context) { }
public override async Task<IHandlerResponse<Response>> ExecuteAsync(Query query, CancellationToken ct)
{
// retire data from data store
var product = await Task.FromResult(new ProductModel(query.Id, "Doom Slayer: Dark Ages", "First-person shooter game developed by id Software and published by Bethesda Softworks, serving as a prequel to the modern Doom games."));
if (product is null)
{
return Error("Product not found");
}
return Success(new Response(product));
}
}
}
Now let's map a handler to an endpoint:
ShipDotnet.Api/Program.cs :
app.MapGetHandler<GetProductById.Query, GetProductById.Response>("/products.getById.{id}");
👏👏👏 Your endpoint is ready! 👏👏👏
You can now run the API and test the endpoint from Swagger: http://localhost:5000/swagger/index.html
You would develop all your endpoints as Queries (GET) and Commands (POST, PUT, DELETE).
Check more examples under MinmalCQRS or contact me via X (Twitter) or Discord
Next:
Database Migrations