Skip to content

Instantly share code, notes, and snippets.

@KingsleyOmon-Edo
Forked from MikeLarned/Repository
Created October 22, 2018 03:12
Show Gist options
  • Save KingsleyOmon-Edo/413e6cbd2222e0713d42e801ca46b162 to your computer and use it in GitHub Desktop.
Save KingsleyOmon-Edo/413e6cbd2222e0713d42e801ca46b162 to your computer and use it in GitHub Desktop.
Repository
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using NHibernate;
using NHibernate.Linq;
namespace BMobile.Infrastructure.Entitys
{
public class Repository : IRepository
{
private readonly ISession _session;
public Repository(ISession session)
{
_session = session;
}
public T Get<T>(int id) where T : IEntity
{
return _session.Get<T>(id);
}
public void Save<T>(T entity) where T : IEntity
{
_session.SaveOrUpdate(entity);
}
public void Delete<T>(T entity) where T : IEntity
{
_session.Delete(entity);
}
public T Execute<T>(IQuery<T> query)
{
return query.Execute(_session);
}
public IQueryable<T> Query<T>(Expression<Func<T, bool>> predicate)
{
return _session.Query<T>().Where(predicate);
}
public IEnumerable<T> PagedQuery<T>(int startIndex, int count, Expression<Func<T, bool>> predicate)
{
var query = _session.Query<T>().Where(predicate);
query.Skip(startIndex).Take(count);
return query.ToList<T>();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using NHibernate;
using NHibernate.Linq;
namespace BMobile.Infrastructure.Entitys
{
public class Repository : IRepository
{
private readonly ISession _session;
public Repository(ISession session)
{
_session = session;
}
public T Get<T>(int id) where T : IEntity
{
return _session.Get<T>(id);
}
public void Save<T>(T entity) where T : IEntity
{
_session.SaveOrUpdate(entity);
}
public void Delete<T>(T entity) where T : IEntity
{
_session.Delete(entity);
}
public T Execute<T>(IQuery<T> query)
{
return query.Execute(_session);
}
public IQueryable<T> Query<T>(Expression<Func<T, bool>> predicate)
{
return _session.Query<T>().Where(predicate);
}
public IEnumerable<T> PagedQuery<T>(int startIndex, int count, Expression<Func<T, bool>> predicate)
{
var query = _session.Query<T>().Where(predicate);
query.Skip(startIndex).Take(count);
return query.ToList<T>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment