Adding New Entities
Complete workflow for adding domain entities with all layers
GuidesIntermediate• 15 min read
Implementation Steps
Follow these 9 steps to properly implement a new entity following Clean Architecture principles
1
Domain Entity2
Domain Events3
Repository Interface4
EF Configuration5
Repository Implementation6
DTOs & Mapping7
Application Service8
API Controller9
Database MigrationExample: Product Entity
We'll walk through adding a Product entity with pricing, inventory, and category relationships
Business Requirements
- • Products have names, descriptions, and SKUs
- • Track inventory levels and pricing
- • Support multiple categories per product
- • Audit creation and modification times
- • Soft delete with deactivation dates
What We'll Build
- • Rich domain entity with business logic
- • Repository pattern for data access
- • Application services for use cases
- • RESTful API endpoints
- • Complete test coverage
Architecture Benefits
- • Business logic in the domain
- • Testable without dependencies
- • Consistent data access patterns
- • Clean API design
- • Easy to extend and modify
Files You'll Create
Complete list of files needed for the Product entity
ModernAPI.Domain/
├── Entities/
│ └── Product.cs # Step 1: Domain entity
├── Events/
│ ├── ProductCreatedEvent.cs # Step 2: Domain events
│ ├── ProductPriceChangedEvent.cs
│ └── ProductDiscontinuedEvent.cs
└── Interfaces/
└── IProductRepository.cs # Step 3: Repository contract
ModernAPI.Infrastructure/
├── Data/Configurations/
│ └── ProductConfiguration.cs # Step 4: EF configuration
├── Repositories/
│ └── ProductRepository.cs # Step 5: Repository implementation
└── Migrations/
└── 20240101_AddProductEntity.cs # Step 9: Database migration
ModernAPI.Application/
├── DTOs/
│ ├── ProductDtos.cs # Step 6: Data transfer objects
│ └── ProductMappings.cs # Step 6: AutoMapper profiles
├── Services/
│ └── ProductService.cs # Step 7: Application service
└── Interfaces/
└── IProductService.cs # Step 7: Service contract
ModernAPI.API/
└── Controllers/
└── ProductsController.cs # Step 8: API controller
Next Steps
Complete the implementation with testing and migration
Testing
- • Write domain entity tests
- • Test repository implementations
- • Unit test application services
- • Integration test API endpoints
Database Migration
- • Run: dotnet ef migrations add AddProduct
- • Review generated migration
- • Apply: dotnet ef database update
- • Verify database schema