Last active
September 22, 2017 02:25
-
-
Save melvinlee/39bdfd2bfa15e4c0acd7081ef5614279 to your computer and use it in GitHub Desktop.
EF 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; | |
using System.Data.Entity; | |
using System.Data.Entity.Infrastructure; | |
using System.Linq; | |
public class GenericRepository<T> : IRepository<T>, IDisposable where T : class | |
{ | |
public GenericRepository(IDbContext context) | |
{ | |
if (context == null) | |
{ | |
throw new ArgumentException("An instance of DbContext is " + | |
"required to use this repository.", "context"); | |
} | |
Context = context; | |
DbSet = Context.Set<T>(); | |
} | |
protected IDbSet<T> DbSet { get; set; } | |
protected IDbContext Context { get; set; } | |
public virtual void Dispose() | |
{ | |
} | |
public virtual IQueryable<T> GetAll() | |
{ | |
return DbSet; | |
} | |
public virtual IQueryable<T> GetAll(string include) | |
{ | |
return DbSet.Include(include); | |
} | |
public virtual T GetById(dynamic id) | |
{ | |
return DbSet.Find(id); | |
} | |
public virtual void Add(T entity) | |
{ | |
var entry = Context.Entry(entity); | |
if (entry.State != EntityState.Detached) | |
{ | |
entry.State = EntityState.Added; | |
} | |
else | |
{ | |
DbSet.Add(entity); | |
} | |
Context.SaveChanges(); | |
} | |
public virtual void Update(T entity) | |
{ | |
DbEntityEntry<T> entry = Context.Entry(entity); | |
entry.State = EntityState.Modified; | |
Context.SaveChanges(); | |
} | |
public virtual void Delete(T entity) | |
{ | |
DbEntityEntry<T> entry = Context.Entry(entity); | |
if (entry.State != EntityState.Deleted) | |
{ | |
entry.State = EntityState.Deleted; | |
} | |
else | |
{ | |
DbSet.Attach(entity); | |
DbSet.Remove(entity); | |
} | |
Context.SaveChanges(); | |
} | |
public virtual void Delete(dynamic id) | |
{ | |
T entity = GetById(id); | |
if (entity != null) | |
{ | |
Delete(entity); | |
} | |
} | |
} |
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.Linq; | |
public interface IRepository<T> where T : class | |
{ | |
IQueryable<T> GetAll(); | |
IQueryable<T> GetAll(string include); | |
T GetById(dynamic id); | |
void Add(T entity); | |
void Update(T entity); | |
void Delete(T entity); | |
void Delete(dynamic id); | |
} |
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
public class RepositoryImp : GenericRepository<Model> | |
{ | |
public IncomingCallsRepository(IDbContext context) | |
: base(context) | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment