I have a datamapper class that will eager load any property that does not have a Lazy attribute attached to it. I have two entities, State and Country, Country has an inverse relationship to state in that it contains a list of all the states of that country and State has a forward relationship to Country in that it has a property Country that eager loads the country that it is assigned to. However if I try and retrieve one of these objects, lets say a State this is what happens:
- State is loaded by mapper
- Mapper reaches an eager property Country
- Mapper retrieves the country for that state
- Mapper loads the country
- Mapper reaches an eager collection property of States
- Mapper loads a list of states and begins mapping each individual one using the cache where it can.
- GOTO 1 for each state loaded in country
I am at a loss as to how I can avoid this loop. So mainly I am looking for ideas. I will post any code anyone asks, but this process encompasses A LOT of lines of code so I didn't want to flood the question with code.
Thanks in advance!
Edit:
Alright after following Matt Howells advice and investigating deeper into the datamapper pattern Martin Fowler does indeed speak about a cyclic reference on page 169 and 170. His suggestion is to use an empty object and load it into an identity map and return it thus stopping the recursive loading. I've read this paragraph about 1000 times now and I still don't understand how this stops the load and beyond that I am lost as to when or how I would know when to load this empty object into my identity map. I apologize for being dense here, but this just seems to be flying right over my head.
Thanks again.