NCode.PropertyBag
2.1.0
Prefix Reserved
See the version list below for details.
dotnet add package NCode.PropertyBag --version 2.1.0
NuGet\Install-Package NCode.PropertyBag -Version 2.1.0
<PackageReference Include="NCode.PropertyBag" Version="2.1.0" />
<PackageVersion Include="NCode.PropertyBag" Version="2.1.0" />
<PackageReference Include="NCode.PropertyBag" />
paket add NCode.PropertyBag --version 2.1.0
#r "nuget: NCode.PropertyBag, 2.1.0"
#:package NCode.PropertyBag@2.1.0
#addin nuget:?package=NCode.PropertyBag&version=2.1.0
#tool nuget:?package=NCode.PropertyBag&version=2.1.0
NCode.PropertyBag
Strongly typed property bags for .NET: store and read values by type-safe keys, override a value for a scope, clone a bag, and create bags through dependency injection.
This package holds the bag, PropertyBagDictionary, its factories and AddPropertyBag(); the interfaces and keys are in NCode.PropertyBag.Abstractions, which it depends on.
Installation
dotnet add package NCode.PropertyBag
The target frameworks are the ones the package's .csproj declares; they are listed on nuget.org's Frameworks tab and shown by the badge above. CHANGELOG.md records when a framework is added or dropped and which release line to stay on.
Example
Define each key once, typically as a static readonly field:
using NCode.PropertyBag;
public static class UserKeys
{
public static readonly PropertyBagKey<string> UserName = new("UserName");
public static readonly PropertyBagKey<int> UserAge = new("UserAge");
}
Then create a bag, store values and read them back:
var propertyBag = PropertyBagFactory.Create();
propertyBag.SetValue(UserKeys.UserName, "Alice").SetValue(UserKeys.UserAge, 30);
if (propertyBag.TryGetValue(UserKeys.UserName, out var userName))
{
Console.WriteLine($"User: {userName}"); // User: Alice
}
Scope sets a value until the returned scope is disposed, then restores the value the key held before, or removes the key when it held none:
var cultureKey = new PropertyBagKey<string>("Culture");
propertyBag.SetValue(cultureKey, "en-US");
string? inScope;
using (propertyBag.Scope(cultureKey, "fr-FR"))
{
propertyBag.TryGetValue(cultureKey, out inScope); // "fr-FR"
}
propertyBag.TryGetValue(cultureKey, out var afterScope); // "en-US" again
Dependency injection
AddPropertyBag() registers IPropertyBagFactory as a singleton, keeping one the application registered first:
var services = new ServiceCollection();
services.AddPropertyBag();
services.AddSingleton<RequestContextFactory>();
Learn more
- Documentation site: the quick start, the guides, the concepts and the API map
- README and samples
- API overview
- Changelog
- Report a vulnerability
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. |
-
net10.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 11.0.0)
- NCode.PropertyBag.Abstractions (>= 2.1.0)
NuGet packages (18)
Showing the top 5 NuGet packages that depend on NCode.PropertyBag:
| Package | Downloads |
|---|---|
|
NCode.Identity.Jwt.Abstractions
Package Description |
|
|
NCode.Identity.OpenId.Client.Abstractions
Package Description |
|
|
NCode.Identity.OpenId.Authentication.Abstractions
Package Description |
|
|
NCode.Identity.Jwt
Package Description |
|
|
NCode.Identity.Bff.Abstractions
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
See the changelog: https://github.com/NCodeGroup/PropertyBag/blob/HEAD/CHANGELOG.md