The Gatekeeper
Think of an Aggregate Root as a Store Manager holding the keys to the inventory room.
Customers (external objects) cannot walk into the back room and take an item (internal entity) directly. They must go through the Manager. This ensures the inventory count is always correct and no rules are broken (e.g., "You can't buy an item that is reserved").
-
check_circle
Global Identity: It's the only object you can look up directly by ID from a repository.
-
check_circle
Transactional: Changes to the aggregate are saved as one atomic transaction.
-
check_circle
Encapsulation: It forbids external access to its internal children.
The Facilitator
Think of a Domain Service as a Marriage Officiant.
A marriage involves two people (Entities), but the act of marrying them isn't a method on the Groom or the Bride. It's a process that involves both but belongs to neither. The Officiant (Service) performs the ceremony according to the laws (Business Rules).
-
check_circle
Stateless: It doesn't hold data; it performs an action.
-
check_circle
Cross-Cutting: Perfect for logic involving multiple Aggregate Roots.
-
check_circle
Ubiquitous Language: The service name comes from the business (e.g., "FundsTransfer"), not tech.
The Delivery System
Think of an Infrastructure Service as the Postal System.
You write the letter (Domain content), but you rely on the Postal Service (Infrastructure) to physically move it. The Postal Service doesn't care about the emotional content of your letter; it only cares about the address and the transport.
-
check_circle
Dependency Inversion: The Interface lives in the Domain (e.g., `IEmailSender`), but the Implementation lives in Infrastructure.
-
check_circle
External World: Talks to DBs, APIs, File Systems, Hardware.
-
check_circle
Replaceable: You can swap `SmtpEmailSender` for `MockEmailSender` without changing business rules.
Architect's Challenge
Where does this responsibility belong? Test your knowledge.
Quick Comparison
| Feature | Aggregate Root | Domain Service | Infrastructure Service |
|---|---|---|---|
| Main Goal | Protect integrity of state | Execute logic spanning entities | Communicate with external systems |
| State | Stateful | Stateless | Stateless |
| Dependencies | Other entities, Value Objects | Entities, Repositories (Interfaces) | External Drivers, SDKs, DBs |
| Layer | Domain Layer | Domain Layer | Infrastructure Layer |