Overview
Domain services handle:- Multi-entity operations
- Complex business rules
- Domain calculations
- Third-party integrations affecting the domain
- Operations requiring multiple aggregates
Installation
Key Concepts
When to Use Domain Services
Use domain services when:- Operation involves multiple entities
- Logic doesn’t belong to any single entity
- Stateless operations are needed
- External services affect domain logic
- Simple CRUD operations (use Application Services)
- Data access (use Repositories)
- Infrastructure concerns (use Infrastructure Services)
Generation
Domain services are generated from the Domain Designer:Domain Service
Common Patterns
Transfer Operations
Transfer Service
Validation Services
Validation Service
Calculation Services
Calculation Service
Policy Services
Policy Service
Usage in Entities
Entities can use domain services:Entity Using Service
Dependency Injection
Domain services are automatically registered:DI Registration
Testing
Testing
Best Practices
Keep Stateless
Domain services should not maintain state between calls. Use entities for stateful operations.
Pure Logic
Focus on business logic, not infrastructure concerns like database access or external APIs.
Minimal Dependencies
Depend only on other domain objects and minimal infrastructure (like UnitOfWork).
Testability
Design for testability with clear inputs and outputs. Avoid hidden dependencies.
Domain Service vs Application Service
| Domain Service | Application Service |
|---|---|
| Contains business logic | Orchestrates use cases |
| Works with domain objects | Works with DTOs |
| No infrastructure concerns | Can access infrastructure |
| Reusable across use cases | Specific to one use case |
| Lives in Domain layer | Lives in Application layer |
Related Modules
- Entities - Domain entities
- Domain Events - Domain event handling
- Application Services - Application layer services
- EF Core Repositories - Data access
