Ok i read many things about the repository pattern, including Fowler's book. I know pretty good what it is and what it does, however what i'm not quite sure yet is how it is called by factories and/or domain objects.
What I understood is that the repository is supposed to act like an in-memory collection of domain object, and the factory is the class in charge of the instance creation: new myDomainObject()
With that in mind, it seems obvious that the repository will need a reference to the factory to create new objects from the data source queries. (Repository -> Factory)
Domain objects also need a reference to the factory in order to create new objects.
My dilemma is when a domain object want to retreive an existing object, should it call the repository or the factory ? If it calls the repository directly (Domain -> Repository -> Factory), than it would need to have both the references to the factory and the repository, which seems too much to me, but is it that bad ?
On the other hand, if it calls the factory like factory.CreateObjectWithId(id)
, then the factory would have to only redirect the call to the repository repository.GetById(id)
, and this last would call another method on the same factory for object creation (if it is not already in memory) factory.CreateObject(dataset)
, so that leads to a circular reference:
Domain object -> Factory <--> Repository, which again does not seems really a good thing to me.
So in your opinion which of these options is better ? or is there another option ?