66 lines
1.9 KiB
C#
66 lines
1.9 KiB
C#
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<TEntity, TKey> : IRepository<TEntity, TKey> where TEntity : PersistentObject<TKey>
|
|
{
|
|
public virtual TEntity GetById(TKey id)
|
|
{
|
|
var session = GetSession();
|
|
return session.Linq<TEntity>().FirstOrDefault(e => ((object)e.Id).Equals(id));
|
|
}
|
|
|
|
protected abstract ISession GetSession();
|
|
|
|
public virtual void Save(TEntity entity)
|
|
{
|
|
this.GetSession().SaveOrUpdate(entity);
|
|
}
|
|
|
|
public virtual IEnumerable<TEntity> GetAll()
|
|
{
|
|
return this.getAll();
|
|
}
|
|
|
|
public IEnumerable<TEntity> GetAllOrderBy<TProperty>(Expression<Func<TEntity, TProperty>> selector)
|
|
{
|
|
return this.GetAllOrderBy(selector, true);
|
|
}
|
|
|
|
private IQueryable<TEntity> getAll()
|
|
{
|
|
var session = this.GetSession();
|
|
var query = session.Linq<TEntity>().Select(entity => entity);
|
|
return query;
|
|
}
|
|
|
|
public IEnumerable<TEntity> GetAllOrderBy<TProperty>(Expression<Func<TEntity, TProperty>> 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;
|
|
}
|
|
}
|
|
|
|
}
|