Mineplex2018-withcommit/Website/LOC.Core/Data/Repository.cs

127 lines
3.9 KiB
C#
Raw Normal View History

2013-08-27 17:14:08 +02:00
namespace LOC.Core.Data
{
using System;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Validation;
using System.Linq;
using System.Linq.Expressions;
public class Repository<TContext> : IRepository
where TContext : DbContext
{
public Repository(TContext context)
{
Context = context;
}
public DbContext Context { get; private set; }
public virtual TEntity Add<TEntity>(TEntity entity) where TEntity : class
{
return Context.Set<TEntity>().Add(entity);
}
public bool Any<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class
{
return Context.Set<TEntity>().AsQueryable().Any(predicate);
}
public bool Any<TEntity>() where TEntity : class
{
return Context.Set<TEntity>().Any();
}
public void AddOrEdit<TEntity>(TEntity entity) where TEntity : class
{
var tracked = Context.Set<TEntity>().Find(Context.KeyValuesFor(entity));
if (tracked != null)
{
Context.Entry(tracked).CurrentValues.SetValues(entity);
}
else
{
Context.Set<TEntity>().Add(entity);
}
}
public void CommitChanges()
{
const string PREFIX = "CommitChanges() failed because: ";
try
{
Context.SaveChanges();
}
catch (DbEntityValidationException ex)
{
var l = (from err in ex.EntityValidationErrors from ve in err.ValidationErrors select ve.ErrorMessage).ToList();
throw new ApplicationException(PREFIX + String.Join("; ", l), ex); // contains the human-readable validation exception
}
catch (DbUpdateException e)
{
if (e.InnerException != null && e.InnerException.InnerException != null && !String.IsNullOrEmpty(e.InnerException.InnerException.Message))
{
throw new ApplicationException(PREFIX + String.Join("; ", e.InnerException.InnerException.Message), e); // contains the reason
}
throw;
}
}
public virtual void Delete<TEntity>(TEntity entity) where TEntity : class
{
Context.Set<TEntity>().Attach(entity);
Context.Set<TEntity>().Remove(entity);
}
public virtual void Edit<TEntity>(TEntity entity) where TEntity : class
{
Context.Entry(entity).State = EntityState.Modified;
}
public virtual void Attach<TEntity>(TEntity entity) where TEntity : class
{
Context.Set<TEntity>().Attach(entity);
}
public virtual TEntity GetByKeyValues<TEntity>(params object[] keyValues) where TEntity : class
{
var entity = Context.Set<TEntity>().Find(keyValues);
entity.LoadNavigationProperties(Context);
return entity;
}
public IQueryable<TEntity> Where<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class
{
var query = Context.Set<TEntity>().AsQueryable().Where(predicate);
return query;
}
public IQueryable<TEntity> GetAll<TEntity>() where TEntity : class
{
var query = Context.Set<TEntity>().AsQueryable();
return query;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (disposing)
{
var context = Context;
Context = null;
if (context != null)
{
context.Dispose();
}
}
}
}
}