c# - ORM / How to deal with the correspondence between Domain object and Persistent object? -


in application, there @ least 2 ways dealing domain object persistence , orm.

  • mapping directly domain object persistence using kind of orm (xml or annotations)
  • making separation of concerns in case of lot of impedance mismatch between domain , persistent model (table columns). means, domain object persistence agnostic , there conversions corresponding persistent object, latter maps orm.

as pure ddd developers know, domain should not driven database needs, in project, use separation of concerns. think of yagni, "great" (like here). project need different databases according need of reusability, chose separation of concerns between domain model , persistent model. came across issue (some kind of performance loss), spring-data. detail maybe, assume orm doesn't have functionnality of merge, or related, reattach detached entities current transaction.

to understand, let's suppose conceptual code (in java):

@transaction public void participatetomeeting(string userid, string meetingid){   user user = userrepository.ofid(userid);  //returns user domain type   meeting meeting = meetingrepository.ofid(meetingid); //returns meeting domain type   if(user != null && meeting != null) {     user.participate(meeting);    // attached entity, automatically persist relationship   } } 

but if henceforth, persistence occurs on persistent model, not domain model directly, we'd lose attachement, since during conversion domain persistent object (indeed, repositories deal persistent objects (instead of domain model directly) , merely convert result domain object return type), managedentity state lost.

    @transaction         public void participatetomeeting(string userid, string meetingid){           user user = userrepository.ofid(userid);  //returns user domain type (converted userpo user)           meeting meeting = meetingrepository.ofid(meetingid); //returns meeting domain type (converted meetingpo userpo)           if(user != null && meeting != null) {             userrepository.participatetomeeting(user, meeting);  //although not conventional, adding kind of method allows convert user , meeting persistent object: userpo , meetingpo, before proceeding persistence           }         } 

here's issue: while converting user userpo (in infrastructure layer), lose entity "attachment". thus, in userrepository.participatetomeeting method, have retrieve userpo , meetingpo again database (to make them attached)...therefore involving, 2 additional requests.

is there better practice deal conversions domain object/persistent object without performance loss?

i disagree linked article. while agree concerns between domain model , persistence model different, entire purpose of orm map between domain model , persistence model. since orm supposed provide mapping, creating additional class hierarchy facilitate mapping overkill , can lead problems 1 you're describing. fact domain model resembles data model indeed far more mere coincidence. instead, both representing aspects of same domain , should therefore have high degree of correspondence. orm designed address mismatch between object model , corresponding relational model. there cases mapping gets tough, in nhibernate example, these can addressed implementing custom user types component mappings.


Comments