0

I have been studying DDD for 1.5 weeks now, and I came a cross the use of the Unit Of Work pattern together with Data Mapper pattern. I understood why they are both important and when to use them but, I couldn't find anywhere how the Data Mapper can "know" that a save method, for example, was called in the middle of a transaction or not. Is there something wrong with the Data Mapper being aware of the Unit of Work? Something tells me that is not the right way to go.

2

The boundary for a Unit of Work is time. It answers the question: What all are we changing in this one transaction?

The boundary for a Data Mapper is space. It answers the question: What all is part of this one data structure?

The unit of work is used to prevent hitting the database too often by sending every little chunk of info one at a time. The data mapper translates the structure of those little chunks in memory to the structure of those little chunks on disk, and vise versa.

These don't need to know about each other. A single unit of work may involve many data mappers. A single data mapper may involve many units of work. The two are orthogonal to each other.

  • But when Martin gives an example of implementation for the Unit Of Wok on his commit method, to add the new created objects he simply loops through them and calls the save method on the right Mapper. So if the Mapper is not aware of the Unit how is it suppose to know to treat the saves as a transaction? – Lucas Piske Jul 20 '16 at 4:10
  • That's an implementation choice. Absolutely no reason it has to be done that way. A Unit of Work is just everything you want to tell/ask the database at once. The only limitation on work is getting to a point where you don't know what to do next until it's work is done. Where the data mapper puts its data is really not a concern in this context. – candied_orange Jul 20 '16 at 4:25
0

The data mapper has to know about the unit of work. You would typically pass the UoW to the mapper using DI.

I understood why they are both important and when to use them but, I couldn't find anywhere how the Data Mapper can "know" that a save method, for example, was called in the middle of a transaction or not.

A single UoW instance represents one active business transaction, the fact that the mapper has a valid UoW instance implies that you are in a business transaction and things are not committed yet; also whether the transaction is committed or not is of no concern to the mapper. UoW lifetime management happens at a higher level (E.g for a web app it is usually UoW er request).

  • Sorry, what do you mean by DI? – Lucas Piske Jul 20 '16 at 4:33
  • 1
    Dependency injection – Chamindu Jul 20 '16 at 4:35

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.