I do not understand how DDD works with an ORM

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
adoado
Forum Newbie
Posts: 2
Joined: Fri Dec 27, 2013 9:12 pm

I do not understand how DDD works with an ORM

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post 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.
(#10850)
Post Reply