Page 1 of 1

I do not understand how DDD works with an ORM

Posted: Wed Feb 05, 2014 5:35 pm
by adoado
Hello everyone!

So I'm trying to piece together how domain driven design (DDD) concepts can work in harmony (if at all) with an ORM layer. I understand that the domain layer is meant to be persistent ignorant (has no knowledge of persistence) and domain focused (via some ubiquitous, domain language).

Then we have an ORM layer, which is (meant) to be a one to one mapping between records in a table and an object. So, if a table has three fields (id, name, email) then so does the class. If, say, we instantiate this class and call it A, then performing A.save() will persist these three object fields into the corresponding record in the table, via the primary key (id).

The problem is, where does DDD come into this? If we have this class A, it surely cannot be in the domain layer, as it's too close to persistence. What's even worse is that A.save() persists the class, while DDD dictates that a repository P save the class via P.Save(A); persistence is too close to the domain.

Do I put domain level logic into A?

Re: I do not understand how DDD works with an ORM

Posted: Wed Feb 05, 2014 9:51 pm
by Christopher
DDD lets you define your Domain objects. Those objects should may or may not have save() methods. If you are using an ORM then they will not have same methods. It is the DataMappers within the ORM that know how to persist the Repositories and Entities. So yes, put domain level logic into A and take the persistence out.