Sagittaras.Repository 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Sagittaras.Repository --version 1.0.0                
NuGet\Install-Package Sagittaras.Repository -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Sagittaras.Repository" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Sagittaras.Repository --version 1.0.0                
#r "nuget: Sagittaras.Repository, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Sagittaras.Repository as a Cake Addin
#addin nuget:?package=Sagittaras.Repository&version=1.0.0

// Install Sagittaras.Repository as a Cake Tool
#tool nuget:?package=Sagittaras.Repository&version=1.0.0                

Sagittaras.Repository by Sagittaras Games

Implementation of Generic Repository Pattern.

Usage

Repository pattern provides an extra layer for accessing entities data in database without direct access to database context class.

Your entity operations and queries are grouped together in one single class.

Define your repository

To define your repository just extends Repository<TEntity, TKey> class, providing the type of entity and the type of its primary key.

public class AuthorRepository : Repository<Author, Guid>
{
    public AuthorRepository(DbContext dbContext) : base(dbContext)
    {
    }
}

Composite key

If your entity is using composite key (only two primary keys are supported), extends instead Repository<TEntity, TFirstKey, TSecondKey> class.

public class BookTagRepository : Repository<BookTag, Guid, int>
{
    public BookTagRepository(DbContext dbContext) : base(dbContext)
    {
    }
}

Using interfaces

When you prefer to describe your repositories by interfaces, you can simply use interfaces equivalents to base classes.

public interface IAuthorRepository : IRepository<Author, Guid>
{
    Task<Author> GetByEmail(string email);
}

And then our repository...

public class AuthorRepository : Repository<Author, Guid>, IAuthorRepository 
{
    public BookTagRepository(DbContext dbContext) : base(dbContext)
    {
    }
    
    public Task<Author> GetByEmail(string email) 
    {
        throw new NotImplementedException();
    }
}

Register your repositories

Repositories can be easily registered to Dependency Container via extension method for the IServiceCollection.

ServiceCollection services = new();

services.UseRepositoryPattern(options =>
{
    options.AddRepository<IAuthorRepository, AuthorRepository>();
    options.AddRepository<BookRepository>();
    options.AddRepository<PublisherRepository>();
    options.AddRepository<BookTagRepository>();
});

Repositories can be registered only by their type or in combination with interface. All registered repositories are also registered under the IRepository type.

Current lifetime of repositories is Scoped.

Accessing data

The repository is providing Queryable property, which can be used to read the data from database.

public class AuthorRepository : Repository<Author, Guid>, IAuthorRepository 
{
    public BookTagRepository(DbContext dbContext) : base(dbContext)
    {
    }
    
    public async Task<Author> GetByEmail(string email) 
    {
        return await Queryable.SingleAsync(e => e.Email == email);
    }
}

Saving data

Repository is providing methods for Insert, Update, Remove and their Range equivalents.

IAuthorRepository repo = ServiceProvider.GetService<IAuthorRepository>();
Author author = new(){
    Email = "john.doe@example.com",
    FirstName = "John",
    LastName = "Doe"
}
repo.Insert(author);
await repo.SaveChangesAsync();

CRUD operations made to the repository needs to be saved by SaveChanges() or SaveChangesAsync() methods on Repository. This methods writes all unapplied changes to database context and saves them to the database.

Joining another DbSets

Repository also provides a Join<TAnotherEntity>() method, which can be used in join statements, to join another DbSets in the query.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.0 320 8/7/2024
2.0.1 88 8/5/2024
2.0.0 124 6/17/2024
2.0.0-preview-09 94 6/6/2024
2.0.0-preview-08 282 3/9/2023
2.0.0-preview-07 140 3/9/2023
2.0.0-preview-06 154 2/19/2023
2.0.0-preview-05 137 2/3/2023
2.0.0-preview-04 224 1/14/2023
2.0.0-preview-03 143 1/14/2023
2.0.0-preview-02 166 12/13/2022
2.0.0-pre1 185 7/31/2022
2.0.0-pre 193 6/24/2022
1.0.0 457 5/19/2022
1.0.0-pre 180 5/17/2022