Skip to content

Instantly share code, notes, and snippets.

@cosoria
Created October 11, 2017 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosoria/53be3341a0da610362482d74b604ff5d to your computer and use it in GitHub Desktop.
Save cosoria/53be3341a0da610362482d74b604ff5d to your computer and use it in GitHub Desktop.
Base class for Entity Framework Repositories
public abstract class EntityFrameworkRepository<TEntity> : Disposable, IRepository<TEntity> where TEntity : IEntity
{
protected readonly IEntityFrameworkUnitOfWork _uow;
protected readonly IEntityFrameworkContext _context;
protected EntityFrameworkRepository(IEntityFrameworkUnitOfWork uow)
{
_uow = uow;
_context = uow.Context;
}
public abstract TEntity Get(object key);
public abstract IEnumerable<TEntity> GetAll();
public abstract IEnumerable<TEntity> GetAllMatching(Expression<Func<TEntity, bool>> filter);
public void Add(TEntity entry)
{
_context.MarkAsAdded(entry);
}
public void Delete(object key)
{
var entry = Get(key);
_context.MarkAsDeleted(entry);
}
public void Update(TEntity entry)
{
_context.MarkAsModified(entry);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment