domain-driven-designaggregaterootbounded-contexts

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?

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?


Solution

  • 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.
        }
    }