我想做这样的事情......
return GetSession()
.ToPagedList<Employee>(page, pageSize,
x=> x.SetFetchMode(DomainModelHelper.GetAssociationEntityNameAsPlural<Team>(), FetchMode.Eager));
但我不知道如何将其传递Func<ICriteria,ICriteria>
给ISession
or ICriteria
。
我有一个标准的分页扩展方法,这个扩展方法应该有一个重载,我可以在其中传递额外的 ICriteria 方法,以便我可以额外设置FetchMode
或其他东西。
扩展方法:
public static class CriteriaExtensions
{
public static PagedList<T> ToPagedList<T>(this ISession session, int page, int pageSize) where T : Entity
{
var totalCount = TotalCount<T>(session);
return new PagedList<T>(session.CreateCriteria<T>()
.SetFirstResult(pageSize * (page - 1))
.SetMaxResults(pageSize * page)
.Future<T>().ToList(), page, pageSize, totalCount);
}
public static PagedList<T> ToPagedList<T>(this ISession session, int page, int pageSize, Func<ICriteria, ICriteria> action) where T : Entity
{
var totalCount = TotalCount<T>(session);
...
}
private static int TotalCount<T>(ISession session) where T : Entity
{
return session.CreateCriteria<T>()
.SetProjection(Projections.RowCount())
.FutureValue<Int32>().Value;
}
}
编辑:
如果没有重载,它看起来像这样:
return GetSession()
.CreateCriteria<Employee>()
.SetFetchMode(DomainModelHelper.GetAssociationEntityNameAsPlural<Team>(), FetchMode.Eager)
.ToPagedList<Employee>(page, pageSize);
扩展方法:
public static class CriteriaExtensions
{
public static PagedList<T> ToPagedList<T>(this ICriteria criteria, int page, int pageSize) where T : Entity
{
var copiedCriteria = (ICriteria) criteria.Clone();
var totalCount = TotalCount(criteria);
return new PagedList<T>(copiedCriteria
.SetFirstResult(pageSize * (page - 1))
.SetMaxResults(pageSize * page)
.Future<T>().ToList(), page, pageSize, totalCount);
}
private static int TotalCount(ICriteria criteria)
{
return criteria
.SetProjection(Projections.RowCount())
.FutureValue<Int32>().Value;
}
}
这条线var copiedCriteria = (ICriteria) criteria.Clone();
在这里闻起来很臭,但我不知道如何改变它。
你会建议哪种方法?