Can anyone explain Repository design pattren
Loading
Can anyone explain Repository design pattren
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jaish MathewsRank #225 · 11K reputationPosted Dec 21, 2024, 11:11 AM EST
The Repository Design Pattern is used to abstract the data layer, making it easier to decouple the business logic and the data access logic. It acts as a mediator between the domain and the data mapping layers. The primary goal is to create a centralized data access logic that can be reused across the application.
Key Benefits
C# Example
Scenario
Let's create a simple example where we manage a list of products using the Repository Pattern.
Step 1: Define the Entity
public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } }Step 2: Define the Repository Interface
public interface IProductRepository { IEnumerable GetAllProducts(); Product GetProductById(int id); void AddProduct(Product product); void UpdateProduct(Product product); void DeleteProduct(int id); }Step 3: Implement the Repository
using System.Collections.Generic; using System.Linq; public class ProductRepository : IProductRepository { private readonly List _products = new List(); public IEnumerable GetAllProducts() { return _products; } public Product GetProductById(int id) { return _products.FirstOrDefault(p => p.Id == id); } public void AddProduct(Product product) { _products.Add(product); } public void UpdateProduct(Product product) { var existingProduct = GetProductById(product.Id); if (existingProduct != null) { existingProduct.Name = product.Name; existingProduct.Price = product.Price; } } public void DeleteProduct(int id) { var product = GetProductById(id); if (product != null) { _products.Remove(product); } } }Step 4: Use the Repository in a Service
public class ProductService { private readonly IProductRepository _productRepository; public ProductService(IProductRepository productRepository) { _productRepository = productRepository; } public void DisplayAllProducts() { var products = _productRepository.GetAllProducts(); foreach (var product in products) { Console.WriteLine($"ID: {product.Id}, Name: {product.Name}, Price: {product.Price}"); } } public void AddSampleProduct() { _productRepository.AddProduct(new Product { Id = 1, Name = "Laptop", Price = 75000 }); } }Step 5: Dependency Injection and Usage
class Program { static void Main(string[] args) { IProductRepository productRepository = new ProductRepository(); var productService = new ProductService(productRepository); productService.AddSampleProduct(); productService.DisplayAllProducts(); } }Real-Time References
Best Practices
This approach ensures modular, testable, and maintainable code for any application.
Jignesh KumarRank #91 · 22.9K reputationPosted Dec 21, 2024, 12:41 PM EST
Hello Kiran,
You can find good resources as below to work with generic repository patterns,
https://codewithmukesh.com/blog/repository-pattern-in-aspnet-core/
https://codingblast.com/entity-framework-core-generic-repository/