Click here to Skip to main content
15,902,445 members
Articles / All Topics

A Common Pitfall of Domain-Driven Design

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
19 Jan 2015CPOL3 min read 10.5K   3   2
This is a common pitfall of Domain-Driven Design

The philosophy of domain-driven design - placing the primary focus on the core domain - is a very natural and powerful concept for application design. Despite the core idea being simple, the learning curve is apparently steep. DDD is very often implemented wrongly, an unideal architectural decision can lose the potential of obtaining and taking advantage of a rich domain model.

There is a specific pattern I've come across surprisingly often in systems built on the notion of DDD. I think it's useful not only to know this anti-pattern, but also think about why systems end up implementing it. Note how surprisingly easy it is to navigate a design to wrong directions.

Given some domain, the development starts with a single layer including some prototype of a domain model. Then the need for some infrastructure comes into picture - most commonly some sort of persistence. That's placed into another layer with the good intention of keeping responsibilities separate.
The result is this:

Domain model prototype

This won't work well, there is a problem with the dependency. The domain model depends on the infrastructure layer to do its task. The infrastructure layer can't access the domain objects - to persist them for instance - due to the direction of the dependency. There are many ways to solve this:

  • Pushing part of the model to the infrastructure layer. Responsibility leak, something to be avoided.
  • Transfer objects defined in the infrastructure layer. The domain will still be concerned with infrastructure specifics. 
  • Let's assume a somewhat better alternative is chosen: moving the domain model to its own layer:

Dedicated domain model

The infrastructure trouble is solved and layer design of many applications stops at this point. In my experience, it is a really common pattern. It looks like the real deal with the domain model having a dedicated layer. So let's take a look at the issues:

The domain model can't access the infrastructure layer. As a result, parts of the domain model that rely on infrastructure - actually most of it - will have to be moved up to the application service layer. Gradually - to keep things consistent, symmetrical - most logic will be moved out from the domain layer, leaving mere property bags behind with little or no logic. The end result of all the efforts is the classic case of an anemic domain model:

Anemic domain model

Where did things go wrong? As it turns out, at the very beginning, when the infrastructure layer was created. In DDD, the domain model is supposed to have the main focus, yet it was made dependent of a layer with infrastructural responsibilities. Subsequent efforts try to address this and while it succeeds in doing that, sacrifices are made on domain model.

The main problem was caused be the violation of the dependency inversion principle. This can be properly solved by the dependency inversion pattern:

Rich domain model

This simple change makes a world of difference: all obstructions are out of the way of building a rich domain model, implementing all the domain functionality. Other layers became thin - no domain logic in them - and can concentrate on their own responsibilities (e.g., application services or infrastructure integration details).

There may be valid reasons for anemic domain models too. In the context of tackling high-complexity problems with DDD though, the pattern described above is definitely something to look out for. It's very easy to compromise a domain model with constraints imposed by fundamental layering issues.

License

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


Written By
Norway Norway
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionExamples? Pin
Member 1011643225-Jan-15 23:33
Member 1011643225-Jan-15 23:33 
AnswerRe: Examples? Pin
mrcellux26-Jan-15 10:32
mrcellux26-Jan-15 10:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.