using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using NHibernate; using NHibernate.Linq; using Taloyhtio.GeneralSSO.Server.CodeFiles.Entities; using Taloyhtio.GeneralSSO.Server.CodeFiles.Infrastructure.DataAccess; namespace Taloyhtio.GeneralSSO.Server.CodeFiles.Repositories.Impl { public abstract class RepositoryBase : IRepository where TEntity : PersistentObject { public virtual TEntity GetById(TKey id) { var session = GetSession(); return session.Linq().FirstOrDefault(e => ((object)e.Id).Equals(id)); } protected abstract ISession GetSession(); public virtual void Save(TEntity entity) { this.GetSession().SaveOrUpdate(entity); } public virtual IEnumerable GetAll() { return this.getAll(); } public IEnumerable GetAllOrderBy(Expression> selector) { return this.GetAllOrderBy(selector, true); } private IQueryable getAll() { var session = this.GetSession(); var query = session.Linq().Select(entity => entity); return query; } public IEnumerable GetAllOrderBy(Expression> selector, bool asc) { var query = this.getAll(); if (asc) { return query.OrderBy(selector); } return query.OrderByDescending(selector); } public virtual void Delete(TEntity entity) { this.GetSession().Delete(entity); } public bool Exist(TKey key) { return this.GetById(key) != null; } } }