Last active
April 5, 2019 20:29
-
-
Save efeozyer/98b87852c5722a18c1c32210cdde75d5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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