Created
February 12, 2016 20:04
-
-
Save morecchia/5326fc9de58ac95a4989 to your computer and use it in GitHub Desktop.
A generic Entity Framework Repository
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Data.Entity; | |
using System.Linq; | |
namespace MyProject.Repositories | |
{ | |
public class EfRepository<T> : IRepository<T> | |
where T : class | |
{ | |
protected DbContext Context; | |
protected readonly bool ShareContext; | |
public EfRepository(DbContext context) : this(context, false) { } | |
public EfRepository(DbContext context, bool sharedContext) | |
{ | |
Context = context; | |
ShareContext = sharedContext; | |
} | |
protected DbSet<T> DbSet | |
{ | |
get | |
{ | |
return Context.Set<T>(); | |
} | |
} | |
public IQueryable<T> All() | |
{ | |
return DbSet.AsQueryable(); | |
} | |
public bool Any(System.Linq.Expressions.Expression<Func<T, bool>> predicate) | |
{ | |
return DbSet.Any(predicate); | |
} | |
public int Count | |
{ | |
get { return DbSet.Count(); } | |
} | |
public T Create(T t) | |
{ | |
DbSet.Add(t); | |
try | |
{ | |
Context.SaveChanges(); | |
} | |
catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) | |
{ | |
Exception raise = dbEx; | |
foreach (var validationErrors in dbEx.EntityValidationErrors) | |
{ | |
foreach (var validationError in validationErrors.ValidationErrors) | |
{ | |
string message = string.Format("{0}:{1}", | |
validationErrors.Entry.Entity.ToString(), | |
validationError.ErrorMessage); | |
// raise a new exception nesting | |
// the current instance as InnerException | |
raise = new InvalidOperationException(message, raise); | |
} | |
} | |
throw raise; | |
} | |
return t; | |
} | |
public int Delete(T t) | |
{ | |
DbSet.Remove(t); | |
if (!ShareContext) | |
{ | |
return Context.SaveChanges(); | |
} | |
return 0; | |
} | |
public int Delete(System.Linq.Expressions.Expression<Func<T, bool>> predicate) | |
{ | |
var records = FindAll(predicate); | |
foreach (var record in records) | |
{ | |
DbSet.Remove(record); | |
} | |
if (!ShareContext) | |
{ | |
return Context.SaveChanges(); | |
} | |
return 0; | |
} | |
public T Find(params object[] keys) | |
{ | |
return DbSet.Find(keys); | |
} | |
public T Find(System.Linq.Expressions.Expression<Func<T, bool>> predicate) | |
{ | |
return DbSet.SingleOrDefault(predicate); | |
} | |
public IQueryable<T> FindAll(System.Linq.Expressions.Expression<Func<T, bool>> predicate) | |
{ | |
return DbSet.Where(predicate).AsQueryable(); | |
} | |
public IQueryable<T> FindAll(System.Linq.Expressions.Expression<Func<T, bool>> predicate, int index, int size) | |
{ | |
var skip = index * size; | |
IQueryable<T> query = DbSet; | |
if (predicate != null) | |
{ | |
query = query.Where(predicate); | |
} | |
if (skip != 0) | |
{ | |
query = query.Skip(skip); | |
} | |
return query.Take(size).AsQueryable(); | |
} | |
public int Update(T t) | |
{ | |
var entry = Context.Entry(t); | |
DbSet.Attach(t); | |
entry.State = EntityState.Modified; | |
if (!ShareContext) | |
{ | |
return Context.SaveChanges(); | |
} | |
return 0; | |
} | |
public void Dispose() | |
{ | |
if (!ShareContext && Context != null) | |
{ | |
try | |
{ | |
Context.Dispose(); | |
} | |
catch { } | |
} | |
} | |
} | |
public class MyRepository : EfRepository<MyType>, IMyRepository | |
{ | |
public MyRepository(DbContext context, bool sharedContext) : base(context, sharedContext) { } | |
public IQueryable<MyType> GetAll() | |
{ | |
return FindAll(a => a.Id > 0); | |
} | |
public MyType GetById(int id) | |
{ | |
return Find(a => a.Id == id); | |
} | |
} | |
public interface IMyRepository : IRepository<MyType> | |
{ | |
IQueryable<MyType> GetAll(); | |
MyType GetById(int id); | |
} | |
public interface IRepository<T> : IDisposable where T : class | |
{ | |
IQueryable<T> All(); | |
bool Any(Expression<Func<T, bool>> predicate); | |
int Count { get; } | |
T Create(T t); | |
int Delete(T t); | |
int Delete(Expression<Func<T, bool>> predicate); | |
T Find(params object[] keys); | |
T Find(Expression<Func<T, bool>> predicate); | |
IQueryable<T> FindAll(Expression<Func<T, bool>> predicate); | |
IQueryable<T> FindAll(Expression<Func<T, bool>> predicate, int index, int size); | |
int Update(T t); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment