Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read model per bounded context

i have a question related to read models in cqrs.

Assume we have two bounded contexts: A and B

In context A we build a readmodel based on the events from context A. We have some kind of dao to access the readmodel in A.

Now assume that B needs the same read model as A. As far as i understood, bounded context shouldn't depend on each other.

So how can i use the model from A. I see three possiblities to solve this

  1. Create an API Module for the read model in A and use this in context B (Would be a dependency between A and B)

  2. Create an seperate read model in context B thats exactly the same as in A (Would result in code duplication)

  3. Create a service facade (REST or SOAP or whatever) in B that is accessible from A to provide the read model (possibly the service doesnt provide exactly the data needed)

like image 860
user948392 Avatar asked Aug 23 '13 08:08

user948392


People also ask

What is bounded context model?

A bounded context is simply the boundary within a domain where a particular domain model applies. Looking at the previous diagram, we can group functionality according to whether various functions will share a single domain model. Bounded contexts are not necessarily isolated from one another.

How do you determine bounded context?

To identify bounded contexts, you can use a DDD pattern called the Context Mapping pattern. With Context Mapping, you identify the various contexts in the application and their boundaries. It's common to have a different context and boundary for each small subsystem, for instance.

What does the term bounded context mean?

A Bounded Context is a logical boundary of a domain where particular terms and rules apply consistently. Inside this boundary, all terms, definitions, and concepts form the Ubiquitous Language.

Is read model part of the domain?

In event sourcing, read models do not represent domain entities by definition. The only source of truth is the event stream of that entity.


2 Answers

Your read models don't belong to any bounded contexts, they are produced by some domain objects in some bounded context. But they are a separate component of your system.

Your bounded context shouldn't need any read model. Read model is an output of the domain, not the input. If you need 2 BC communicate, use events, not read models. Read models are for GUI/reporting, not for processing business rules.

like image 105
Bartłomiej Szypelow Avatar answered Oct 21 '22 13:10

Bartłomiej Szypelow


It's actually very common to have dependencies between contexts, see relationships and context mapping in the DDD Reference.

In your example, context B depends on context A. Depending on the type of relationship (upstream-downstream, partnership, ...) context A decides how to let context B integrate with them (open-host, customer/supplier, ...).

Context A can provide a read model, events, or both. Integrating via events gives you independence, though integrating via a read model might be more practical for your example (but might cause friction once context A decides to diverge). Factors to take into account are your relationship with the other context, and the probability of change vs. cost of change.

like image 39
Martijn van den Broek Avatar answered Oct 21 '22 13:10

Martijn van den Broek