This guide will help you generate your first microservice in under 5 minutes.
- .NET 8 SDK installed
- Basic understanding of microservices and Clean Architecture
git clone https://github.com/your-repo/microservice-net8-ddd.git
cd microservice-net8-ddd
dotnet builddotnet run --project src/CLI/MicroserviceGenerator.CLI -- new MyFirstService --interactiveThis will guide you through:
- Architecture level selection
- Domain modeling
- Feature selection
- Technology choices
dotnet run --project src/CLI/MicroserviceGenerator.CLI -- new MyFirstServiceThis generates a standard-level service with:
- Clean Architecture (Domain + Application + API)
- REST Controllers
- Unit Tests
- Basic CRUD operations
dotnet run --project src/CLI/MicroserviceGenerator.CLI -- new OrderService --config examples/enterprise-service.jsonYour service will be generated in ./MyFirstService/ with this structure:
MyFirstService/
├── src/
│ ├── MyFirstService.Domain/ # Your business logic
│ ├── MyFirstService.Application/ # Use cases & handlers
│ └── MyFirstService.Api/ # REST endpoints
├── tests/
│ └── MyFirstService.UnitTests/ # Comprehensive tests
└── MyFirstService.sln # Solution file
cd MyFirstService
dotnet build
dotnet run --project src/MyFirstService.ApiYour API will be available at:
- HTTP: http://localhost:5000
- HTTPS: https://localhost:5001
- Swagger: https://localhost:5001/swagger
# Run all tests
dotnet test
# Test a specific endpoint
curl https://localhost:5001/api/todoitems- Aggregates: Rich domain models with business logic
- Value Objects: Immutable data structures
- Domain Events: For decoupled communication
- Specifications: Business rule validation
- Commands: Write operations (Create, Update, Delete)
- Queries: Read operations with filtering and paging
- Handlers: Business logic execution
- DTOs: Data transfer objects
- Validators: Input validation with FluentValidation
- Controllers: REST endpoints with full CRUD
- Middleware: Error handling, logging, CORS
- Filters: Validation and exception handling
- Documentation: OpenAPI/Swagger integration
- Unit Tests: Fast, isolated tests for all layers
- Test Utilities: Builders, mocks, and test data
- Architecture Tests: Dependency rule validation
Edit the generated aggregates in src/MyFirstService.Domain/Entities/
Implement your business rules in domain entities and value objects
Add new controllers or modify existing ones in src/MyFirstService.Api/Controllers/
Use the generated test utilities to create integration tests
# Generate Docker support
dotnet run --project src/CLI/MicroserviceGenerator.CLI -- new MyFirstService --config examples/docker-enabled.json
# Build and run
docker-compose up -d- Define the aggregate in
Domain/Entities/ - Add commands/queries in
Application/ - Create controller in
Api/Controllers/ - Write tests in
Tests/
// In your aggregate
public void CompleteOrder()
{
Status = OrderStatus.Completed;
AddDomainEvent(new OrderCompletedEvent(Id, CustomerId));
}
// Handler will be auto-generated
public class OrderCompletedEventHandler : INotificationHandler<OrderCompletedEvent>
{
public async Task Handle(OrderCompletedEvent notification, CancellationToken cancellationToken)
{
// Your event handling logic
}
}// Command (Write)
public record CreateOrderCommand(Guid CustomerId, List<OrderItem> Items);
// Query (Read)
public record GetOrderByIdQuery(Guid OrderId);
// Handlers are auto-generated with proper validation and error handling| Level | When to Use | Generated Structure |
|---|---|---|
| MINIMAL | Simple CRUD, prototypes, learning | Single project with folders |
| STANDARD | Most business applications | Domain + Application + API |
| ENTERPRISE | Complex domains, large teams | + Infrastructure layer, advanced patterns |
| AUTO | Let the generator decide | Analyzes your config and chooses |
{
"ServiceName": "ProductCatalog",
"Architecture": { "Level": "minimal" },
"Domain": {
"Aggregates": [
{
"Name": "Product",
"Properties": [
{ "Name": "Name", "Type": "string" },
{ "Name": "Price", "Type": "decimal" }
],
"Operations": ["Create", "Update", "Delete"]
}
]
}
}{
"ServiceName": "OrderService",
"Architecture": { "Level": "standard" },
"Features": {
"Messaging": { "Enabled": true, "Provider": "rabbitmq" }
},
"Domain": {
"Aggregates": [
{
"Name": "Order",
"Properties": [
{ "Name": "CustomerId", "Type": "Guid" },
{ "Name": "Status", "Type": "OrderStatus" }
],
"Operations": ["Create", "Confirm", "Cancel", "Complete"]
}
]
}
}# Clean and rebuild
dotnet clean
dotnet build# Restore packages
dotnet restoreChange ports in appsettings.json or use:
dotnet run --project src/MyFirstService.Api --urls "https://localhost:6001;http://localhost:6000"Ready to build amazing microservices? Let's go! 🚀