Rystem.RepositoryFramework.Abstractions 4.1.18

There is a newer version of this package available.
See the version list below for details.
dotnet add package Rystem.RepositoryFramework.Abstractions --version 4.1.18                
NuGet\Install-Package Rystem.RepositoryFramework.Abstractions -Version 4.1.18                
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="Rystem.RepositoryFramework.Abstractions" Version="4.1.18" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Rystem.RepositoryFramework.Abstractions --version 4.1.18                
#r "nuget: Rystem.RepositoryFramework.Abstractions, 4.1.18"                
#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 Rystem.RepositoryFramework.Abstractions as a Cake Addin
#addin nuget:?package=Rystem.RepositoryFramework.Abstractions&version=4.1.18

// Install Rystem.RepositoryFramework.Abstractions as a Cake Tool
#tool nuget:?package=Rystem.RepositoryFramework.Abstractions&version=4.1.18                

What is Rystem?

Interfaces

Based on CQRS we could split our repository pattern in two main interfaces, one for update (write, delete) and one for read.

Command (Write-Delete)
public interface ICommandPattern<T, TKey> : ICommandPattern
    where TKey : notnull
{
    Task<State<T, TKey>> InsertAsync(TKey key, T value, CancellationToken cancellationToken = default);
    Task<State<T, TKey>> UpdateAsync(TKey key, T value, CancellationToken cancellationToken = default);
    Task<State<T, TKey>> DeleteAsync(TKey key, CancellationToken cancellationToken = default);
    Task<BatchResults<T, TKey>> BatchAsync(BatchOperations<T, TKey> operations, CancellationToken cancellationToken = default);
}
Query (Read)
public interface IQueryPattern<T, TKey> : IQueryPattern
    where TKey : notnull
{
    Task<State<T, TKey>> ExistAsync(TKey key, CancellationToken cancellationToken = default);
    Task<T?> GetAsync(TKey key, CancellationToken cancellationToken = default);
    IAsyncEnumerable<IEntity<T, TKey>> QueryAsync(IFilterExpression filter, CancellationToken cancellationToken = default);
    ValueTask<TProperty> OperationAsync<TProperty>(OperationType<TProperty> operation, IFilterExpression filter, CancellationToken cancellationToken = default);
}
Repository Pattern (Write-Delete-Read)

Repository pattern is a sum of CQRS interfaces.

public interface IRepositoryPattern<T, TKey> : ICommandPattern<T, TKey>, IQueryPattern<T, TKey>, IRepositoryPattern, ICommandPattern, IQueryPattern
    where TKey : notnull
{
    Task<State<T, TKey>> InsertAsync(TKey key, T value, CancellationToken cancellationToken = default);
    Task<State<T, TKey>> UpdateAsync(TKey key, T value, CancellationToken cancellationToken = default);
    Task<State<T, TKey>> DeleteAsync(TKey key, CancellationToken cancellationToken = default);
    Task<BatchResults<T, TKey>> BatchAsync(BatchOperations<T, TKey> operations, CancellationToken cancellationToken = default);
    Task<State<T, TKey>> ExistAsync(TKey key, CancellationToken cancellationToken = default);
    Task<T?> GetAsync(TKey key, CancellationToken cancellationToken = default);
    IAsyncEnumerable<IEntity<T, TKey>> QueryAsync(IFilterExpression filter, CancellationToken cancellationToken = default);
    ValueTask<TProperty> OperationAsync<TProperty>(OperationType<TProperty> operation, IFilterExpression filter, CancellationToken cancellationToken = default);
}

Examples

Model
public class User
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}
Command

Your storage class has to extend ICommand, and use it on injection

public class UserWriter : ICommand<User, string>
{
    public Task<State<User, string>> DeleteAsync(string key, CancellationToken cancellationToken = default)
    {
        //delete on with DB or storage context
        throw new NotImplementedException();
    }
    public Task<State<User, string>> InsertAsync(string key, User value, CancellationToken cancellationToken = default)
    {
        //insert on DB or storage context
        throw new NotImplementedException();
    }
    public Task<State<User, string>> UpdateAsync(string key, User value, CancellationToken cancellationToken = default)
    {
        //update on DB or storage context
        throw new NotImplementedException();
    }
    public Task<BatchResults<User, string>> BatchAsync(BatchOperations<User, string> operations, CancellationToken cancellationToken = default)
    {
        //insert, update or delete some items on DB or storage context
        throw new NotImplementedException();
    }
}
Query

Your storage class has to extend IQuery, and use it on injection

public class UserReader : IQuery<User, string>
{
    public Task<User?> GetAsync(string key, CancellationToken cancellationToken = default)
    {
        //get an item by key from DB or storage context
        throw new NotImplementedException();
    }
    public Task<State<User, string>> ExistAsync(string key, CancellationToken cancellationToken = default)
    {
        //check if an item by key exists in DB or storage context
        throw new NotImplementedException();
    }
    public IAsyncEnumerable<IEntity<User, string>> QueryAsync(IFilterExpression filter, CancellationToken cancellationToken = default)
    {
        //get a list of items by a predicate with top and skip from DB or storage context
        throw new NotImplementedException();
    }
    public ValueTask<TProperty> OperationAsync<TProperty>(OperationType<TProperty> operation, IFilterExpression filter, CancellationToken cancellationToken = default)
    {
        //get an items count by a predicate with top and skip from DB or storage context or max or min or some other operations
        throw new NotImplementedException();
    }
}
Alltogether as repository pattern

if you don't have CQRS infrastructure (usually it's correct to use CQRS when you have minimum two infrastructures one for write and delete and at least one for read). You may choose to extend IRepository, but when you inject you have to use IRepository

public class UserRepository : IRepository<User, string>, IQuery<User, string>, ICommand<User, string>
{
    public Task<State<User, string>> DeleteAsync(string key, CancellationToken cancellationToken = default)
    {
        //delete on with DB or storage context
        throw new NotImplementedException();
    }
    public Task<State<User, string>> InsertAsync(string key, User value, CancellationToken cancellationToken = default)
    {
        //insert on DB or storage context
        throw new NotImplementedException();
    }
    public Task<State<User, string>> UpdateAsync(string key, User value, CancellationToken cancellationToken = default)
    {
        //update on DB or storage context
        throw new NotImplementedException();
    }
    public Task<BatchResults<User, string>> BatchAsync(BatchOperations<User, string> operations, CancellationToken cancellationToken = default)
    {
        //insert, update or delete some items on DB or storage context
        throw new NotImplementedException();
    }
    public Task<User?> GetAsync(string key, CancellationToken cancellationToken = default)
    {
        //get an item by key from DB or storage context
        throw new NotImplementedException();
    }
    public Task<State<User, string>> ExistAsync(string key, CancellationToken cancellationToken = default)
    {
        //check if an item by key exists in DB or storage context
        throw new NotImplementedException();
    }
    public IAsyncEnumerable<IEntity<User, string>> QueryAsync(IFilterExpression filter, CancellationToken cancellationToken = default)
    {
        //get a list of items by a predicate with top and skip from DB or storage context
        throw new NotImplementedException();
    }
    public ValueTask<TProperty> OperationAsync<TProperty>(OperationType<TProperty> operation, IFilterExpression filter, CancellationToken cancellationToken = default)
    {
        //get an items count by a predicate with top and skip from DB or storage context or max or min or some other operations
        throw new NotImplementedException();
    }
}

How to use it

In DI you install the service

services.AddRepository<User, string, UserRepository>();

And you may inject the object

Please, use IRepository and not IRepositoryPattern

IRepository<User, string> repository

Query and Command

In DI you install the services

services.AddCommand<User, string, UserWriter>();
services.AddQuery<User, string, UserReader>();

And you may inject the objects

Please, use ICommand, IQuery and not ICommandPattern, IQueryPattern

ICommand<User, string> command
IQuery<User, string> command

Twice or more configurations for the same repository service

Pay attention during the DI, you may install the same repository two or more times for the same model. If you want to be sure, you may start the DI with a method that checks for you. When can it happen? For instance, when you use at the same time InMemory integration with a custom integration.

services.ThrowExceptionIfARepositoryServiceIsAddedTwoOrMoreTimes();

TKey when it's not a primitive

You can use a class or record. Record is better in my opinion, for example, if you want to use the Equals operator from key, with record you don't check it by the refence but by the value of the properties in the record. My key:

public class MyKey 
{
    public int Id { get; set; }
    public int Id2 { get; set; }
}

the DI

services.AddRepository<User, MyKey, UserRepository>();

and you may inject (for ICommand and IQuery is the same)

IRepository<User, MyKey> repository

Default TKey record

You may use the default record key in repository framework namespace. It's not really useful when used with no-primitive or no-struct objects (in terms of memory usage [Heap]). For 1 value (it's not really useful I know, but I liked to create it).

new Key<int>(2);

or for 2 values (useful)

new Key<int, int>(2, 4);

or for 3 values (unuseful)

new Key<int, int, string>(2, 4, "312");

or for 4 values (useful)

new Key<int, int, double, Guid>(2, 4, 3, Guid.NewGuid());

or for 5 values (unuseful)

new Key<int, int, string, Guid, string>(2, 4, "312", Guid.NewGuid(), "3232");

the DI

services.AddRepository<User, Key<int, int>, UserRepository>();

and you may inject (for ICommand and IQuery is the same)

IRepository<User, Key<int, int>> repository

Translation

In some cases you need to "translate" your query for your database context query, for example in case of EF integration.

.AddDbContext<SampleContext>(options =>
{
    options.UseSqlServer(configuration["ConnectionString:Database"]);
}, ServiceLifetime.Scoped)
.AddRepository<AppUser, AppUserKey, AppUserStorage>(settings => 
{
    settings
        .Translate<User>()
            .With(x => x.Id, x => x.Identificativo)
            .With(x => x.Username, x => x.Nome)
            .With(x => x.Email, x => x.IndirizzoElettronico);
});

In this case I'm helping the Filter class to understand how to transform itself when used in a different context. Use Filter methods to help to translate and apply to your context the right query.

await foreach (var user in filter.ApplyAsAsyncEnumerable(_context.Users))
    yield return new AppUser(user.Identificativo, user.Nome, user.IndirizzoElettronico, new());

You may use Filter for queryable, FilterAsEnumerable for Enumerable and FilterAsAsyncEnumerable for async enumerable context.

You can add more translations for the same model

.AddDbContext<SampleContext>(options =>
{
    options.UseSqlServer(configuration["ConnectionString:Database"]);
}, ServiceLifetime.Scoped)
.AddRepository<AppUser, AppUserKey, AppUserStorage>(settings => {
    settings
        .Translate<User>()
            .With(x => x.Id, x => x.Identificativo)
            .With(x => x.Username, x => x.Nome)
            .With(x => x.Email, x => x.IndirizzoElettronico)
        .AndTranslate<AnotherUser>()
            .With(x => x.Id, x => x.Identificativo)
            .With(x => x.Username, x => x.Nome)
            .With(x => x.Email, x => x.IndirizzoElettronico)
});

Entity framework examples

Here you may find the example Repository pattern applied Unit test flow

Business Manager

You have the chance to write your business methods to execute them before or after a command or query. For instance, you have to check before an update or insert the value of an entity and deny the final insert/update on the database.

Example

In this example BeforeInsertAsync runs before InsertAsync of IRepository/ICommand and AfterInsertAsync runs after InsertAsync of IRepository/ICommand.

.AddRepositoryInMemoryStorage<Animal, long>(settings => {
    settings
        .AddBusiness()
            .AddBusinessAfterInsert<AnimalBusiness>()
            .AddBusinessBeforeInsert<AnimalBusiness>();
});
    

more interesting usage comes to move business in another project, you can add to your infrastructure in the following way

.AddBusinessForRepository<Animal, long>(settings => {
    settings
        .AddBusiness()
            .AddBusinessAfterInsert<AnimalBusiness>()
            .AddBusinessBeforeInsert<AnimalBusiness>();
});
   

Then, you could have a library for infrastructure (or more than one) and a library for business to separate furthermore the concepts.

The animal business to inject will be the following one.

public sealed class AnimalBusiness : IRepositoryBusinessBeforeInsert<Animal, long>, IRepositoryBusinessAfterInsert<Animal, long>
{
    public static int After;
    public Task<State<Animal>> AfterInsertAsync(State<Animal, long> state, Entity<Animal, long> entity, CancellationToken cancellationToken = default)
    {
        After++;
        return Task.FromResult(state);
    }

    public static int Before;
    public Task<State<Animal, long>> BeforeInsertAsync(Entity<Animal, long> entity, CancellationToken cancellationToken = default)
    {
        Before++;
        return Task.FromResult(State.Ok(entity));
    }
}

You have to create the class as public to allow the dependency injection to instastiate it. It's added directly to DI.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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 (12)

Showing the top 5 NuGet packages that depend on Rystem.RepositoryFramework.Abstractions:

Package Downloads
Rystem.RepositoryFramework.Api.Server

Rystem.RepositoryFramework allows you to use correctly concepts like repository pattern, CQRS and DDD. You have interfaces for your domains, auto-generated api, auto-generated HttpClient to simplify connection "api to front-end", a functionality for auto-population in memory of your models, a functionality to simulate exceptions and waiting time from external sources to improve your implementation/business test and load test.

Rystem.RepositoryFramework.Api.Client

Rystem.RepositoryFramework allows you to use correctly concepts like repository pattern, CQRS and DDD. You have interfaces for your domains, auto-generated api, auto-generated HttpClient to simplify connection "api to front-end", a functionality for auto-population in memory of your models, a functionality to simulate exceptions and waiting time from external sources to improve your implementation/business test and load test.

Rystem.RepositoryFramework.Infrastructure.Azure.Storage.Blob

Rystem.RepositoryFramework allows you to use correctly concepts like repository pattern, CQRS and DDD. You have interfaces for your domains, auto-generated api, auto-generated HttpClient to simplify connection "api to front-end", a functionality for auto-population in memory of your models, a functionality to simulate exceptions and waiting time from external sources to improve your implementation/business test and load test.

Rystem.RepositoryFramework.Infrastructure.Azure.Storage.Table

Rystem.RepositoryFramework allows you to use correctly concepts like repository pattern, CQRS and DDD. You have interfaces for your domains, auto-generated api, auto-generated HttpClient to simplify connection "api to front-end", a functionality for auto-population in memory of your models, a functionality to simulate exceptions and waiting time from external sources to improve your implementation/business test and load test.

Rystem.RepositoryFramework.Cache

Rystem.RepositoryFramework allows you to use correctly concepts like repository pattern, CQRS and DDD. You have interfaces for your domains, auto-generated api, auto-generated HttpClient to simplify connection "api to front-end", a functionality for auto-population in memory of your models, a functionality to simulate exceptions and waiting time from external sources to improve your implementation/business test and load test.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
9.0.0 60,744 11/16/2024
9.0.0-rc.1 150 10/18/2024
6.2.0 219,337 10/9/2024
6.1.1 222 10/9/2024
6.1.0 48,084 9/29/2024
6.0.24 353 9/11/2024
6.0.23 340,257 7/18/2024
6.0.21 290 6/18/2024
6.0.20 727,874 6/16/2024
6.0.19 30,802 6/14/2024
6.0.18 260 6/14/2024
6.0.17 263 6/14/2024
6.0.16 50,128 6/10/2024
6.0.15 272 6/9/2024
6.0.14 94,596 5/24/2024
6.0.13 280 5/23/2024
6.0.12 234 5/23/2024
6.0.11 302 5/20/2024
6.0.9 293 5/19/2024
6.0.7 262 5/18/2024
6.0.6 292 5/10/2024
6.0.5 236 5/10/2024
6.0.4 550,232 4/3/2024
6.0.3 1,637 3/25/2024
6.0.2 378,195 3/11/2024
6.0.1 51,177 3/8/2024
6.0.0 1,172,017 11/21/2023
6.0.0-rc.6 146 10/25/2023
6.0.0-rc.5 112 10/25/2023
6.0.0-rc.4 99 10/23/2023
6.0.0-rc.3 87 10/19/2023
6.0.0-rc.2 124 10/18/2023
6.0.0-rc.1 136 10/16/2023
5.0.20 641,480 9/25/2023
5.0.19 3,190 9/10/2023
5.0.18 2,459 9/6/2023
5.0.17 2,358 9/6/2023
5.0.16 2,413 9/5/2023
5.0.15 2,370 9/5/2023
5.0.14 2,385 9/5/2023
5.0.13 2,446 9/1/2023
5.0.12 2,308 8/31/2023
5.0.11 2,331 8/30/2023
5.0.10 2,366 8/29/2023
5.0.9 2,424 8/24/2023
5.0.8 2,473 8/24/2023
5.0.7 451,925 8/23/2023
5.0.6 19,827 8/21/2023
5.0.5 6,577 8/21/2023
5.0.4 2,479 8/16/2023
5.0.3 215,003 8/2/2023
5.0.2 4,294 8/2/2023
5.0.1 14,046 8/1/2023
5.0.0 14,450 7/31/2023
4.1.26 143,328 7/20/2023
4.1.25 27,136 7/16/2023
4.1.24 400,542 6/13/2023
4.1.23 48,217 6/13/2023
4.1.22 132,171 5/30/2023
4.1.21 58,199 5/20/2023
4.1.20 407,574 4/19/2023
4.1.19 98,343 3/20/2023
4.1.18 2,894 3/20/2023
4.1.17 3,146 3/16/2023
4.1.16 2,891 3/16/2023
4.1.15 2,847 3/15/2023
4.1.14 10,566 3/9/2023
4.1.13 2,998 3/7/2023
4.1.12 3,455 2/9/2023
4.1.11 3,057 1/26/2023
4.1.10 3,280 1/22/2023
4.1.9 2,941 1/20/2023
4.1.8 3,216 1/18/2023
4.1.7 3,169 1/18/2023
4.1.6 3,236 1/17/2023
4.1.1 3,193 1/4/2023
4.1.0 3,075 1/1/2023
3.1.5 3,025 12/21/2022
3.1.4 1,542 12/21/2022
3.1.3 3,334 12/12/2022
3.1.2 3,086 12/7/2022
3.1.1 3,174 12/7/2022
3.1.0 3,154 12/1/2022
3.0.29 3,121 12/1/2022
3.0.28 3,952 12/1/2022
3.0.27 3,324 11/23/2022
3.0.25 6,892 11/23/2022
3.0.24 4,544 11/18/2022
3.0.23 4,219 11/18/2022
3.0.22 4,359 11/15/2022
3.0.21 4,398 11/14/2022
3.0.20 4,464 11/13/2022
3.0.19 4,756 11/2/2022
3.0.18 4,509 11/2/2022
3.0.17 4,572 10/29/2022
3.0.16 4,649 10/29/2022
3.0.15 1,640 10/29/2022
3.0.14 7,478 10/24/2022
3.0.13 4,697 10/24/2022
3.0.12 4,707 10/17/2022
3.0.11 4,660 10/10/2022
3.0.10 4,219 10/6/2022
3.0.9 4,147 10/6/2022
3.0.8 4,121 10/6/2022
3.0.7 4,233 10/6/2022
3.0.6 4,225 10/5/2022
3.0.5 4,126 10/5/2022
3.0.4 4,195 10/5/2022
3.0.3 4,175 10/3/2022
3.0.2 4,235 9/30/2022
3.0.1 4,178 9/29/2022
3.0.0 1,676 9/29/2022
2.0.17 3,816 9/29/2022
2.0.16 4,297 9/27/2022
2.0.15 4,407 9/27/2022
2.0.14 4,312 9/26/2022
2.0.13 4,236 9/26/2022
2.0.12 4,232 9/26/2022
2.0.11 4,170 9/25/2022
2.0.10 4,377 9/25/2022
2.0.9 4,254 9/22/2022
2.0.8 4,166 9/22/2022
2.0.7 1,684 9/22/2022
2.0.6 4,178 9/20/2022
2.0.5 4,383 9/20/2022
2.0.4 4,255 9/20/2022
2.0.2 4,272 9/20/2022
2.0.1 4,495 9/13/2022
2.0.0 4,361 8/19/2022
1.1.24 4,380 7/30/2022
1.1.23 4,319 7/29/2022
1.1.22 4,129 7/29/2022
1.1.21 4,535 7/29/2022
1.1.20 4,296 7/29/2022
1.1.19 4,325 7/27/2022
1.1.17 4,348 7/27/2022
1.1.16 4,336 7/26/2022
1.1.15 4,384 7/25/2022
1.1.14 4,335 7/25/2022
1.1.13 4,233 7/22/2022
1.1.12 4,238 7/19/2022
1.1.11 4,315 7/19/2022
1.1.10 4,287 7/19/2022
1.1.9 4,324 7/19/2022
1.1.8 4,386 7/18/2022
1.1.7 4,213 7/18/2022
1.1.6 4,310 7/18/2022
1.1.5 4,304 7/17/2022
1.1.4 1,659 7/17/2022
1.1.3 6,823 7/17/2022
1.1.2 4,357 7/17/2022
1.1.1 1,691 7/17/2022
1.1.0 4,295 7/17/2022
1.0.2 4,300 7/15/2022
1.0.1 2,985 7/15/2022
1.0.0 5,499 7/8/2022
0.10.7 4,316 7/7/2022
0.10.6 1,752 7/7/2022
0.10.3 2,202 7/7/2022
0.10.2 5,748 7/2/2022
0.10.1 4,306 7/1/2022
0.10.0 4,106 7/1/2022
0.9.10 5,409 6/20/2022
0.9.9 4,365 6/11/2022
0.9.8 1,675 6/10/2022
0.9.7 4,243 6/9/2022
0.9.6 4,232 6/5/2022
0.9.5 2,849 6/3/2022
0.9.3 4,140 6/3/2022
0.9.2 2,459 5/31/2022
0.9.1 2,414 5/31/2022
0.9.0 2,400 5/31/2022
0.8.3-beta.1 113 5/31/2022
0.8.2 1,720 5/30/2022
0.8.1 1,729 5/29/2022