Hibernate version:
Production
Question
I have a WebForm page that makes it possible to list entries and sort them by different criteria. For this I use a class I call EntriesBLL and EntryCollection : List<Entry>, IComparer<Entry>.
The entry page binds to a GridView when it first is called - all entries in the database for this specific user.
Then I can sort by different criteria, but what about the overall design to minimize database queries. First I created an instance of EntryCollection in the page, but I couldn't reuse it because it was recreated for every page load. Would it be a good idea to store the EntryCollection instance in the session? Or in the viewstate?
Right now I'm storing it in the viewstate. But then; what about when I sort it? Should I use NHibernate to do the sorting, by creating a new ICriteria, adding ICriteria and ICriterion-s onto this, (and hence by fetching it all again from the database) or has NHibernate some way of caching a collection and then sorting it when needed?
This is a method I use to sort the data:
Code:
public EntryCollection GetEntriesByStatusAndType(int StatusId, int TypeId)
{
IList results = null;
try
{
session = factory.OpenSession();
ICriteria baseCrit = session.CreateCriteria(typeof(Entry));
if (StatusId != 0)
{
ICriteria statusCrit = baseCrit.CreateCriteria("Status")
.Add(Expression.Eq("Id", StatusId));
}
if (TypeId != 0)
{
ICriteria typeCrit = baseCrit.CreateCriteria("Type")
.Add(Expression.Eq("Id", TypeId));
}
results = baseCrit.List();
}
catch { throw; }
finally
{
session.Disconnect();
}
EntryCollection entries = new EntryCollection(results);
return entries;
}
and as you can see, it takes the route I just talked about...
What would be a good design pattern to use here?