views:

43

answers:

1

Hi,

There is something not clear to me about domain-driven-design and unit of work pattern. Let's say I have an entity that was retrieved from a repository. Once I updated this entity, how UnitOfWork should know that it was updated, so the updated entity can be persisted to the DB? Currently, I see two options:

1) Manually call uow.Update(entity) in the service layer. E.g.:

Employee emp = EmployeeRepository.GetByID(1);
emp.Name = "NewName";
uow.Update(emp);
uow.Commit();

2) Rely on some ORM magic to track changes, and persist updated entities to the DB. E.g.:

Employee emp = EmployeeRepository.GetByID(1);
emp.Name = "NewName";
uow.Commit();

Both these options seems hairy to me: First - if changes were made inside some aggregate, the aggregate should let the service layer know that it updated some entities. Second - isn't relying on ORM implementation is violation of Persistence Ignorance principle?

What do you do in your projects? Or maybe I miss something / messed up something and there is better solution?

Thanks

+1  A: 

First: Why does the aggregate have to let anyone know that it updated some entities? If you're using 'Repository per Aggregate Root' and a proper ORM that will persist your entire graph you can do something like this..

Employee emp = employeeRepository.GetById(1);

emp.RewardWith(new GoldStar());

employeeRepository.Save(emp);

Any half decent ORM will be tracking the entire employee graph so it will know that the employees star chart has been modified and persist changes accordingly.

Second: your domain needs to be persistence ignorant.. it is very important that developers working with the system not be :)

ShaneC
+1 - persistence ignorance is about keeping your persistence concerns out of your **domain objects**. It *requires you* to rely on an outside provider that knows how to monitor changes to aggregates (like an Object/Relational Mapper). See [The Unit of Work Pattern and Persistence Ignorance](http://msdn.microsoft.com/en-us/magazine/dd882510.aspx) for a great summary of what PI buys you and why it's absolutely appropriate to let an ORM handle it..
Jeff Sternal
Thanks ShanceC and Jeff, you both helped me a lot!
Vladikk