Skip to content

Instantly share code, notes, and snippets.

@efeozyer
Last active April 5, 2019 20:29
Show Gist options
  • Save efeozyer/98b87852c5722a18c1c32210cdde75d5 to your computer and use it in GitHub Desktop.
Save efeozyer/98b87852c5722a18c1c32210cdde75d5 to your computer and use it in GitHub Desktop.
public class ProductRepository : IProductRepository
{
public ProductRepository(DbContext dbContenxt) {
_table = dbContext.Set<Product>();
_dbContext = dbContext;
}
public bool Create(Product product) {
_table.Add(product);
return Save();
}
public bool Delete(int productId) {
var product = _table.SingleOrDefault(x => x.Id == productId);
_table.Delete(product);
return Save();
}
public IEnumerable<Product> GetAll()
{
return _table.ToList();
}
public void DeleteAll()
{
_table.RemoveRange(_table);
Save();
}
private bool Save() {
try {
_dbContext.SaveChanges();
}
catch {
return false;
}
return true;
}
private readonly DbSet<Product> _table;
private readonly DbContext _dbContext;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment