I have my own repository that is as follows. However this does not take into account some of the new features such as the range features. Does anyone have a repository that
You don't need a generic repository. DbContext already is a generic repository. Try this out:
public class EntityDbContext : DbContext, IWriteEntities
{
public IQueryable EagerLoad(IQueryable query,
Expression> expression)
{
// Include will eager load data into the query
if (query != null && expression != null)
query = query.Include(expression);
return query;
}
public IQueryable Query()
{
// AsNoTracking returns entities that are not attached to the DbContext
return Set().AsNoTracking();
}
public TEntity Get(object firstKeyValue, params object[] otherKeyValues)
{
if (firstKeyValue == null) throw new ArgumentNullException("firstKeyValue");
var keyValues = new List
... and the interfaces are merely a formality if you need to separate UoW from queries from commands:
public interface IUnitOfWork
{
int SaveChanges();
Task SaveChangesAsync();
Task DiscardChangesAsync();
void DiscardChanges();
}
public interface IReadEntities
{
IQueryable Query();
IQueryable EagerLoad(IQueryable query,
Expression> expression);
}
public interface IWriteEntities : IUnitOfWork, IReadEntities
{
TEntity Get(object firstKeyValue, params object[] otherKeyValues);
Task GetAsync(object firstKeyValue,
params object[] otherKeyValues);
IQueryable Get();
void Create(TEntity entity);
void Delete(TEntity entity);
void Update(TEntity entity);
void Reload(TEntity entity);
Task ReloadAsync(TEntity entity);
}
With this, your interface doesn't need to be generic because the methods are generic.
private readonly IWriteEntities _entities;
...
_entities.Get(keyA);
await _entities.GetAsync(keyB);
_entities.Get.Where(...
var results = await _entities.Query().SingleOrDefaultAsync(...
etc. You just saved 3 unnecessary generic repository dependencies in the code above. One interface can handle all 4 of the entitiy types.