Skip to content

Domain-Driven Design in practice: bounded contexts and ubiquitous language

B2Bnetwork team · · 6 min

Technology and trends

In brief

Domain-Driven Design (DDD) is a way of designing software so that the structure of the code mirrors the structure of the business. The team and domain experts agree on a shared language, and the system is split into bounded contexts: areas in which every term has exactly one meaning. In practice the strategic part delivers the most value, meaning defining contexts and the relationships between them. Tactical patterns such as aggregates and value objects organize code inside a context. DDD pays off for complex business logic, not for simple CRUD forms.

What DDD is and what it consists of

The term was introduced by Eric Evans in his 2003 book “Domain-Driven Design”. A condensed version, the “DDD Reference”, is available on the author's website and collects the definitions of all the patterns. DDD has two layers:

  • strategic design: ubiquitous language, bounded contexts, context maps, and the split into a core domain and supporting domains;
  • tactical design: entities, value objects, aggregates, domain events, repositories and domain services.

Teams that start with the tactical layer often end up with code full of “Aggregate” and “Repository” classes but no clear boundaries. The reverse order works better: boundaries and language first, code patterns second.

Ubiquitous language: one word, one meaning

A ubiquitous language is a vocabulary everyone uses: the analyst, the developer, the tester and the business expert. The same words appear in conversations, requirements, class and table names, and acceptance tests.

An example from an insurer's system. The sales department says “policy” and means a priced quote the customer can still reject. The claims department says “policy” and means a signed contract with a coverage scope and a sum insured. If the code has a single `Policy` class for both meanings, it grows fields from both departments, and every change in sales risks a defect in claims handling.

Practical rules:

  • write down the glossary for each context and keep it in the repository next to the code;
  • when the expert uses a different word than the code, change the code, not the expert;
  • write acceptance test scenarios in the same language, as we describe in the article on BDD and acceptance testing.

Bounded contexts and context maps

A bounded context is a boundary inside which the model and the language are consistent. In the insurer example, “Sales” and “Claims” are two contexts, each with its own policy model. Only what is needed flows between them: the policy number, the coverage scope, the dates.

A context map shows how contexts depend on each other. The DDD Reference names several typical relationships:

RelationshipWhat it meansWhen it works
Partnershiptwo teams plan changes togethercontexts depend on each other in both directions
Shared kernela small, jointly maintained part of the modelwhen the teams work closely
Customer and supplierthe downstream team raises needs, the upstream team plans themwhen the supplier has capacity for the customer's needs
Conformistthe team adopts the supplier's model unchangedwhen the supplier will not change the model and the model is good
Anti-corruption layertranslating a foreign model into your own at the boundarywhen integrating with a legacy system or an external vendor
Open host service and published languagethe context exposes a stable API and data format for many consumerswhen there are many consumers

The anti-corruption layer is especially useful during modernization. The new context talks to the old policy administration system through a translator, so old field names and status codes do not leak into the new model. Microsoft describes this pattern in the Azure Architecture Center as a separate layer between subsystems with different semantics.

How to define contexts on a project

Contexts are not drawn at the architect's desk but in a workshop with people who know the process. A proven sequence:

  1. Business events on a timeline. Participants write down what happens in the process, in the past tense: “application submitted”, “risk assessed”, “policy issued”, “claim reported”.
  2. Who triggers each event and what decision is behind it. This surfaces commands, roles and rules.
  3. Where the meaning of words changes. The point at which “policy” starts to mean something else is a candidate context boundary.
  4. Who owns it. A context should have one team and one business owner.
  5. Which part is the core domain. Invest most where the company competes, for example pricing. Supporting domains, such as address dictionaries, can be bought or built more simply.

Contexts defined this way become modules of a modular monolith, or candidates for separate services if one of the reasons from the article Microservices: when they pay off applies.

Tactical patterns inside a context

Inside a context, DDD organizes code with a few patterns:

  • Entity: an object with an identity that persists through changes, for example a numbered policy whose sum insured changes.
  • Value object: an object without identity, compared by its values, for example an amount with a currency or a coverage period. It is immutable, which makes it easy to test.
  • Aggregate: a group of objects changed together in one transaction, with a single root object through which all changes pass. The aggregate enforces rules, for example that total payouts never exceed the sum insured.
  • Domain event: a record of something that happened in the business, for example “claim accepted”. Other contexts react to it without a direct call.

Domain events do not require event sourcing. When it is worth going further and storing state solely as a sequence of events is covered in the article on event sourcing and CQRS.

When DDD does not pay off

DDD takes business experts' time and team discipline. It does not pay off when the application is mostly forms saving data to tables without rules, when the project lasts a few weeks, or when nobody on the business side has time for workshops. In those cases a simple layered architecture is enough.

The biggest winners are systems with many rules and a long life: insurer and bank core systems, telecom billing, production planning. B2Bnetwork engineers work on such systems as architects, developers and testers; see What we do.

Questions and answers

Does DDD mean microservices?

No. Bounded contexts are boundaries in the model, not deployment units. Several contexts can run in a single application as modules. Microservices benefit from DDD, because a well-defined context is a natural candidate for a service, but the decision to deploy separately is made for other reasons: independent teams, scaling or security.

How big should a bounded context be?

Big enough for one team to develop it without constantly coordinating changes with others, and small enough for the model to stay consistent. A sign that a context is too big is terms with two meanings and classes with fields only part of the code uses. A sign that it is too small is changes that always touch several contexts at once.

Can DDD be introduced into an existing system?

Yes, gradually. Start with a context map of the current system, even if the boundaries are blurred today. Build a new feature as a separate context with its own model, and connect it to the old code through an anti-corruption layer. Tidy up further areas as part of planned changes, not in a separate rewrite program.

Who should take part in a context mapping workshop?

Business experts who know the process from the inside, analysts, the architect, developers and testers. The business brings the meaning of words and the rules, the technical team brings knowledge of the current system and its constraints. Without domain experts, the workshop turns into drawing a database diagram.

What it looks like in a project

Considering a new technology for your system? Talk to an engineer.

Let's talk

Read more: Technology and trends