// Interface definition public interface IProductRepository { void Add(Product product); void Update(Product product); void Delete(int id); Product GetById(int id); IEnumerableGetAll(); }
// Implementation using in-memory collection public class InMemoryProductRepository : IProductRepository { private ListIn the first code example, we define the IProductRepository interface with methods for adding, updating, deleting, and retrieving product entities. In the second code example, we implement the IProductRepository interface using an in-memory collection of products. We initialize the collection with three sample products in the constructor, and then implement the methods of the interface using LINQ queries on the collection. The package library containing this interface and implementation may be part of a larger application or framework related to e-commerce or product management._products; public InMemoryProductRepository() { _products = new List { new Product { Id = 1, Name = "Product A", Price = 10.0m }, new Product { Id = 2, Name = "Product B", Price = 20.0m }, new Product { Id = 3, Name = "Product C", Price = 30.0m } }; } public void Add(Product product) { _products.Add(product); } public void Update(Product product) { var existingProduct = GetById(product.Id); if (existingProduct != null) { existingProduct.Name = product.Name; existingProduct.Price = product.Price; } } public void Delete(int id) { var existingProduct = GetById(id); if (existingProduct != null) { _products.Remove(existingProduct); } } public Product GetById(int id) { return _products.FirstOrDefault(p => p.Id == id); } public IEnumerable GetAll() { return _products; } }