Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

anemic domain model versus domain model

Being confused again after reading about this anti-pattern and the many concerns about it here on SO.

If I have a domain model and capture the data that must be persisted in a data transfer object, does that make my domain model a wrapper around the data? In that case I would be using an anemic domain model. But if I add enough domain logic on that wrapper, at what point does it become a real domain model then?

I get the impression that capturing what must be persisted in a domain model violates good practice and creates the anemic domain model anti-pattern. Yet if you use a relational DB there's no way to avoid to single out the part that makes the state of the object and save it.

Since I'm pretty confused about the concepts I'm not sure that what I write makes sense. Feel free to ask clarification.

like image 237
koen Avatar asked Nov 26 '09 21:11

koen


People also ask

Is anemic domain model good?

As great boosters of a proper Domain Model, this is not a good thing. The basic symptom of an Anemic Domain Model is that at first blush it looks like the real thing.

What is the difference between model and domain?

A domain model is a representation of the organization's data, independent of the way the data is stored in the database, with a domain being the collection of all the objects in that system, while the data model is used in database design and development.

How does domain model prevent anemia?

Ways to avoid an anemic domain model Use private setters. If your properties are defined by the Client directly you will lose the chance to use Domain Events and you will have to validate your Entities by external methods. Always validate the status of your entities, your Entities must self validate.

What is domain based model?

Domain Modeling is a way to describe and model real world entities and the relationships between them, which collectively describe the problem domain space.


1 Answers

It becomes a 'real' domain model when it contains all (or most) of the behaviour that makes up the business domain (note I'm emphasising business logic, not UI or other orthogonal concerns).

If you're using the Ubiquitous Language, and getting constant feedback from your domain experts, you'll know that you're on the right track (experts should nod when they see your domain model). If you're not doing these things, you're not doing DDD (Eric Evans speak about it).

Onto the point of DTOs: Don't ignore them. From an implementation perspective, you'll need them to ferry data between layers/tiers. How you combine DTOs and true Domain Objects really depends on the technology you're using.

As alluded to in an earlier answer, maybe your focus on data and persistence is distracting you from true domain modelling...

like image 114
Vijay Patel Avatar answered Oct 13 '22 07:10

Vijay Patel