Created
February 27, 2012 12:12
-
-
Save Tazer/1923345 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TheDefaultCategory : ICreateCriteria<Category> | |
{ | |
public DetachedCriteria GetCriteria() | |
{ | |
return DetachedCriteria.For<Category>().Add(Restrictions.Eq("Default", true)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public IEnumerable<T> Matching<T>(ICreateCriteria<T> query, params IAppendCriterion[] extraCriterias) | |
{ | |
var criteria = query.GetCriteria(); | |
foreach (var criterion in extraCriterias) | |
{ | |
criterion.Append(criteria); | |
} | |
return criteria.GetExecutableCriteria(_session).List<T>(); | |
} | |
public IPagedList<T> Matching<T>(ICreateCriteria<T> query, IPagedQuerySettings paging, params IAppendCriterion[] extraCriterias) | |
{ | |
var multiCriteria = _session.CreateMultiCriteria(); | |
var detachedCriteria = query.GetCriteria(); | |
foreach (var criterion in extraCriterias) | |
{ | |
criterion.Append(detachedCriteria); | |
} | |
var totalCountCriteria = CriteriaTransformer.Clone((DetachedCriteria) detachedCriteria); | |
//Ta bort alla ORDER BY | |
totalCountCriteria.ClearOrders(); | |
totalCountCriteria.SetProjection(Projections.RowCount()); | |
//Get the rows | |
multiCriteria.Add(totalCountCriteria); | |
if (paging != null) | |
{ | |
detachedCriteria.SetFirstResult(paging.GetFirstResult()).SetMaxResults(paging.PageSize); | |
} | |
//Get the actual result | |
multiCriteria.Add((ICriteria) detachedCriteria.GetExecutableCriteria(_session)); | |
var results = multiCriteria.List(); | |
int count = (int)((IList)results[0])[0]; | |
var entities = (IList)results[1]; | |
return new PagedList<T>(paging.Page, paging.PageSize, count, entities.Cast<T>()); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ICreateCriteria<T> : ICreateCriteria | |
{ } | |
public interface ICreateCriteria | |
{ | |
DetachedCriteria GetCriteria(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment