3

I m studying DDD in a development context, and it seems that I've misunderstood something.

Actually, I was thinking that an Entity is the object that can be persisted on a storage support like a database.

But does it mean that the entity is the same thing than a row in my database ?

I mean there, can it exist objects between my Entities and my database, like a representation of a database row, that will be "converted" to my business entity ?

  • Are you asking about persistence models and domain models and if they are the same or not? – kayess Dec 14 '16 at 15:55
  • I think so, I m probably missing that part of the thing – mfrachet Dec 14 '16 at 16:02
  • Recommended reading onsite. – kayess Dec 14 '16 at 16:06
  • Often entities simply end up being a row in a table. But they also can end up in different rows in different tables. – qwerty_so Dec 14 '16 at 17:03
  • DDD is a design methodology. The database is an implementation detail. – Robert Harvey Dec 23 '16 at 16:43
9

But does it mean that the entity is the same thing than a row in my database ?

No, absolutely not. Your database might not even be a database; remember that a REPOSITORY provides "the illusion of an in-memory collection of all objects of that type" (Evans, chapter 6). It can, of course, support this illusion by in fact being an in-memory collection of objects.

I mean there, can it exist objects between my Entities and my database, like a representation of a database row, that will be "converted" to my business entity ?

Yes. For instance, in event-sourcing, "rows" in the database are representations of "events", each of which describes a change of the state of an entity. In such a system, you load the entity by reading from the the database a representation of the history of the entity, and then "replay" that history to get a representation of the entity itself.

4

But does it mean that the entity is the same thing than a row in my database?

No, not exactly. From the DDD perspective, an Entity is an element of the domain data model. It's part of the Ubiquitous Language. Read losttechies post about Entities.

DB rows are representations of the Entities states.

DDD does not state relationships 1-1 between DB rows and Entities. Quite the contrary. Abstract yourself from such detail.

The representations of a state could be stored in different rows, tables or why not, in different DBs. They could be stored in memory too, files, NoSQL DB's, etc

I mean there, can it exist objects between my Entities and my database, like a representation of a database row, that will be "converted" to my business entity?

Yes, It can. Whether to use ORMs (EF) or mere POCOS (Row-mappers) is implementation details.

I found vaughnvernon post to be very insightful. He exemplifies the approach with EF.

Especially this quote:

We are going to implement the Product Aggregate using two approaches. One approach uses a Separated Interface with an implementation class, and the other uses a domain object backed by a state object. The whole point of these examples is to stay as far out of Entity Framework’s way as possible.

1

Technically DDD doesn't really say much about how to persist data. In the DDD book by Eric Evans, he seems to prefer very simple schemas without a lot of joins. There's also some videos floating around with interviews of Evans wherein he extolls the virtues of NoSQL databases in the context of DDD. So it seems reasonable to conclude that when doing DDD most of your attention should be on modeling the domain well and persisting that model when needed in the simplest way possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.