Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DDD - How to design associations between different bounded contexts

I have setup a domain project which is being populated with an ORM. The domain contains of different aggregates each with its own root object. My question is how properties that cross the aggregate boundries should be treated?

  • Should these properties simply ignore the boundries so that a domain object in bounded context A has a reference to an object in context B?
  • Or, should there be no direct link from context A to B and does the object in context A have an "int ContextBId" property that can be used to get the domain object from B through the B aggregate root?
  • Or ...

An example:
Context A = Users
Context B = Games

Inside the Users context there is an object UserOwnedGames. This object has a property User which is a reference to an object in the same Users context. The object also has a property to a Game which is obviously not in the Users but rather in the Games context.

How would (or should?) this relation look like? It's clear in the database (ie 2 foreign keys) but what should the code look like?

like image 821
Laoujin Avatar asked Sep 12 '13 09:09

Laoujin


People also ask

Can a domain have multiple bounded contexts?

Anything that shows domain concepts, relationships, rules, and so on. Since a bounded context is a boundary for a model, it could include concepts from multiple subdomains. Or a single subdomain could be modelled as multiple bounded contexts.

Can bounded contexts overlap?

Bounded context and the organizationThe overlapping of concepts is quite natural in business domains; speaking in general, the best way to handle such overlapping is to use different bounded contexts, as shown in the third option of Figure 5-4.

Can a bounded context have multiple aggregate roots?

A single Bounded Context can include many aggregate roots, or we can organise a single aggregate root into a single Bounded Context.

What is the relationship between a bounded context and a domain 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.


1 Answers

It sounds like your User context also needs a Game entity. Note however that this is not necessarily the same Game entity which is the root of the Game context. These two bounded contexts may have different ideas of what a Game is, and what properties it has. Only the identity ties the two Game objects together.

User Context
{
    Aggregate Root User
    {
        Identity;
        Name;
        OwnedGames : List of Game value entities
    }

    Value Entity Game
    {
        Identity;
        Name;
    }
}

Game Context
{
    Aggregate Root Game
    {
        Identity;
        Name;
        Owner : User value entity
        HighScore : int
        TimesPlayed : int
        ... A whole bunch of other properties which are not relevant in the User context
    }

    Value Entity User
    {
        Identity;
        Name;
        // No OwnedGames property, in this context we don't care about what other games the user owns.
    }
}
like image 82
MattDavey Avatar answered Oct 02 '22 14:10

MattDavey