This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi, I bought your Lightspeed and I am a happy customer. Nevertheless I have an question about loading a self refereced entity and holding this in the memory. In my wpf-application i have an entity "MenuItem" which itself can reference to it self (submenuitems). Because of comfort and optimization I tried to eager load all menuitems. But this doesn't work for the subitems (and subsubitems) and i have to attach the menuitem evertime in order to get the subitems. Is there a way to load this complete into the memory? Or is there a way to bind the menu to this entity? Or a better way? Best regards Markus PS: I already use Eager loading in my application in other cases, but there is no self referencing. |
|
|
LightSpeed doesn't support indefinite eager loading of self-references. The reason is that most databases don't provide a means to do this hierarchical query (some have extensions, but most don't), and if we can't emit our normal "where exists parent s.t. child.fkid = parent.id"-style eager-load SQL because we don't know how many levels of eager loading (how many SELECT statements) we'd need to do to get the full hierarchy. So in order to avoid database queries as the user progressively opens menus and submenus, I think you would need to manually traverse the hierarchy, thus forcing each entity to eager-load its children, until you reached the end. Something like: void ForceAllDescendantsToLoad(MenuItem item) { Although this method seems vacuous, its real purpose is in its side-effect of causing each SubMenuItems collection to load. Once you have done this you will have the full hierarchy in memory and should be able to bind to it without incurring further database queries. |
|
|
Wow, really fast answer - thanks. The problem is, that we have a lot of dependencies of the menuitems and i have to save e.g. IsChecked, IsEnabled etc. But i don't want to attach each time to the database if there is a changing. In my current implementation, I load all items in the way you suggested. And additional i add the respected entity to the menuitem tag in order to save changings later.> eager-load SQL because we don't know how many levels of eager loading Yes, I understand the problem. Also there could exist a circular referencing :)
But then we have found a feature :) An addition property "Level" for eager loading. Thanks and have a nice day Markus |
|
|
In my database I have a similar situation. I solved this by having each item reference it's "main" or top level item. This way each item references its parent and the master or top level. So all you have to do is eager load the items in the Top Menu. if your Menu Structure is very large you could even have items buried in the structure classified as "Main items" just set their "MasterItem" = null and have all sub items use them as their master and not the top menu item.
|
|
|
Hi MiddleTommy, good idea, i am using now a similar way. First loading all items into the memory: allMenuItems = unitOfWork.Find<MenuItem>.ToList(); Then i create the whole menu using the items in the memory: foreach (var item in allMenuItems .Where(mi => mi.ParentMenuItem == null).OrderBy(mi => mi.Position)) { ... } Works fine for me |
|