Created
July 29, 2013 19:10
-
-
Save danielkillyevo/6106876 to your computer and use it in GitHub Desktop.
Generic repository pattern + unit of work
This file contains 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 abstract class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class | |
{ | |
protected readonly DbContext _context; | |
protected readonly DbSet<TEntity> _dbSet; | |
protected BaseRepository(DbContext context) | |
{ | |
_context = context; | |
_dbSet = context.Set<TEntity>(); | |
} | |
public TEntity Get(int Id) | |
{ | |
return _dbSet.Find(Id); | |
} | |
public IQueryable<TEntity> Get(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, Type[] includePropertyTypes = null) | |
{ | |
IQueryable<TEntity> query = _dbSet; | |
if (filter != null) | |
query = query.Where(filter); | |
//note: can be here | |
if (includePropertyTypes != null) | |
{ | |
IEnumerable<string> includePropertyNames = includePropertyTypes.Select(t => t.Name); | |
foreach (var propertyName in includePropertyNames) | |
query.Include(propertyName); | |
} | |
if (orderBy != null) | |
query = orderBy(query); | |
return query; | |
} | |
public void Add(TEntity entity) | |
{ | |
_dbSet.Add(entity); | |
} | |
public void Update(TEntity entity) | |
{ | |
_dbSet.Attach(entity); | |
_context.Entry(entity).State = EntityState.Modified; | |
} | |
public void Remove(TEntity entity) | |
{ | |
if (_context.Entry(entity).State == EntityState.Detached) | |
_dbSet.Attach(entity); | |
_dbSet.Remove(entity); | |
} | |
} |
This file contains 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 EntityRepository : BaseRepository<Blog>, IEntityRepository | |
{ | |
public EntityRepository(DbContext context) | |
: base(context) | |
{ | |
} | |
} |
This file contains 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 interface IRepository<TEntity> where TEntity : class | |
{ | |
TEntity Get(int Id); | |
IQueryable<TEntity> Get(Expression<Func<TEntity,bool>> filter = null, Func<IQueryable<TEntity>,IOrderedQueryable<TEntity>> orderBy = null, Type[] includePropertyTypes = null); | |
void Add(TEntity entity); | |
void Update(TEntity entity); | |
void Remove(TEntity entity); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment