109

I've read Eric Evan's book and am reading Vaughn Vernon's book now. I'm in the second chapter where he talks about subdomains and bounded context and am thoroughly confused now.

From what I was able to distill, there should be a 1:1 relationship between a BC and an SD. However, I read in other places that this isn't necessarily the case.

Can someone explain to me the relationship between a BC and SD?

Michael Currie
  • 13,721
  • 9
  • 42
  • 58
Chris
  • 1,690
  • 2
  • 17
  • 24

12 Answers12

72

A subdomain is a part of your business. There are core domains, supporting domains and generic domains. Core domains are where the money is, supporting domains support your core business, and generic domains are the ones you need, but don't care a lot about, so you would probably buy them of the shelf. For an insurance company, the core domain is insurance, a supporting domain could be client portfolio, and a generic domain could be something like timesheets.

In general a bounded context is a boundary within which the ubiquitous language is consistent. In DDD walhalla each subdomain would live in its own bounded context. In reality however, there is legacy, there are packages that try to do everything at once... which will force all kinds of awkard relationships.

JefClaes
  • 3,275
  • 21
  • 23
  • 6
    If a subdomain would live in its own bounded context, it means that the bounded context is broader than the subdomain. But from what I'm getting from the book by Vaughn Vernon, in a subdomain you can have multiple Bounded Contexts. So maybe it would be more correct to say that each BC would live in its SD. Am I wrong? – acejazz Jul 10 '19 at 08:27
  • 1
    A model's boundary, the bounded context, can contain ideas from various subdomains. Or a single subdomain might be represented by a number of bounded contexts. The ideal scenario would be one bounded context for one subdomain. If you are able to define multiple bounded context for a subdomain, that sometimes leans you into realizing that the subdomain is not fine-grained, and maybe the subdomain could be distilled into separated subdomains. The other way around could also be justified, when you had multiple subdomain aspect covered in one BD, because e.g. that was pragmatic to do so. – proximab Oct 14 '22 at 14:58
  • I have the same problem understanding this too. Thanks for the question & explanation. – Bonsaisteak Dec 14 '22 at 06:57
71

I try to explain these concepts with my understanding.

In DDD, everything should be communicated under ubiquitous language so the technical team and business team can use the same terms and have same views on the problems

  • Domain in DDD represent real problem in business. Such as: E commerce is a domain, Payroll system is a domain
  • Domain is divided into many sub domains, so each sub domains focus smaller problems. Such as: E commerce has many sub domains such as: Shopping Cart, Billing, Product Catalog, Customer Information...
  • Each sub domain should have explicit responsibilities so it has a boundary to limit their functionalities, the boundary will help sub domain focus to do only 1 thing and do well. This boundary is considered as bounded context of the sub domain. The bounded context will define:
    • How many domain models needed for the sub domain?
    • Which properties needed in the each model?
    • Which functionalities needed in sub domain?

Ex: Shopping Cart sub domain needs models: Cart, Product, Customer Info... and contains functions to perform CRUD on the cart. Notes: The Product and Customer model in the Shopping Cart sub domain maybe not the same with the models in Product Catalogs and Customer Profiles sub domain, they just contain necessary properties to display on Shopping Cart.

huaminhluan
  • 745
  • 5
  • 2
24

Vaughn Vernon in his “Implementing Domain-Driven Design” book states that “the subdomains live in the problem space and the bounded contexts in the solution space”

Imagine a software that is being developed to support a dentist. A dentist has two problems: fixing patients’ teeth and making appointments for the patients. Fixing teeth is the core domain and making appointments is a supporting subdomain. In the core domain the medical staff cares about a patient’s dental history, can they handle general anesthesia or not, what their current problem is, etc. In the subdomain the staff (not necessarily medical staff) cares about a patient’s contact information, a date and a time that best suits both the doctor and the patient, the type of dental work needed, etc. Both domains need a model of a patient, but that model will depend on the bounded context we put in place to ensure the correct information and features are available when solving the problems of each domain. read https://robertbasic.com/blog/bounded-contexts-and-subdomains/

mic4ael
  • 7,974
  • 3
  • 29
  • 42
Fasil Tadesse
  • 241
  • 2
  • 3
10

Here is my understanding, I would use the Hospital example to elaborate the concepts and deep dive into how is BC is different than Subdomain and why they can be a case where there is no 1:1 relationship between them

Example

Imagine we are making software for a Hospital, in which we have identified 3 subdomain

  • Health Care (Core domain, where they actually want to cure the patient)
  • Invoice (Supporting domain focused on invoicing)
  • Knowledge (Generic domain, where doctors maintain procedures on how to operate on a patient for a particular disease)

Now we know that Bounded Contexts are boundaries under which terms have a very well-defined meaning. So let us apply those in Subdomains

Let's consider the term. Patient. What are the things that you think about when hearing the term patient?

  1. Their current symptoms
  2. Past medical records
  3. Allergies

How about their bill-paying credibility? Current outstanding balance? Didn't think of it? The reason is you were thinking in the core subdomain space of Health Care. The bill-paying credibility makes sense only when you shift to the Invoice subdomain.

What we understand from this is the Patient term is inside a Bounded Context, its a boundary inside a subdomain where it has a very specific meaning

The reason it said

BC is in solution/implementation/programming space and not in business space

is because here we decide what fields and behaviors should be part of the Patient model

In the core domain space, you might represent Patient it like this

    class Patient {
     
     List<Allergy> alergies;
     List<MedicalRecord> records;
     Age age;
    
     boolean isAllergicTo(Allergy allergy)
     boolean canTakeLocalAnesthesia()
    
    }

Whereas in the Invoicing subdomain you might want to represent it like this

    class Patient {
     
     CreditCard creditCard;
     CreditScore creditScore;
     Bill currentBill;
    
     void charge(Amount amount)
    }

Similarly, the term Cure in the Health Core subdomain, would have the operations that were/are_to_be performed on a patient to cure the disease whereas in the Knowledge subdomain it would contain information about Symptoms, Diagnosis tests, Prescription suggestions, that go along with a disease.

You can now see the Health Care subdomain has multiple BCs and under a BC each term has a very specific meaning thus supporting the Ubiquitous Language

  • 1
    Within the internet universe, this explanation was the only one help me understand the concepts and their differences. Thanks! – Subliminal Hash Jun 23 '22 at 11:10
  • This solution went in the right direction but a bounded context exists outside code. You are reflecting in code something that already happens: that doctors and administration staff have different context – borjab Feb 17 '23 at 17:27
9

Rereading the Booking Context from the blue book 18 times helped me finally get a handle: Maintaining Model Integrity: Bounded Context

This article helped as well: Sub-domains and Bounded Contexts in Domain-Driven Design (DDD)

Philipp Merkle
  • 2,555
  • 2
  • 11
  • 22
Chris
  • 1,690
  • 2
  • 17
  • 24
  • 5
    nice, but if you copy here from the articles what answer the question, it will be great! – Sameh Deabes Nov 03 '14 at 20:54
  • 7
    and here is a summary from the second article: A sub-domain delimits a domain and exists within the problem space. A bounded context delimits the domain model and exists within the solution space. The ideal is full alignment between a sub-domain and a bounded context, however in practice a degree of flexibility must be accepted in this regard. – Sameh Deabes Nov 03 '14 at 20:55
  • 1
    Disagree with the gorodinksi link as he speaks too much in a technical sense and not enough in a business sense. @JefClaes answer is much better. – Shane Courtrille Feb 19 '16 at 23:00
  • The first link seems broken – hannojg Feb 08 '20 at 13:00
5

First. The official definitions from the Blue Book is:

  • Domain: A sphere of knowledge, influence, or activity.
  • Bounded context: The delimited applicability of a particular model. Bounded contexts gives team members a clear and shared understanding of what has to be consistent and what can develop independently.

Note that those concepts exists by themselves before any architecture design or line of code is written down.

DDD is about having a mental model shared by business people and programmers that is reflected in source code. But with medium or bigger organisations it is not practical to have a single model. It is better to divide and conquer because:

  • Different areas have different needs, cultures, jargon, etc. Sometimes the same concept has different terms or viceversa.
  • Creating a big meeting to make people agree is costly and it is really hard to agree in something at this scale for so many people.
  • The cognitive load of developing an enterprise-wide mega application. Better to implement a components that can assigned to smaller teams.

So you reduce the domain modelling to an specific an concrete bounded contexts. This has the advantage of also reducing the complexity. But what if the same concept is used in several contexts? This leads me to the second question:

There should be a 1:1 relationship between a BC and an SD. However, I read in other places that this isn't necessarily the case.

No. There is no need. Here is an example from Martin Fowler: the products and customers subdomains are shared by the sales and support bounded contexts.

model with sales and support with several domains each but sharing products and customer domain

Of course you try to select bounded contexts as loosely coupled as possible. But just as when you separate modules in an app there is a minimum level of coupling to make the connection. So, the same concept is modelled differently in each context (Multiple Canonical Models). This can be implemented in code by adding an Anti-Corruption-Layer that translates between models.

Moving to a single bounded context is not just a matter of software design. It would require modifying the mental model of business and this is hard. Also, people sometimes have simpler views of a domain because it reduces the complexity and their cognitive load.

Concrete example:

In this talks from DDD Europe they have an example from Amazon:

The sub-domain term Book has a very different model in different bounded contexts:

  • In the Catalog bounded context: Picture, title, authors, rating...
  • In the Shipping bounded context: Dimensions, weight, international restrictions
  • In the Search inside bounded context: full-text content, copyright dealing policy

Book model by context

So Amazon may have very complex sub-domains with lots of attributes:

  • Books: isbn, title, number of pages)
  • Clothing: size, colour, material
  • Computers: cpu, graphic card, hard-drive, ram

But only some of them would be relevant in different subdomains.

Let me add a diagram with a more global example

diagram with example

Extra resources:

borjab
  • 11,149
  • 6
  • 71
  • 98
3

Please check this link it will help you, Bounded Context or Context? The term Context is a general description of a grouping of concepts, the term Bounded Context is more specific – a Bounded Context is an area of your application which has explicitly defined borders, has its own Model, and maintains its own code. Within the Bounded Context everything should be strictly consistent.

Usually, we can use the terms Context and Bounded Context interchangeably, though I tend to talk in terms of Context about the business side of things, and the term Bounded Context about the technical implementation.

Paulo Merson
  • 13,270
  • 8
  • 79
  • 72
3

In a very short and simple sentence, we can say: subdomains are part of the problem space and are chosen by The Business whereas bounded contexts are software boundaries defined by engineers.

Alireza Rahmani Khalili
  • 2,727
  • 2
  • 32
  • 33
1

Vaughn Vernon states in his book “Implementing Domain-Driven Design” the following:

"It is a desirable goal to align Subdomains one-to-one with Bounded Contexts." Page 57

1

A model's boundary, the bounded context, can contain ideas from various subdomains. Or a single subdomain might be represented by a number of bounded contexts. The ideal scenario would be one bounded context for one subdomain. If you are able to define multiple bounded context for a subdomain, that sometimes leans you into realizing that the subdomain is not fine-grained, and maybe the subdomain could be distilled into separated subdomains.

The other way around could also be justified, when you had multiple subdomain aspect covered in one BD, because e.g. that was pragmatic to do so.

More specifically, when the subdomain is generic, and the generic solution is easy to integrate, it may be more cost-effective to integrate it in each of the bounded contexts locally.

An example is a logging framework; it would make little sense for one of the bounded contexts to expose it as a service, as the added complexity of integrating such a solution would outweigh the benefit of not duplicating the functionality in multiple contexts.

proximab
  • 1,865
  • 1
  • 13
  • 18
0

When two different languages talking the same or similar thing, the thing is referred in 2 different contexts. You can translate the thing in 2 context in certain extents.

Similarly a term could have different meaning in different departments. in that case different context explain the term differently. Translation between two to some extent maybe possible.

Instead of saying “Bounded context” maybe try saying “bounded world”

0

My understanding about sub-domain and bounded context is-

Each subdomain represents a specific area of knowledge or responsibility within the overall domain, and each subdomain may have one or more responsibilities associated with it. In some cases responsibilities can split across multiple subdomains. Considering all theses issues, it can be useful to draw logical boundaries to separate those responsibilities and maintain consistency and transactional integrity.

Bounded contexts in Domain-Driven Design (DDD) are used to define these logical boundaries and provide a way to manage the complexity of large and complex systems by dividing them into smaller, more manageable parts. By using ubiquitous language we can ensure that the concepts and rules of that context are clearly understood and communicated within the development team. So we draw that boundary based on the uses language in that particular context.

So, in summary, a subdomain represents a specific area of knowledge or responsibility within the overall domain, and bounded contexts are used to manage the complexity of large systems by creating logical boundaries around specific areas of responsibility, and using a specific language, or ubiquitous language, to ensure clear communication and understanding of the concepts and rules within that context.

Bounded context ensure us that one responsibility doesn't blend with another one and finally prevent us from introducing complexity and confusion. In that sense it is very similar with SRP of SOLID

Mojammel Haque
  • 651
  • 1
  • 7
  • 19