DanielCunha.Toolkit.RepositoryPattern.EntityFrameworkCore
1.0.4
dotnet add package DanielCunha.Toolkit.RepositoryPattern.EntityFrameworkCore --version 1.0.4
NuGet\Install-Package DanielCunha.Toolkit.RepositoryPattern.EntityFrameworkCore -Version 1.0.4
<PackageReference Include="DanielCunha.Toolkit.RepositoryPattern.EntityFrameworkCore" Version="1.0.4" />
paket add DanielCunha.Toolkit.RepositoryPattern.EntityFrameworkCore --version 1.0.4
#r "nuget: DanielCunha.Toolkit.RepositoryPattern.EntityFrameworkCore, 1.0.4"
// Install DanielCunha.Toolkit.RepositoryPattern.EntityFrameworkCore as a Cake Addin #addin nuget:?package=DanielCunha.Toolkit.RepositoryPattern.EntityFrameworkCore&version=1.0.4 // Install DanielCunha.Toolkit.RepositoryPattern.EntityFrameworkCore as a Cake Tool #tool nuget:?package=DanielCunha.Toolkit.RepositoryPattern.EntityFrameworkCore&version=1.0.4
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.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 is compatible. netcoreapp3.1 was computed. |
-
.NETCoreApp 3.0
- DanielCunha.Toolkit.RepositoryPattern (>= 1.0.1)
- Microsoft.EntityFrameworkCore (>= 3.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.