DanielCunha.Toolkit.RepositoryPattern 1.0.1

Install-Package DanielCunha.Toolkit.RepositoryPattern -Version 1.0.1
dotnet add package DanielCunha.Toolkit.RepositoryPattern --version 1.0.1
<PackageReference Include="DanielCunha.Toolkit.RepositoryPattern" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DanielCunha.Toolkit.RepositoryPattern --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: DanielCunha.Toolkit.RepositoryPattern, 1.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install DanielCunha.Toolkit.RepositoryPattern as a Cake Addin
#addin nuget:?package=DanielCunha.Toolkit.RepositoryPattern&version=1.0.1

// Install DanielCunha.Toolkit.RepositoryPattern as a Cake Tool
#tool nuget:?package=DanielCunha.Toolkit.RepositoryPattern&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Repository Pattern

This package contains classes and interfaces I frequently use in my projects for working with repository pattern. Read me more about repository pattern in this article.

Motivation

Create a reusable project which contains all the basics I need to implement repository pattern without relying on copying the classes and interfaces every time.

Installation

Install NuGet package DanielCunha.Toolkit.RepositoryPattern or download repo and compile it manually so you can reference it from your target project.

Also, install or clone DanielCunha.Toolkit.RepositoryPattern.EntityFrameworkCore in case you're going to work with Entity Framework Core because it already contains a class which implements IRepository interface.

Usage

These packages are very ease of use. They just contain one interface and one class. You'll find an interface called IRepository in the DanielCunha.Toolkit.RepositoryPattern NuGet package.

The IRepository interface basically contains a couple of commonly used LINQ methods which your repository will inherit. You may implement your own base repository just inheriting from this interface.

public abstract class RepositoryBase<TEntity> : IRepository<TEntity> where TEntity : class
{
    protected readonly DbContext Context;

    protected RepositoryBase(DbContext context) 
    {
        Context = context;
    }

    // YOUR IMPLEMENTATION
}

In this case I'm using Entity Framework Core, but fell free to use whatever ORM you want to.

Last but not least, in the package DanielCunha.Toolkit.RepositoryPattern.EntityFrameworkCore you'll find the RepositoryBase class. It's basically a class which already implements all the methods from IRepository using Entity Framework Core.

When implementing your repositories, just create an interface inheriting from IRepository, add some methods and then create a class which inherits from RepositoryBase and your interface. This is a sample:

// IRepository receives the type of your entity because it's generic
public interface IAuthorRepository : IRepository<Author>
{
    Task<IEnumerable<Author>> FindActiveAuthorsAsync();
}

// RepositoryBase receives the entity and the type of your DbContext, because it will be accessible
// from a protected readonly property in case you want to use it in your implementations
internal class AuthorRepository : RepositoryBase<Author, DataContext>, IAuthorRepository
{
    public AuthorRepository(DataContext context) : base(context)
    {
        
    }

    public async Task<IEnumerable<Author>> FindActiveAuthorsAsync()
    {
        return await Context.Authors
            .Where(x => x.Active)
            .ToListAsync();
    }
}

After that, all you have to do is to add this repository to your container, or to your Unit of Work in case your using one. Otherwise, you can still just instantiates the repository passing your DbContext. It really depends on your call.

In case there were any doubts, there is a sample project in this repository which use this packages.

Contribute

Fell free to clone this repository and modify the projects as you wish. I'll glad to add your changes to the next version of the package!

License

MIT License.

  • .NETCoreApp 3.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on DanielCunha.Toolkit.RepositoryPattern:

Package Downloads
DanielCunha.Toolkit.RepositoryPattern.EntityFrameworkCore

An implementation of DanielCunha.Toolkit.RepositoryPattern using Entity Framework Core.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 321 11/9/2019
1.0.0 258 11/9/2019