Skip to content

Provides a base when implementing the repository pattern with EF Core.

Notifications You must be signed in to change notification settings

LebaneseDevelopers/LD.Patterns.AspNetCore.Repository

Repository files navigation

LD.Patterns.AspNetCore.Repository

Provides a base when implementing the repository pattern with EF Core.

Objectives

  • Quickly implement the pattern with little to no plumbing or boilerplate on your end.
  • Ease out unit testing by providing some kind of an in memory repository store.

To that end:

  • Have a base repository class that handles most of the plumbing (like implementing Add, Remove, Update, SaveChanges, Transactions, ...).
  • Have another base repository class that in memory implementations should inherit from. It should basically be an in memory store for entities that also provides things like ensuring (int, long) PKs get a proper incrementing value when entities are added.

The basics

IGenericRepository, GenericRepository, and InMemoryGenericRepository are provided for your (real) Repository classes to inherit from. They do most of the work.

A simple example

// This is your service.
public interface IRepository : IGenericRepository
{
    IQueryable<Blog> Blogs { get; }
}
// This is the real repository that stores entities in a database.
public class Repository : GenericRepository<ApplicationDbContext>, IRepository
{
    public Repository(ApplicationDbContext context) : base(context) { }

    IQueryable<Blog> Blogs => Context.Blogs;

    // Add, Remove, Update, SaveChangesAsync are all implemented in RepositoryCore.
}
// This is the in memory repository that will depend on InMemoryRepositoryCore to store entities in memory.
public class InMemoryRepository : InMemoryGenericRepository, IRepository
{
    // For() is a method on InMemoryRepositoryCore
    IQueryable<Blog> Blogs => For<Blog>();

    // Add, Remove, Update, SaveChangesAsync are all implemented in InMemoryRepositoryCore.
}

Note: The InMemoryRepository requires the PK property to be called "Id" for auto incrementing to work.

Nice improvements to have

  • Relationship fixups for the in memory store.

About

Provides a base when implementing the repository pattern with EF Core.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages