Created
January 15, 2018 08:09
-
-
Save weiting-tw/42ba7e56261d6966bd33f43d8732bccc 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
using System; | |
using System.Collections.Generic; | |
using System.Collections.ObjectModel; | |
using System.Linq.Expressions; | |
using GSS.Core.Domain; | |
using NHibernate; | |
using Spring.Stereotype; | |
using X.PagedList; | |
namespace GSS.Data.NHibernate | |
{ | |
[Repository] | |
public abstract class NHibernateRepositoryBase<T> : IRepository<T>, ILambdaQuery<T>, IPagedQuery<T> where T : class | |
{ | |
/// <summary> | |
/// Session factory for sub-classes. | |
/// </summary> | |
protected ISessionFactory sessionFactory; | |
public NHibernateRepositoryBase(ISessionFactory sessionFactory) | |
{ | |
this.sessionFactory = sessionFactory; | |
} | |
/// <summary> | |
/// Get's the current active session. Will retrieve session as managed by the | |
/// Open Session In View module if enabled. | |
/// </summary> | |
protected ISession CurrentSession | |
{ | |
get { return sessionFactory.GetCurrentSession(); } | |
} | |
#region IRepository | |
public virtual T Add(T entity) | |
{ | |
return Get(CurrentSession.Save(entity)); | |
} | |
public virtual void Update(T entity) | |
{ | |
CurrentSession.SaveOrUpdate(entity); | |
} | |
public virtual void Remove(T entity) | |
{ | |
CurrentSession.Delete(entity); | |
} | |
#endregion | |
#region IReadOnlyRepository | |
public virtual int Count() | |
{ | |
return CurrentSession.QueryOver<T>().RowCount(); | |
} | |
public virtual T Get(object id) | |
{ | |
return CurrentSession.Get<T>(id); | |
} | |
public virtual IReadOnlyList<T> GetAll() | |
{ | |
return new ReadOnlyCollection<T>(CurrentSession.QueryOver<T>().List()); | |
} | |
#endregion | |
#region ILambdaQuery | |
public int Count(Expression<Func<T, bool>> predicate = null) | |
{ | |
var query = CurrentSession.QueryOver<T>(); | |
if (predicate != null) query.Where(predicate); | |
return query.RowCount(); | |
} | |
public virtual T FindOne(Expression<Func<T, bool>> predicate) | |
{ | |
return CurrentSession.QueryOver<T>() | |
.Where(predicate) | |
.SingleOrDefault<T>(); | |
} | |
public IReadOnlyList<T> FindAll( | |
Expression<Func<T, bool>> predicate = null, | |
Expression<Func<T, object>> orderBy = null, | |
bool descending = false) | |
{ | |
var query = CurrentSession.QueryOver<T>(); | |
if (predicate != null) query.Where(predicate); | |
if (orderBy != null) | |
{ | |
query = descending ? query.OrderBy(orderBy).Desc : query.OrderBy(orderBy).Asc; | |
} | |
return new ReadOnlyCollection<T>(query.List()); | |
} | |
#endregion | |
#region IPagedQuery | |
public virtual IPagedList<T> PagedGetAll(int skip = 0, int top = 10) | |
{ | |
if (top <= 0) top = 10; | |
var page = skip > 0 ? (int)Math.Ceiling(skip / (double)top) + 1 : 1; | |
return FindAll(page, top); | |
} | |
public virtual IPagedList<T> PagedQuery(Expression<Func<T, bool>> predicate, int skip = 0, int top = 10) | |
{ | |
if (top <= 0) top = 10; | |
var page = skip > 0 ? (int)Math.Ceiling(skip / (double)top) + 1 : 1; | |
return FindAll(page, top, predicate); | |
} | |
public IPagedList<T> PagedQuery( | |
Expression<Func<T, bool>> predicate, | |
Expression<Func<T, object>> orderBy, | |
bool descending = false, | |
int skip = 0, | |
int top = 10) | |
{ | |
if (top <= 0) top = 10; | |
var page = skip > 0 ? (int)Math.Ceiling(skip / (double)top) + 1 : 1; | |
return FindAll(page, top, predicate, orderBy, descending); | |
} | |
public IPagedList<T> FindAll( | |
int page = 1, | |
int pageSize = 10, | |
Expression<Func<T, bool>> predicate = null, | |
Expression<Func<T, object>> orderBy = null, | |
bool descending = false) | |
{ | |
if (page < 1) page = 1; | |
var query = CurrentSession.QueryOver<T>(); | |
if (predicate != null) query.Where(predicate); | |
var totalItemCount = query.RowCount(); | |
if (page > 1) query.Skip((page - 1) * pageSize); | |
if (pageSize > 0) query.Take(pageSize); | |
if (orderBy != null) | |
{ | |
query = descending ? query.OrderBy(orderBy).Desc : query.OrderBy(orderBy).Asc; | |
} | |
var subset = query.List(); | |
pageSize = pageSize > 0 | |
? pageSize | |
: totalItemCount > 0 | |
? totalItemCount | |
: 1; | |
return new StaticPagedList<T>(subset, page, pageSize, totalItemCount); | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment