Maybe i’m already doing this in the “ideal” way but somehow it does not feel right to me.
I’ve got a search form which after finding the result instantly display the information over different controls (of which three are datagrids bound to eachother).
Currently i’ve set my UoW management up as following:
- When ever a users starts a search check if we had an old UoW and dispose it (and it’s session)
- Create a new UoW
- Begin transactions
- Perform search
- Commit transaction
Code example:
if(_unitOfWork != null)
{
_unitOfWork.Dispose();
}
_unitOfWork = new UnitOfWork();
_unitOfWork.Begin();
ICollection<Case> cases = casesQuery.ToList();
_unitOfWork.Commit();
iDeally i’d like all objects to be databound at this point, and after the databinding is completed i’d like to dispose of the UoW.
Sadly, i can’t do this because of the lazy loading beeing done to databind two of the three datagrids. Explicitly loading the collections & object graphs in these entities is not really an option due to the fact some of these entities are subclasses with different properties & references. Explicitly loading those seems like to much work.
What i’d like to know is if there is a way to make this nicer, i’ve considered the following:
- Use a Converter to get the value beeing databound, findout if it’s a proxy and if so load this proxy. This would mean i’d have to set a converter to every databound property which again seems inefficient
- Explicitly loading, reasons for not doing this are mentioned above
- My current solution, make the UoW span the “search”, biggest issue with this is that i cannot keep a transaction open while stuff is beeing lazy loaded. (I could but that seems like a VERY bad thing to do due to the fact a user can keep that transaction open indefinatly)
Does anyone have a better way to handle this situation or is what i’m doing currently the best thing possible?
In MVVM apps, the most common pattern is to have a session per VM. This enables lazy loading and rich client change tracking without much additional effort.
There’s nothing wrong with the current approach. Not being able to do lazy loading inside a transaction is not that big of a deal.
Now, explicit loading doesn’t look like “too much work” if you can create a reflection-based solution. But you still need to know what to load. In any case, it’s a lot less work than the converters idea.