NHibernate 3.0 will come with some really nice features. One of them is the new query API QueryOver. You can now create type safe queries. So no magical and error-prone strings are required.
If you ask yourself how to use QueryOver for paging just read the following:
I defined an extension method that docks on every object that implements
IQueryOvery<T,T>
public static class QueryOverExtensions
public static PagedList<T> ToPagedList<T>(this IQueryOver<T, T> queryOver, int pageIndex, int pageSize)
{
var rowCountQuery = queryOver.ToRowCountQuery();
IEnumerable<T> list = queryOver.Take(pageSize).Skip((pageIndex - 1) * pageSize).Future();
int totalCount = rowCountQuery.FutureValue<int>().Value;
return new PagedList<T>(list, pageIndex, pageSize, totalCount);
}
}
You could use this extension in a repository as follows:
public class TeamEmployeeRepository : Repository<TeamEmployee>, ITeamEmployeeRepository
{
public PagedList<TeamEmployee> GetPagedEmployees(int pageIndex, int pageSize)
{
return GetSession().QueryOver<TeamEmployee>()
.Fetch(x => x.Employee).Eager
.Fetch(x => x.Team).Eager
.ToPagedList(pageIndex, pageSize);
}
}
This code says, create a QueryOver object for the object TeamEmployee and fetch all Teams and Employees by using the eager fetching strategy. The extension method takes the result and pages it.
I really like this
It is type safe and very easy to read!
