Speed related question - (F)NHibernate
I have a class A which contains a list of Bs: IList. This associates As with Bs (mapped as a many-to-many relationship). The association between As and Bs is determined by an external source which contains the Bs. Please note that I have a copy of Bs in my database for efficiency reasons. From time to time I have to run a script that determines all Bs that belong to a particular A. The script returns some keys that I can use to obtain the Bs from a repository.
Now each time I run this update I clear the list of Bs and get each B via my repository. This is very inefficient. I am just wondering whether there is anything I can do to make this faster?
I have tried to just use a list of ids of Bs here:
Fluent Nhibernate problem
but I cannot pe开发者_Go百科rsist the ids for some unexplained reason so I may have to use a list of ‘fully blown’ entities again, which is no problem but has this overhead of obtaining each entity one after another via the repository.
Any feedback would be very much appreciated. Many thanks.
Best wishes,
Christian
for this case there is Session.Load
X.Blas.Add(Session.Load(bid)); // returns a proxy with initialized Id, almost the same as the id
and i think X.Blas.Clear();
would be better than X.Blas = new Iesi.Collections.Generic.HashedSet<B>();
Edit: to load multiple Bs efficiently you can also do
var blist = Session.QueryOver<B>()
.Where(b => b.Id.IsIn(IdList))
.List();
to further save some work you can
foreach(var bid in idList)
{
var b = Session.Load(bid);
if (!x.SetOfBs.Contains(b))
x.SetOfBs.Add(b);
}
精彩评论