This is a question about the data mapper pattern, in which the mapper is responsible for CRUD operations on domain models in a flexible manner. The data mapper pattern is usually used together with UnitOfWork pattern. The UnitOfWork keeps track of the states of domain objects, it manages which objects will be inserted, updated or deleted. But there is a problem with UnitOfWork used in conjunction with DataMapper object.
I've heard from many articles that a good data mapper design the domain models are supposed to be unaware of the mapper's existence. For instance, if I create a User Object, it's should not keep a mapper object as its property. This does not seem to work at all with UnitOfWork, since when you commit on a UnitOfWork object, it will carry out the insert/update/delete operations on domain models. Now we are getting down to the main issue, these are operations that the data mapper handles, but how is a UnitOfWork object supposed to know about the data mappers? In other words, how does a UnitOfWork, a storage of domain models, know how to get the mappers from somewhere?
To me it seems that there are only two possible ways: Make the domain model aware of the existence of data mapper, or use static methods on mapper registry. I definitely hate the latter, but the former also comes with a price as the domain model will now possess the knowledge of its mapper object.
What do you think about this issue? How can it be resolved? Is it not a problem even if the domain model is indeed aware of the existence of mapper? Or are there other approaches to solve this dilemma? Please lemme know if you can help, thx.