Click here to Skip to main content
13,837,307 members
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 23 Dec 2014
Licenced CPOL

Composition for Partial Aggregate Domain Model

, 23 Dec 2014
Rate this:
Please Sign up or sign in to vote.
How to handle (load/save) only a part of an aggregate domain model with composition


The goal of this tip is to see how to work with a partial aggregate in a bounded context, this is an optimization for loading/saving, so we don't have to load or save the full bounded aggregate.


Bob is writing a new game with superheros, they have several powers and he wants to modelize this into his new game. First, we write the interface, a superhero is an IHero, and we have an IBatman and ISpiderman which both inherit from IHero. In the game, later the player will be able to fusion the two characters to have a more powerful one, let’s call him IBatmanSpiderman, which inherit from IBatman and ISpiderman. Everything is perfect, except modern languages doesn’t support multiple inheritance, so it’s not possible to create the BatmanSpiderman class inheriting Batman and Spiderman.

Enterprise Application

The problem still arises for enterprise applications. Imagine the company works with Domain Driven Design (DDD), where the domain are business objects they loaded in their bounded context. Due to performance reason, sometimes we want to load the full bounded context, sometimes only a part of it, some classes but not the others. One solution would be splitting the bounded context into smaller bounded contexts but it’s not always possible because we don't work anymore with a logical business set of data, so we’ll need then to join data.

Composition to Rescue

Object composition is a way to combine simple objects or data types into more complex ones. We introduce the notion of aspect, we can say an aspect is the superpower of Batman or Spiderman, meaning it’s the properties/methods we’d like to add to our root object. For the interface, IBatman is either an IHero and IBatmanAspect, ISpiderman is either an IHero and ISpidermanAspect, and IBatmanSpiderman is an IHero, an IBatmanAspect and ISpidermanAspect.So we can substitute any IHero by an IHero with superpowers, and for example, IBatman has the methods of IHero, and the methods of the IBatmanAspect (the superpower).

For the concrete class, we can see we use composite to connect the aspect (XAspect) to the real class (X). In the implementation, if the BatmanAspect has a method “Fly()”, then Batman should implement a method Fly() with returning the Fly() method of the BatmanAspect.


Here is the final synthetic diagram. Of course, the number of aspects is not limited, it works for methods, properties and members, and this pattern is a good substitute over multiple inheritance or working with partial domain model. But as many patterns, it has a cost of writing and a bit more complicated, as you can see in the code below:

Interface IA {}
Interface IBAspect { void MethodB(); }
Interface ICAspect { void MethodC(); }
Interface IAWithB : IA, IBAspect {}
Interface IAWithC : IA, ICAspect {}
Interface IAWithBAndC: IA, IBAspect, ICAspect {}

Class A {}
Class BAspect { void MethodB() {} }
Class CAspect { void MethodC() {} }
Class AwithB {
     private BAspect bAspect = new BAspect();
     public void MethodB() { return bAspect.MethodB(); }
Class AwithC {
     private CAspect cAspect = new CAspect();
     public void MethodC() { return cAspect.MethodC(); }
Class AwithBwithC {
     private BAspect bAspect = new BAspect();
     private CAspect cAspect = new CAspect();
     public void MethodB() { return bAspect.MethodB(); }
     public void MethodC() { return cAspect.MethodC(); }


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Dominique Gilleman
Belgium Belgium
I'm a software engineer working for a company in the field of laboratory services. I started programming at 10 years old, first in basic and pascal, and now in C++ and C#

My main interests are Software architecture, Team leading, Algorithms and complex problems.
I did independant project in image analysis, clustering software, 3D visualization and Enterprise social networks

You can have more information on my website:

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190114.1 | Last Updated 23 Dec 2014
Article Copyright 2014 by Dominique Gilleman
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid