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.
$ dotnet add package Rystem.RepositoryFramework.CacheYou can add a repository (with default blob integration for instance) and after attack an in memory cache for all methods. The RefreshTime is a property that adds an Expiration date to the cached value, in the example below you can see that after 20 seconds the in memory cache requests again to the repository pattern a new value for each key. The Methods is a flag that allows to setup what operations have to be cached.
Query -> query will be cached with this key
var keyAsString = $"{nameof(RepositoryMethod.Query)}_{your query request as string}";
Operation -> operation will be cached with this key
var keyAsString = $"{nameof(RepositoryMethod.Operation)}_{type of operation}_{your query request as string}";
Get -> query will be cached with this key
var keyAsString = $"{nameof(RepositoryMethod.Get)}_{key}";
Exist -> query will be cached with this key
var keyAsString = $"{nameof(RepositoryMethod.Exist)}_{key}";
Now you can understand the special behavior for commands. If you set Insert and/or Update and/or Delete, during any command if you allowed it for each command automatically the framework will update the cache value, with updated or inserted value or removing the deleted value. The code below allows everything
x.Methods = RepositoryMethod.All
In the example below you're setting up the following behavior: setting up a cache only for Get operation, and update the Get cache when exists a new Insert or an Update, or a removal when Delete operation were perfomed.
x.Methods = RepositoryMethod.Get | RepositoryMethod.Insert | RepositoryMethod.Update | RepositoryMethod.Delete
builder.Services
.AddRepositoryInBlobStorage<User, string>(builder.Configuration["ConnectionString:Storage"])
.WithInMemoryCache(x =>
{
x.RefreshTime = TimeSpan.FromSeconds(20);
x.Methods = RepositoryMethod.All;
})
You always will find the same interface. For instance
IRepository<User, string> repository
or if you added a query pattern or command pattern
IQuery<User, string> query
ICommand<User, string> command
Based on this link you may use the standard interface IDistributedCache instead of create a custom IDistributedCache<T, TKey, TState>. For instance you may choose between three libraries: Distributed SQL Server cache, Distributed Redis cache, Distributed NCache cache. You need to add the cache
builder.Services.AddStackExchangeRedisCache(options =>
{
options.Configuration = builder.Configuration.GetConnectionString("MyRedisConStr");
options.InstanceName = "SampleInstance";
});
then you add the IDistributedCache implementation to your repository patterns or CQRS.
builder.Services.
AddRepository<User, UserRepository>()
WithDistributedCache();
and as always you will use the standard interface that is automatically integrated in the repository flow.
IRepository<User> repository;
The same is valid for ICommand and IQuery.