Standalone conceptPatternChapter I · Domain · Building block 3 of 18

Domain service

A domain service makes visible a domain-language operation that combines several business facts.

  1. FactsFacts used by the rule.
  2. OperationNamed decision.
  3. OutcomeBusiness-readable output.

Context

Why this is important

Model and independently test business rules with no natural object owner.

A business calculation with explicit inputs; infrastructure and process coordination remain separate responsibilities.

Shop example · Mapping

Object behavior or domain service

The shipping-eligibility rule checks values from ordering, catalog and shipping options in the seven-module shop.

This is how you read the picture

  1. Facts Inputs
  2. Operation Policy
  3. Outcome Response
Object behavior or domain service
CriterionPolicySuitable location
Direct invariantEntity or value objectEntity or value object
Several factsDomain serviceDomain service
The matrix assigns rules to their natural owner.

Benefits & limitations

Helps if:

  • A decision combines facts from several objects and belongs to none of them alone.
  • A business operation needs a clear contract that can be tested independently.

Boundaries

  • One service per entity creates procedural layers and scatters related rules.
  • Infrastructure calls and process coordination belong in the appropriate application or integration components.

In practice

  1. State the rule in domain language as a decision with clear inputs and an outcome. Also note which missing or conflicting values require a business response.
  2. First check whether an Entity or Value Object has all relevant facts and can express the rule as its own behaviour. Consider a Domain Service only when no object is the natural owner.
  3. Pass the remaining facts through a narrow contract and return a business-named outcome. Test edge cases without a database or network, and keep side effects in an appropriate application layer.
Read the detailed explanation

The service expresses an operation such as “determine shipping eligibility.” It receives the facts needed by the rule and returns a business result. It has no long-lived state of its own and hides no database or network access.

If a rule clearly belongs to an entity or value object, keep it there. Rules spanning consistency boundaries need deliberate process coordination. A domain service is neither a generic application step nor automatically a distributed transaction manager.

First check whether the rule belongs to an Entity or a Value Object; the Aggregate defines its consistency boundary.

Check your understanding

Test yourself in three stages

  1. ReadingCheck in the picture

    Which facts does the shop rule check?

    View the solution with justification

    Weight, destination and shipping method.

  2. ApplyingTransferred to a new situation

    A rule naturally belongs to the cart. Where should it live?

    View the solution with justification

    As behavior of the relevant cart object.

  3. EvaluateWeighing an alternative

    When is a separate domain service justified?

    View the solution with justification

    When no object naturally owns the operation and its inputs and result are clear in domain terms.

Explore further

These building blocks continue the thoughts of this page.

Continue on the learning path

Next in Domain Modelling & DDD: Event Storming.

18 Building blocks

Continue withEvent Storming