What is the Repository Pattern?

The repository pattern aims to decouple the code that handles data persistence from business logic by storing the data access code in a separate layer. This way, application services do not need to know how data is being stored or retrieved. This separ…


This content originally appeared on DEV Community and was authored by Dylan

The repository pattern aims to decouple the code that handles data persistence from business logic by storing the data access code in a separate layer. This way, application services do not need to know how data is being stored or retrieved. This separation also allows changing the implementation of data access without affecting the upper layers.

The repository pattern generally involves three main components:

  • The repository interface
  • The repository implementation
  • The entity model
  1. Defining the Repository Interface The repository interface specifies the operations that can be performed on the entity. Generally, these operations include the CRUD operations (Create, Read, Update, and Delete).

Below is an example of a repository interface for the Product entity:

public interface IProductRepository
{
    IEnumerable<Product> GetAll();
    Product GetById(int id);
    void Add(Product product);
    void Update(Product product);
    void Delete(int id);
}

The interface defines the basic operations we can perform on the Product entity. For example, GetAll() returns a list of all products, GetById() returns a specific product by its ID, and the Add(), Update(), and Delete() functions handle creating, updating, and deleting products, respectively.

  1. Repository Implementation The repository implementation is responsible for providing the logic that interacts with the database. In this case, we will use Entity Framework to manage database access efficiently and avoid writing SQL queries manually.

Here is the repository implementation for Product:

public class ProductRepository : IProductRepository
{
    private readonly ApplicationDbContext _context;

    public ProductRepository(ApplicationDbContext context)
    {
        _context = context;
    }

    public IEnumerable<Product> GetAll()
    {
        return _context.Products.ToList();
    }

    public Product GetById(int id)
    {
        return _context.Products.Find(id);
    }

    public void Add(Product product)
    {
        _context.Products.Add(product);
        _context.SaveChanges();
    }

    public void Update(Product product)
    {
        _context.Products.Update(product);
        _context.SaveChanges();
    }

    public void Delete(int id)
    {
        var product = _context.Products.Find(id);
        if (product != null)
        {
            _context.Products.Remove(product);
            _context.SaveChanges();
        }
    }
}

In this example, the ProductRepository class implements the IProductRepository interface and uses a database context provided by Entity Framework, ApplicationDbContext, to perform CRUD operations. This reduces the need to write SQL queries directly, simplifying the interaction with the database.

  1. Entity Model The entity model represents the data that will be stored and manipulated. In this case, Product is our entity, corresponding to the Products table in the database.

Here is the entity model for Product:

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public string Description { get; set; }
}

Each instance of the Product class represents a record in the Products table. The properties of the class correspond to the columns of the table.

Using the Repository Pattern
With the repository interface, repository implementation, and entity model in place, we can now use the repository in our services or controllers to perform data access operations without worrying about the database implementation details.


This content originally appeared on DEV Community and was authored by Dylan


Print Share Comment Cite Upload Translate Updates
APA

Dylan | Sciencx (2024-10-01T18:08:59+00:00) What is the Repository Pattern?. Retrieved from https://www.scien.cx/2024/10/01/what-is-the-repository-pattern/

MLA
" » What is the Repository Pattern?." Dylan | Sciencx - Tuesday October 1, 2024, https://www.scien.cx/2024/10/01/what-is-the-repository-pattern/
HARVARD
Dylan | Sciencx Tuesday October 1, 2024 » What is the Repository Pattern?., viewed ,<https://www.scien.cx/2024/10/01/what-is-the-repository-pattern/>
VANCOUVER
Dylan | Sciencx - » What is the Repository Pattern?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/01/what-is-the-repository-pattern/
CHICAGO
" » What is the Repository Pattern?." Dylan | Sciencx - Accessed . https://www.scien.cx/2024/10/01/what-is-the-repository-pattern/
IEEE
" » What is the Repository Pattern?." Dylan | Sciencx [Online]. Available: https://www.scien.cx/2024/10/01/what-is-the-repository-pattern/. [Accessed: ]
rf:citation
» What is the Repository Pattern? | Dylan | Sciencx | https://www.scien.cx/2024/10/01/what-is-the-repository-pattern/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.