Fluent shard registration & per-shard resource factories for Shardis using Microsoft.Extensions.DependencyInjection.
$ dotnet add package Shardis.DependencyInjectionLightweight fluent registration helpers for per-shard resource factories (e.g. DbContext, IDocumentSession, Redis connections) integrated with Microsoft.Extensions.DependencyInjection.
dotnet add package Shardis.DependencyInjection --version 0.1.*
IShardFactory<T> resolution via DIIShardFactory<T> resolution (implemented by DependencyInjectionShardFactory<T>)AddShard / AddShards (sync + async variants) fluent registration methodsAddShardInstance for supplying a pre-created singleton-per-shard instanceGetRegisteredShards<T>() enumeration helperUseAsync helpers for safe create/use/dispose of IAsyncDisposable shard resourcesvar services = new ServiceCollection();
// Register 4 shards with synchronous factory
services.AddShards<MyDbContext>(4, shard => new MyDbContext(BuildOptionsFor(shard)));
// Or async creation per shard
services.AddShard<MyDbContext>(new ShardId("primary"), async (sp, shard) =>
{
await Task.Yield();
return new MyDbContext(BuildOptionsFor(shard));
});
var provider = services.BuildServiceProvider();
var factory = provider.GetRequiredService<IShardFactory<MyDbContext>>();
await using var ctx = await factory.CreateAsync(new ShardId("2"));
All registration methods are additive; duplicate shard ids throw InvalidOperationException.
Patterns:
Func<ShardId, T> (wrapped in completed ValueTask)Func<IServiceProvider, ShardId, ValueTask<T>>Shardis abstractions + Microsoft.Extensions.DependencyInjectionShardis.Query.* packages — provision sessions/contexts, then plug into query executorsSamples use standard service registration patterns (see repository samples folder). Add specific DI usage samples in future iterations.
net8.0, net9.0PRs welcome. See contribution guidelines: CONTRIBUTING.md
MIT — see LICENSE