Good day,
I'm using repository pattern. But someone advised me to use include Unit of Work. I read a lot of articles and honestly I found a docs that are too complicated to understand.
Supposing I have a non-generic repository.
// My Interface
public interface IProductRepository{
IQueryable Products();
IQueryable ProductById(int id);
void InsertProduct(Product product);
void UpdateProduct(Product product);
void DeleteProductById(int id);
}
// Abstract Implementation
public class ProductRepository : IProductRepository{
private readonly MyDbContext context;
public ProductRepository(MyDbContext context){
this.context= context;
}
public IQueryable Products(){
context.Product();
}
public IQueryable ProductById(int id){
context.Product().Where(prod=>prod.Id == id);
}
public void InsertProduct(Product product){
context.Product.Add(product);
context.SaveChanges();
}
public void UpdateProduct(Product product){
context.Product.Update(product);
context.SaveChanges();
}
public void DeleteProductById(int id){
var product = ProductById(id);
context.Product.Remove(product);
context.SaveChanges();
}
}
My question is, how can I use Unit of Work here? Can you please show me a code below. It will be so much helpful for me to understand.