Engineering Core
ISB Vietnam's skilled software engineers deliver high-quality applications, leveraging their extensive experience in developing financial tools, business management systems, medical technology, and mobile/web platforms.

The Repository Pattern is a design pattern used to abstract data access logic, providing a clean separation between the business logic and data access layers in an application. By implementing this pattern, you encapsulate data access logic in a repository class that acts as an intermediary between the business logic and the data source (like a database, web service, or API).

 

In C#, the Repository Pattern helps to centralize data access logic and makes it easier to swap out different data sources (e.g., switching from an SQL database to a NoSQL database) without affecting the rest of the application. It also simplifies testing by allowing mock repositories to be used instead of actual data sources.

1. Components of the Repository Pattern

Entity: A class that represents the model of the data being stored in the database (e.g., Company, Customer..).

Repository Interface: Defines the contract for how data access operations are handled.

Repository Implementation: Implements the data access logic, such as querying the database.

Business Logic: The service layer or business rules of the application.

2. Example of the Repository Pattern in C#

Step 1: Define the Entity
Let's start with an entity class representing a Customer.
public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
}
Step 2: Create the Repository Interface
The repository interface defines the contract that all repository classes must implement. It usually includes methods for basic CRUD (Create, Read, Update, Delete) operations.
public interface ICustomerRepository
{
    IEnumerable GetAll();
    Product GetById(int id);
    void Add(Customer customer);
    void Update(Customer customer );
    void Delete(int id);
}
Step 3: Implement the Repository
In this step, you create the concrete repository class that implements the interface. Here’s an example using Entity Framework as the data access technology.
public class CustomerRepository : ICustomerRepository
{
    private readonly EntitiesDbContest _context;

    public CustomerRepository(EntitiesDbContest context)
    {
        _context = context;
    }

    public IEnumerable GetAll()
    {
        return _context.Customers.ToList();
    }

    public Customer GetById(int id)
    {
        return _context.Customers.Find(id);
    }

    public void Add(Customer customer)
    {
        _context.Customers.Add(customer);
        _context.SaveChanges();
    }

    public void Update(Customer customer)
    {
        _context.Customers.Update(customer);
        _context.SaveChanges();
    }

    public void Delete(int id)
    {
        var Customer = _context.Customers.Find(id);
        if (Customer != null)
        {
            _context.Customers.Remove(Customer);
            _context.SaveChanges();
        }
    }
}
Step 4: Using the Repository:
public class ProductService
{
    private readonly IProductRepository _productRepository;

    public ProductService(IProductRepository productRepository)
    {
        _productRepository = productRepository;
    }

    public IEnumerable GetProducts()
    {
        return _productRepository.GetAll();
    }

    public void AddProduct(Product product)
    {
        _productRepository.Add(product);
    }

    // Additional service methods can be added as needed
}

3. Benefits of the Repository Pattern

Ease of Testing: Interfaces allow for easy mocking, making unit testing more straightforward. Code Reusability: Common data access logic is centralized in the repository. Maintenance: Changes in data access logic (e.g., switching from SQL Server to MongoDB) require changes in only one place. Support for Multiple Data Sources: Different implementations can be created for different data sources while maintaining the same interface.

Conclusion

The Repository Pattern is an effective way to decouple business logic from data access in C# applications. It makes the code more maintainable, testable, and scalable. By abstracting the data access layer, the repository pattern promotes cleaner and more organized architecture, especially when working with complex systems where data access logic can become tangled with business logic. In modern applications, especially those built with ASP.NET Core, using the Repository Pattern along with Dependency Injection and Entity Framework results in a robust and flexible architecture that is easier to manage in the long term.

Cover image from freepik.com

Written by
Author Avatar
Engineering Core
ISB Vietnam's skilled software engineers deliver high-quality applications, leveraging their extensive experience in developing financial tools, business management systems, medical technology, and mobile/web platforms.

COMPANY PROFILE

Please check out our Company Profile.

Download

COMPANY PORTFOLIO

Explore my work!

Download

ASK ISB Vietnam ABOUT DEVELOPMENT

Let's talk about your project!

Contact US