SOLID & Design Patterns Interview Question #73
What is a Domain Service?
Advanced Patterns, CQRS & Domain-Driven Design Fundamentals Mid-Level Intermediate
Quick Interview Answer
A Domain Service contains domain behavior that does not naturally belong to a single entity or value object. It expresses business logic using domain concepts and should remain focused on the domain rather than infrastructure concerns.
Detailed Explanation
A Domain Service contains domain behavior that does not naturally belong to a single entity or value object. It expresses business logic using domain concepts and should remain focused on the domain rather than infrastructure concerns.
For example, a pricing calculation involving several domain objects may belong in a domain service when placing it on any one entity would distort that entity's responsibility.
Code Example
public class PricingService
{
public Money Calculate(
Customer customer,
Order order,
PricingPolicy policy)
{
// Domain calculation.
return new Money(0m, "USD");
}
}