Skip to content

Instantly share code, notes, and snippets.

@miguelludert
Last active November 22, 2015 23:55
Show Gist options
  • Save miguelludert/9218548 to your computer and use it in GitHub Desktop.
Save miguelludert/9218548 to your computer and use it in GitHub Desktop.
Entity Framework reflection repositories
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
namespace ThinkingSites.Entity
{
public interface I_Repository<T>
{
T Find(params object[] keys);
IQueryable<T> Get();
T Add(T entity);
T Attach(T entity);
void Remove(T entity);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ThinkingSites.Entity
{
public interface I_RepositoryContext : IDisposable
{
T Find<T>(params object[] keys) where T : class;
I_Repository<T> GetRepo<T>() where T : class;
IQueryable<T> Get<T>() where T : class;
T Get<T>(int id) where T : class;
T Add<T>(T entity) where T : class;
IEnumerable<T> Add<T>(IEnumerable<T> entity) where T : class;
T Attach<T>(T entity) where T : class;
void Remove<T>(T entity) where T : class;
void Commit();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ThinkingSites.Entity
{
public interface I_RepositoryContextAsync : IDisposable
{
Task<T> Find<T>(params object[] keys) where T : class;
Task<I_Repository<T>> GetRepo<T>() where T : class;
Task<IQueryable<T>> Get<T>() where T : class;
Task<T> Get<T>(int id) where T : class;
Task<T> Add<T>(T entity) where T : class;
Task<IEnumerable<T>> Add<T>(IEnumerable<T> entity) where T : class;
Task<T Attach<T>>(T entity) where T : class;
Task Remove<T>(T entity) where T : class;
Task Commit();
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Reflection;
using System.Web;
namespace ThinkingSites.Entity
{
public class Repository<T> : I_Repository<T>
where T : class
{
private DbContext Context { get; set; }
private DbSet<T> Set { get; set; }
public RepositoryT(DbContext context)
{
Context = context;
Set = Context.Set<T>();
}
public T Find(params object[] keys)
{
return Set.Find(keys);
}
public IQueryable<T> Get()
{
return Set;
}
public T Add(T entity)
{
var dbEntity = this.Context.Entry(entity);
if (dbEntity.State == System.Data.Entity.EntityState.Detached)
{
Set.Add(entity);
}
else
{
dbEntity.State = System.Data.Entity.EntityState.Added;
}
return entity;
}
public void Remove(T entity)
{
var dbEntity = this.Context.Entry(entity);
if (dbEntity.State == System.Data.Entity.EntityState.Deleted)
{
Set.Attach(entity);
Set.Remove(entity);
}
else
{
dbEntity.State = System.Data.Entity.EntityState.Deleted;
}
}
public T Attach(T entity)
{
var dbEntity = this.Context.Entry(entity);
if (dbEntity.State != System.Data.Entity.EntityState.Detached)
{
Set.Attach(entity);
}
dbEntity.State = System.Data.Entity.EntityState.Modified;
return entity;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ThinkingSites.Entity
{
public class RepositoryContext<TDBContext> : I_RepositoryContext where TDBContext : System.Data.Entity.DbContext
{
private Lazy<TDBContext> dbContext = new Lazy<TDBContext>(() =>
{
var context = Activator.CreateInstance<TDBContext>();
context.Configuration.AutoDetectChangesEnabled = true;
context.Configuration.LazyLoadingEnabled = false;
return context;
});
private Dictionary<Type, object> _Repositories = new Dictionary<Type, object>();
public I_Repository<T> GetRepo<T>() where T : class
{
var type = typeof(T);
if (_Repositories.ContainsKey(type))
{
return (I_Repository<T>)_Repositories[type];
}
else
{
var repoType = typeof(Repository<>).MakeGenericType(type);
var result = (I_Repository<T>)Activator.CreateInstance(repoType, this.dbContext.Value);
_Repositories.Add(type,result);
return result;
}
}
//[System.Diagnostics.DebuggerHidden]
public void Commit()
{
this.dbContext.Value.SaveChanges();
}
protected virtual void Dispose(bool disposing)
{
if (disposing && this.dbContext != null && this.dbContext.IsValueCreated)
{
this.dbContext.Value.Dispose();
}
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
public T Find<T>(params object[] keys) where T : class
{
return this.GetRepo<T>().Find(keys);
}
public IQueryable<T> Get<T>() where T : class
{
return this.GetRepo<T>().Get();
}
public T Get<T>(int id) where T : class
{
return this.GetRepo<T>().Find(id);
}
public T Add<T>(T entity) where T : class
{
return this.GetRepo<T>().Add(entity);
}
public IEnumerable<T> Add<T>(IEnumerable<T> entities) where T : class
{
foreach (var e in entities)
{
this.GetRepo<T>().Add(e);
}
return entities;
}
public T Attach<T>(T entity) where T : class
{
return this.GetRepo<T>().Attach(entity);
}
public void Remove<T>(T entity) where T : class
{
this.GetRepo<T>().Remove(entity);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment