Skip to content

Instantly share code, notes, and snippets.

@dervalp
Created February 17, 2012 13:36
Show Gist options
  • Select an option

  • Save dervalp/1853475 to your computer and use it in GitHub Desktop.

Select an option

Save dervalp/1853475 to your computer and use it in GitHub Desktop.
Generic Repository
public class Repository<T> : IRepository<T> where T : class, IEntity
{
private readonly ISession _session;
private readonly IDalValidator<T> _dalValidator;
public Repository(ISession session, IDalValidator<T> dalValidator)
{
_session = session;
_dalValidator = dalValidator;
}
public void Save(T entity)
{
if (null == entity)
{
throw new ArgumentNullException("entity");
}
_dalValidator.ValidateSave(entity);
using (var tx = _session.BeginTransaction())
{
if (entity.Id != 0)
{
_session.Merge(entity);
}
else
{
_session.Save(entity);
}
tx.Commit();
}
}
public T Get(long id)
{
if (0 == id)
{
throw new ArgumentNullException("id");
}
using (var tx = _session.BeginTransaction())
{
var result = _session.Get<T>(id);
tx.Commit();
return result;
}
}
public T Get(Guid uniqueId)
{
if (Guid.Empty == uniqueId)
{
throw new ArgumentNullException("uniqueId");
}
using (var tx = _session.BeginTransaction())
{
var result = _session.CreateCriteria<T>().Add(Restrictions.Eq("UniqueId", uniqueId)).UniqueResult<T>();
tx.Commit();
return result;
}
}
public void SaveMany(IEnumerable<T> entities)
{
if (null == entities)
{
throw new ArgumentNullException("entities");
}
using (var tx = _session.BeginTransaction())
{
foreach (var entity in entities)
{
if (null != entity)
{
_dalValidator.ValidateSave(entity);
if (entity.Id != 0)
{
_session.Merge(entity);
}
else
{
_session.Save(entity);
}
}
}
tx.Commit();
}
}
public IEnumerable<T> GetMany(IEnumerable<long> ids)
{
if (null == ids)
{
throw new ArgumentNullException("ids");
}
var li = ids.ToList();
using (var tx = _session.BeginTransaction())
{
var result = _session.QueryOver<T>()
.WhereRestrictionOn(x => x.Id).IsIn(li)
.List<T>();
tx.Commit();
return result;
}
}
public IEnumerable<T> GetMany(IEnumerable<Guid> uniqueIds)
{
if (null == uniqueIds)
{
throw new ArgumentNullException("uniqueIds");
}
var li = uniqueIds.ToList();
using (var tx = _session.BeginTransaction())
{
var result = _session.QueryOver<T>().WhereRestrictionOn(x => x.UniqueId).IsIn(li)
.List<T>();
tx.Commit();
return result;
}
}
public IEnumerable<T> All()
{
using (var tx = _session.BeginTransaction())
{
var result = _session.CreateCriteria<T>().List<T>();
tx.Commit();
return result;
}
}
public void Delete(T entity)
{
if (null == entity) { throw new ArgumentNullException("entity"); }
using (var tx = _session.BeginTransaction())
{
_session.Delete(entity);
tx.Commit();
}
}
public void DeleteMany(IEnumerable<T> entities)
{
if (null == entities)
{
throw new ArgumentNullException("entities");
}
using (var tx = _session.BeginTransaction())
{
foreach (var entity in entities)
{
_session.Delete(entity);
}
tx.Commit();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment