Shardis.DependencyInjection
0.2.2
dotnet add package Shardis.DependencyInjection --version 0.2.2
NuGet\Install-Package Shardis.DependencyInjection -Version 0.2.2
<PackageReference Include="Shardis.DependencyInjection" Version="0.2.2" />
<PackageVersion Include="Shardis.DependencyInjection" Version="0.2.2" />
<PackageReference Include="Shardis.DependencyInjection" />
paket add Shardis.DependencyInjection --version 0.2.2
#r "nuget: Shardis.DependencyInjection, 0.2.2"
#:package Shardis.DependencyInjection@0.2.2
#addin nuget:?package=Shardis.DependencyInjection&version=0.2.2
#tool nuget:?package=Shardis.DependencyInjection&version=0.2.2
Shardis.DependencyInjection
Lightweight fluent registration helpers for per-shard resource factories (e.g. DbContext, IDocumentSession, Redis connections) integrated with Microsoft.Extensions.DependencyInjection.
Install
dotnet add package Shardis.DependencyInjection --version 0.1.*
When to use
- You need to provision one resource instance per logical shard (EF Core DbContext, Marten session, etc.)
- You want strongly-typed
IShardFactory<T>
resolution via DI - You prefer fluent bulk shard registration rather than manual loops
- You want deterministic shard id enumeration at runtime
What’s included
IShardFactory<T>
resolution (implemented byDependencyInjectionShardFactory<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 ofIAsyncDisposable
shard resources
Quick start
var 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"));
Configuration / Options
All registration methods are additive; duplicate shard ids throw InvalidOperationException
.
Patterns:
- Sync delegate:
Func<ShardId, T>
(wrapped in completed ValueTask) - Async delegate:
Func<IServiceProvider, ShardId, ValueTask<T>>
- Instance: constant per shard
Integration notes
- Depends only on core
Shardis
abstractions +Microsoft.Extensions.DependencyInjection
- Works alongside
Shardis.Query.*
packages — provision sessions/contexts, then plug into query executors - Deterministic: no randomness in mapping; enumeration order is registration order (by shard id natural ordering)
Capabilities & limits
- ✅ Thread-safe registration (startup only) & lookup
- ✅ Per-type isolation (registries are generic)
- ⚠️ No dynamic shard add/remove post-build (scope: startup time)
- 🧩 Compatibility: .NET 8.0, .NET 9.0
Samples & tests
Samples use standard service registration patterns (see repository samples folder). Add specific DI usage samples in future iterations.
Versioning & compatibility
- Target frameworks:
net8.0
,net9.0
- Follows parent repo semantic versioning schedule
Contributing
PRs welcome. See contribution guidelines: CONTRIBUTING.md
License
MIT — see LICENSE
Links
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
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 |
---|---|---|
0.2.2 | 127 | 9/11/2025 |
0.2.2-prerelease0001 | 125 | 9/11/2025 |
0.2.1 | 124 | 9/10/2025 |
0.2.1-prerelease0001 | 125 | 9/9/2025 |
0.2.0 | 120 | 9/8/2025 |
0.1.0-prerelease0086 | 134 | 9/7/2025 |
0.1.0-prerelease0085 | 135 | 9/7/2025 |
Initial release. Full notes: https://github.com/veggerby/shardis/blob/main/CHANGELOG.md#010---2025-08-25