Is this okay?
public class BaseRepository<T> where T : class
{
private readonly DbContext _dbContext;
public BaseRepository(DbContext dbContext)
{
_dbContext = dbContext;
}
public T Get(Func<T, bool> predicate)
{
return GetAll(predicate).FirstOrDefault();
}
public IEnumerable<T> GetAll(Func<T, bool> predicate = null)
{
IEnumerable<T> result = _dbContext.Set<T>().AsEnumerable();
return (predicate == null) ? result : result.Where<T>(predicate);
}
public void Add(T entity)
{
_dbContext.Entry(entity).State = EntityState.Added;
}
public void Delete(Func<T, bool> predicate)
{
IEnumerable<T> entities = GetAll(predicate);
foreach (T entity in entities)
_dbContext.Entry(entity).State = EntityState.Deleted;
}
public void Delete(T entity)
{
_dbContext.Entry(entity).State = EntityState.Deleted;
}
public void Update(T entity)
{
_dbContext.Entry(entity).State = EntityState.Modified;
}
public async void Save()
{
await _dbContext.SaveChangesAsync();
}
public void Dispose()
{
if (_dbContext != null)
_dbContext.Dispose();
}
}
abstract repository
exists just to abstract an existing abstraction. I am coding much happier now that I removed it from my code :) \$\endgroup\$ – Felipe Miosso May 19 '14 at 14:28Func<T, bool>
forExpression<Func<T, bool>>
. Stop usingIEnumerable
if you don't know what it is vsIQueryable
. Just usevar
. \$\endgroup\$ – Aron May 19 '14 at 14:47