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
- 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.
- 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.
- 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.
Top comments (0)