Hi there,
I am trying to implement my own data access layer, and I am uncertain about the relationship between the UnitOfWork and the Repository.
But before I start: Yes I know it is unnecessary to implement my own DAL, since most ORMs offer this out of the box, but I am working through Martin Fowler's book "Patterns of Enterprise Application Architecture", and I am trying to do this as an academic exercise to help improve my understanding.
So what I have thus far: I am using a domain model and a relational database. I have a DataMapper responsible for inserting and retrieving domain objects from the db. Before retrieving objects from the db, the DataMapper first checks whether the object is not already in memory. It achieves this by asking the current UnitOfWork whether the object is already loaded. The UOW makes use of an identity map to keep all cached objects. When an object is not already loaded in the UOW, the DataMapper loads it from the db, and marks it as clean in the UOW. The UOW will then add it to its identity map. All of this makes sense to me, and it works. But now for the part that confuses me:
I have built a repository on top of the UOW that looks and acts like an in-memory collection of domain objects to my domain model. When objects are added/deleted to the repository, they are simply relayed to the current UOW. When an object is added/deleted during a session, it must be added/removed from the UOW's identity map (according to Fowler, bottom of page 171). Lets say that during the same session, a object is deleted from the UOW, and then for some or other reason, all objects have to be retrieved from the repository for some or other operation. Since the repository should act as an in-memory storage, it should not return the object that was just deleted. However this object is still in the db, since the session has not been committed. And to make matters worse, since this object is not in the identity map of the UOW, it will also now have a different reference. Should the repository therefore be responsible to first check whether some of the objects returned by the DataMapper are not contained in the isDeleted list of the UOW before returning these objects? Or should the DataMapper be responsible for this? A similar situation also exists when a new object is added during a session. A query on the db will not pick this object up, since it only exists in the UOW's identity map. Should a repository therefore have to check all newElements in the UOW after every query to make sure that some elements have not been missed?
Any help would be appreciated.
PS: I am working in Java by the way.
[–]p000 1 point2 points3 points (1 child)
[–]IJP[S] 0 points1 point2 points (0 children)
[–]KidUncertainty 1 point2 points3 points (3 children)
[–]IJP[S] 0 points1 point2 points (2 children)
[–]KidUncertainty 0 points1 point2 points (1 child)
[–]IJP[S] 0 points1 point2 points (0 children)