A common storage abstraction, tools and its basic local (in-memory) implementation.
$ dotnet add package assistant.net.storageBasic key-value based storage implementation designed to be easily extended and flexibly configured. It gives an opportunity to isolate an application from specific data store provider.
To add storage one of methods should be called:
var services = new ServiceCollection()
.AddStorage(b => ...) // adds dependencies and initial configuration
.ConfigureStorage(b => ...); // appends existing configuration (no dependencies are added)
where a specific provider should be selected for the storing data
services.ConfigureStorage(b => b.UseLocal()); // mongo or other
and key-value types allowed for storing
services.ConfigureStorage(b => b
.Add<SomeKey, SomeModel>() // by registering specific pairs
.AllowAnyType()); // or allowing any type
Storage configuration may be big and complicated so applying it again and again can be annoying and error-prone. But message configuration can help to avoid that
services.ConfigureStorage(b => b
.AddConfiguration<CustomStorageConfiguration>() // by a type parameter
.AddConfiguration(new CustomStorageConfiguration())); // or an instance
public class CustomStorageConfiguration : IStorageConfiguration { ... }
Storage implementation is based on named options so you can have multiple named storages with different configurations.
var provider = new ServiceCollection()
.AddStorage()
.ConfigureStorage("name-1", o => o.UseLocal().Add<int, ModelOne>())
.ConfigureStorage("name-2", o => o.UseMongo().Add<string, ModelTwo>())
.BuildServiceProvider();
using var scope1 = provider.CreateScopeWithNamedOptionContext("name-1");
using var scope2 = provider.CreateScopeWithNamedOptionContext("name-2");
var storage1 = scope1.ServiceProvider.GetRequiredService<IStorage<int, ModelOne>>();
var storage2 = scope2.ServiceProvider.GetRequiredService<IStorage<string, ModelTwo>>();
Once storage was properly configured a specific storages can be resolved
var storage = provider.GetRequiredService<IStorage<Key, SomeModel>>();
var historicalStorage = provider.GetRequiredService<IHistoricalStorage<Key, SomeModel>>();
var partitionedStorage = provider.GetRequiredService<IPartitionedStorage<Key, SomeModel>>();
or specific storages extended with operations like removing values, getting value details or stored keys
var regularAdminStorage = provider.GetRequiredService<IAdminStorage<Key, SomeModel>>();
var historicalAdminStorage = provider.GetRequiredService<IHistoricalAdminStorage<Key, SomeModel>>();
var partitionedAdminStorage = provider.GetRequiredService<IPartitionedAdminStorage<Key, SomeModel>>();
Pay attention, if specific storage isn't properly configured, resolving it will throw an exception.