Implementing Repository Pattern in ASP.NET Core

Implementing Repository Pattern in ASP.NET Core

05 Dec 2024
Beginner
673 Views
9 min read
Learn with an interactive course and practical hands-on labs

ASP.NET MVC with Web API Foundations Course - Free

Repository Pattern in ASP.NET Core

In software development, managing data access can become complex quickly. One way to simplify and organize this aspect of your application is to use the Repository Pattern. This design helps manage and maintain data access.

In this ASP.NET Core Tutorial, we will talk about the Repository Pattern in ASP.NET Core, its functioning, and its importance. If in case you need more detailed information about other ASP.NET-related topics, mddake sure you have enrolled in our ASP.NET Core Certification Training.

What is a Repository Pattern?

The Repository Pattern is a design, that acts like a middleman between the application and data source. The main aim is to have a cleaner code that is easy to maintain and reuse both logic and data access.

Repository Pattern Interface

To implement the Repository Pattern, we first define an interface that outlines the methods for accessing data. This interface will represent the contract that any repository class must fulfill. A simple example is shown below: 

public interface IRepository<T>
{
    IEnumerable<T> GetAll();
    T GetById(int id);
    void Add(T entity);
    void Update(T entity);
    void Delete(int id);
}

Any repository class must carry out the basic CRUD operations (Create, Read, Update, Delete) defined by this interface.

Read More: ASP.NET Core And Entity Framework Core CRUD Operations

Core Principles of the Repository Pattern

The core principles of the Repository Pattern are as follows:
  1. Abstraction- Separates the data access logic from the business logic.
  2. Encapsulation-Encapsulates the logic required to access data sources.
  3. Testability-Makes unit testing easier by allowing you to mock data access layers.
  4. DRY (Don't Repeat Yourself)-Reduces code duplication by centralizing data access logic.

Repository Pattern - SQL Server Implementation

Let's dive into an example of implementing the Repository Pattern with SQL Server in an ASP.NET application. We will start by creating our data models.

1.) Creating Models

First, we define our data models that will represent the database tables. For example, let's create a 'Product' model:
public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

2.) Context Class and the Database Connection

Next, we create a context class that represents a session with the database. This class derives from DbContext and includes DbSet properties for each model:
public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions options) : base(options) { }

    public DbSet Products { get; set; }
}

3.) Repository Pattern Logic

With our models and context in place, we can now implement the repository logic. We start by creating a repository class that implements the IRepository interface:
public class Repository<T> : IRepository<T> where T : class
{
    private readonly ApplicationDbContext _context;
    private readonly DbSet<T> _dbSet;

    public Repository(ApplicationDbContext context)
    {
        _context = context;
        _dbSet = _context.Set<T>();
    }

    public IEnumerable<T> GetAll()
    {
        return _dbSet.ToList();
    }

    public T GetById(int id)
    {
        return _dbSet.Find(id);
    }

    public void Add(T entity)
    {
        _dbSet.Add(entity);
        _context.SaveChanges();
    }

    public void Update(T entity)
    {
        _dbSet.Attach(entity);
        _context.Entry(entity).State = EntityState.Modified;
        _context.SaveChanges();
    }

    public void Delete(int id)
    {
        var entity = _dbSet.Find(id);
        _dbSet.Remove(entity);
        _context.SaveChanges();
    }
}

4.) Repository User Classes

Now, let's see how we can use this repository in our application. For example, in a controller:
public class ProductsController : Controller
{
    private readonly IRepository<Product> _productRepository;

    public ProductsController(IRepository<Product> productRepository)
    {
        _productRepository = productRepository;
    }

    public IActionResult Index()
    {
        var products = _productRepository.GetAll();
        return View(products);
    }

    // Other action methods for Create, Edit, Delete, etc.
}

5.) Creating a Repository Wrapper

To further enhance flexibility, we can create a repository wrapper that consolidates multiple repositories. This wrapper can manage instances of different repositories and simplify their usage.
public interface IRepositoryWrapper
{
    IRepository<Product> Product { get; }
    // Add other repositories here
    void Save();
}

public class RepositoryWrapper : IRepositoryWrapper
{
    private readonly ApplicationDbContext _context;
    private IRepository<Product> _productRepository;

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

    public IRepository<Product> Product
    {
        get
        {
            if (_productRepository == null)
            {
                _productRepository = new Repository<Product>(_context);
            }
            return _productRepository;
        }
    }

    public void Save()
    {
        _context.SaveChanges();
    }
}

Benefits of Repository Pattern

The Repository Pattern offers several benefits, including:
  • Code Reusability- By centralizing data access logic, you can reuse the same repository methods across different parts of your application.
  • Ease of Testing—The abstraction allows you to mock the data layer easily, making unit tests more straightforward and reliable.
  • Separation of Concerns- By keeping the data access logic separate from the business logic, the codebase is cleaner and more organized.
  • Consistency- Guarantees that all data accesses across an application exhibit consistent patterns, reducing mistakes and promoting dependability.
Conclusion
This article talks about the Repository Pattern in ASP.NET Core and why developers find it useful for managing their data access logic, making their code clean and performance-driven thus making development easier. Check out our ASP.NET Core Course to get more insights on ASP.NET Core.

Read More: ASP.NET Core Interview Questions and Answers

FAQs

Repository Pattern in ASP.NET Core is a design pattern that enables the usage of CRUD operations to separate the data logic from the business logic

They are the design patterns used for separating business logic from data access logic, enabling the data manipulation and retrieval in a single place.

Repository types are the different ways how we can structure the repositories and use them in an application. Common types are:
  • Generic repositories
  • Specific repositories
  • Composite repositories
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this