Syndicate

News

My name's Marco De Sanctis and I'm an IT professional from Italy. This is my technical blog, about .NET and related application development and design technologies.

Download my Resume (.doc)

Recent Comments

10/23/2008 at 3:48 PM

sorry, my previous post was not complete... I trie...
by glondi

Read more...

9/22/2008 at 10:03 AM

Good one!!!Could you please post source code for t...
by Nisa

Read more...

9/10/2008 at 5:53 PM

I didn't even realize people were aware of the WPF...
by Mike Brown

Read more...

9/8/2008 at 1:22 AM

Marco,How are you handling scenarios where you nee...
by ctodd

Read more...

9/3/2008 at 1:00 AM

@ctodd: Hi,basically for the reasons I described a...
by Marco De Sanctis

Read more...

Recent Posts

Crunch mode is a pure waste of time, energy and money

7/31/2008 at 5:41 PM

Read more...

Double Click on the .sln file doesn't open Visual Studio on Vista

7/27/2008 at 9:02 AM

Read more...

Domain Model & Aggregates: when do master-detail associations happen?

7/22/2008 at 4:08 PM

Read more...

How I Got Started in Software Development

7/14/2008 at 12:16 AM

Read more...

Unleash the power of VisualStateManager with custom states

6/30/2008 at 12:12 AM

Read more...

Domain Model & Aggregates: when do master-detail associations happen?

posted on Tuesday, July 22, 2008 4:08 PM | Filed Under [ Software Architecture ]

During my experience as a consultant, I usually come across domain models in which entities are so interconnected that my head begins to hurt just a minute after I'm trying to figure out where each aggregation symbol leads to, and often the source of all pain is the overuse of master-detail relationship.

An association in a domain model is something etheral, something that apart from its implementation, represent a logical connection between two or more abstract entities. It usually translates itself as a reference in a Object Oriented world and, for example, as a FK relationship between two tables in a relational database.

Now, let's consider a Bill - BillDetail relationship: no one would disagree if I modeled it with the following couple of classes, building a two ways master-detail association:

image

When we project it to a relational database schema, it becomes something like this:

image

Relational representation lacks a lot of the expressivity the object oriented paradigm provides: looking at the classes topology, we can explicitely see that one Bill instance holds a reference to a collection of BillDetails, and thanks to BillDetail.ParentBill property, we can walk that association in both directions while navigating the object graph; all those infos are only implicitely stored in the relational FK association.

In fact, what happens when we consider the Customer the bill belongs to? Let's start from DB schema this time, as most of us are (incorrectly) still used to do while designing a domain model:

image

Now, the inexperienced (but somehow smart) designer should feel that something's different between the two aforementioned examples, even though the relational representation is exactly the same: the association from a bill towards its customer is something we all agree with, but what about the mirrored association? Should we blindly equip our Customer entity with a Bills collection? Or should we simply ignore that side, perhaps providing a GetBillsByCustomer(..) service if we really need to walk from a customer to his bills?

There's a subtle differerence going on between these two examples, something that, once again, is not easy to infer if we're using the database schema as the starting point for our modeling process. A bill detail is something unuseful if taken away from its root; the "Bill" entity with all its details, encompasses the business concept of a real world "Bill". Using a DDD term, they are an Aggregate, whose root is the Bill itself. In that case, implementing a master detail relationship between the two makes a lot of sense, because you're always going to access each bill detail coming from the main Bill entity (you should never access directly each bill detail, according to what Eric Evans states in its Domain Driven Design book).

The relationship between Customer and its Bills now is clearly different! There's nothing bad on directly referencing a Bill, without coming from the Customer, actually a Bill makes a lot of sense (and has a huge business meaning) even though we don't even take care of which customer it belongs to! in other words, Customer and its Bills are not an aggregate.

Why we should avoid master-detail relationship between the two? Because associations are cool, but don't come for free: they have a great cost in terms of complexity and performance, even though we can leverage some ORM to limit the impact on the application. What could be the purpose of a "Bills" property into the Customer class? Even if we suppose to lazy load it, simply accessing it would trigger a query for every bill he has, regardless of the period, of its paying status, and so on. Does it have a business meaning? I don't think so, but that wrong design choice makes us paying its cost anyway.

In other words, a good rule of thumb is: if it's not an aggregate, limit the cohesion and try to avoid master-detail relationships because they are expensive.

I hope many of you agree with that.

kick it on DotNetKicks.com

Comments

Gravatar
# re: Domain Model & Aggregates: when do master-detail associations happen?
Posted by Tommaso Caldarola on 7/28/2008 9:58 AM
In aggregates the rula is: who is the rool element of aggregation? If Bills then the Customer relationship make sense, else as you said, it's a bad way to do.
Gravatar
# Xanax no prescription overnight delivery.
Posted by Xanax. on 7/8/2009 7:43 AM
Xanax while pregnant. Xanax.
Gravatar
# Re viagra cello.
Posted by How does viagra work. on 7/8/2009 11:56 PM
Free viagra sample. Generic viagra no prescription. Re viagra cello. Viagra.
Gravatar
# Percocet sexual effects.
Posted by Percocet. on 7/9/2009 11:33 PM
Can you snort percocet. Percocet dosing. Percocet withdrawal symptoms. Percocet.
Gravatar
# Soma information.
Posted by Carisoprodol soma drug test. on 7/10/2009 11:39 PM
Soma overnight. Soma muscle relaxant. Soma online sales. Soma. Pornstar soma. The strokes is this it soma lyrics free.
Gravatar
# Phentermine cod.
Posted by Phentermine. on 7/11/2009 11:35 PM
Phentermine online. Phentermine. Order phentermine online. Danger of phentermine. Phentermine on line. Phentermine hcl. Phentermine risk.
Gravatar
# Viagra.
Posted by Re viagra cello. on 7/13/2009 12:00 AM
Free viagra. Generic viagra. Re viagra cello.
Gravatar
# Vicodin side effects.
Posted by Vicodin user message board. on 7/16/2009 12:09 AM
Vicodin. Online vicodin. Effects of vicodin.
Gravatar
# Phentermine 37.5mg.
Posted by Phentermine. on 7/17/2009 2:16 AM
Cheap phentermine. Phentermine. Phentermine overnight. Buy phentermine mg. Phentermine no prescription. Phentermine online pharmacy.
Gravatar
# Soma without prescription.
Posted by Soma. on 7/19/2009 12:34 AM
Soma. Drug soma cause any sexual side effects.
Gravatar
# Xanax 2mg no prescription.
Posted by Xanax. on 7/20/2009 12:51 AM
Xanax no prescription. Xanax xr crushed. Cheap generic xanax 2mg bars. Xanax cocktail. Xanax 2mg. Titel auteur datum xanax medication bmnwbbuk.
Gravatar
# Effect of vicodin.
Posted by Vicodin. on 7/21/2009 11:11 PM
Vicodin abuse. Forum vicodin buy vicodin online. Vicodin side effects. Vicodin. Dangers of vicodin. Buy vicodin online. Is it safe to snort vicodin. Lorcet vicodin online.
Comments have been closed on this topic.