Phân trang trong Nhibernate

Đây là lớp giúp phân trang trong Nhibernate với Lớp T

public abstract class Paging<T>
   {
      public IList<T> GetPagedData(int page, int pageSize, out long count, Order oder, params ICriterion[] criterion)
      {
          IMultiCriteria multiCriteria  = NHibernateHelper.CurrentSession.CreateMultiCriteria();
          ICriteria criteria1 = NHibernateHelper.CurrentSession.CreateCriteria(typeof(T));
          ICriteria criteria2 =
              NHibernateHelper.CurrentSession.CreateCriteria(typeof (T));//.SetProjection(Projections.RowCountInt64());
          foreach (var item in criterion)
          {
              criteria1.Add(item);
              criteria2.Add(item);
          }
          criteria2.SetProjection(Projections.RowCountInt64());
          criteria1.AddOrder(oder);         
          multiCriteria.Add(criteria1);
          multiCriteria.Add(criteria2);
         // page = page > 0 ? page : 1;
          criteria1.SetFirstResult(page * pageSize).SetMaxResults(pageSize);          
          IList result = multiCriteria.List();
          IList<T> all = (from object o in (IList) result[0] select (T) o).ToList();
          count = (long)((IList)result[1])[0];
          return all;
      }
   }

Lớp NHibernateHelper

public sealed  class NHibernateHelper
    {
      private static ISessionFactory factory = null;
      static NHibernateHelper()
        {
            Configuration cfg = new Configuration();
            cfg.Configure();
            factory = cfg.BuildSessionFactory();
        }

        public static ISession CurrentSession
        {
            get
            {
                if(factory==null)
                {
                    var configuration = new Configuration();
                    configuration.Configure();
                    factory = configuration.BuildSessionFactory();
                }
                return factory.OpenSession();
            }
        }
        public static void CloseSession()
        {
            factory.Close();
        }
    }

Chú ý : đối với hệ quản trị Mysql không hỗ trợ MultiCriteria ta có thể viết lại lớp Paging như sau :

public abstract class Paging<T>
   {

    public IList<T> GetPagedData(int page, int pageSize, out long count, Order oder, params ICriterion[] criterion)
        {
           // IMultiCriteria multiCriteria = NHibernateHelper.CurrentSession.CreateMultiCriteria();
            ICriteria criteria1 = NHibernateHelper.CurrentSession.CreateCriteria(typeof(T));
            ICriteria criteria2 =
                NHibernateHelper.CurrentSession.CreateCriteria(typeof(T));//.SetProjection(Projections.RowCountInt64());
            foreach (var item in criterion)
            {
                criteria1.Add(item);
                criteria2.Add(item);
            }
            criteria2.SetProjection(Projections.RowCountInt64());
            criteria1.AddOrder(oder);
           // multiCriteria.Add(criteria1);
           // multiCriteria.Add(criteria2);
            criteria1.SetFirstResult(page * pageSize).SetMaxResults(pageSize);
            count = long.Parse(criteria2.List()[0].ToString());
            return   criteria1.List<T>();
           // IList result = multiCriteria.List();
           // IList<T> all = (from object o in (IList)result[0] select (T)o).ToList();
           // count = (long)((IList)result[1])[0];
           // return all;
        }
    }