dotnet-cqrs/docs/tutorials/modular-solution/05-api-layer.md

46 lines
1.1 KiB
Markdown

# API Layer
Configure HTTP and gRPC endpoints.
## Program.cs
```csharp
using OrderManagement.Infrastructure.Data;
using OrderManagement.Infrastructure.Repositories;
using Svrnty.CQRS;
using Svrnty.CQRS.MinimalApi;
var builder = WebApplication.CreateBuilder(args);
// Database
builder.Services.AddDbContext<OrderDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("Orders")));
// Repositories
builder.Services.AddScoped<IOrderRepository, OrderRepository>();
// CQRS
builder.Services.AddSvrntyCQRS();
builder.Services.AddDefaultCommandDiscovery();
builder.Services.AddDefaultQueryDiscovery();
// Register commands and queries
builder.Services.AddCommand<PlaceOrderCommand, int, PlaceOrderCommandHandler>();
builder.Services.AddQuery<GetOrderQuery, OrderDto, GetOrderQueryHandler>();
// Validators
builder.Services.AddTransient<IValidator<PlaceOrderCommand>, PlaceOrderCommandValidator>();
var app = builder.Build();
// Map endpoints
app.MapSvrntyCommands();
app.MapSvrntyQueries();
app.Run();
```
## Next Steps
Continue to [Testing Strategy](06-testing-strategy.md)