Tenowg.MongoOptions
1.0.2
See the version list below for details.
dotnet add package Tenowg.MongoOptions --version 1.0.2
NuGet\Install-Package Tenowg.MongoOptions -Version 1.0.2
<PackageReference Include="Tenowg.MongoOptions" Version="1.0.2" />
<PackageVersion Include="Tenowg.MongoOptions" Version="1.0.2" />
<PackageReference Include="Tenowg.MongoOptions" />
paket add Tenowg.MongoOptions --version 1.0.2
#r "nuget: Tenowg.MongoOptions, 1.0.2"
#:package Tenowg.MongoOptions@1.0.2
#addin nuget:?package=Tenowg.MongoOptions&version=1.0.2
#tool nuget:?package=Tenowg.MongoOptions&version=1.0.2
+ Please visit my [MongoOptions.Blazor](https://github.com/tenowg/MongoOptions.Blazor) Github, it will be a usuable project to add Razor components to manage your Config files
MongoOptions.Core 🍃
A high-performance, resilient configuration provider for .NET 10 that uses MongoDB as a backing store with built-in memory caching and data validation.
🚀 Features
- Fluent Configuration: Set up in seconds with a clean, readable API.
- Resilient Caching: Powered by
IMemoryCachewith "Stale-on-Failure" protection. - Keyed/Named Options: Support for multiple configuration instances (e.g., Tenant-specific settings).
- Data Validation: Built-in support for Data Annotations to keep your DB clean.
- Management API: Full CRUD support for managing configs via code (perfect for Blazor Admin UIs).
📦 Installation
dotnet add package Tenowg.MongoOptions
🛠️ Quick Start
1. Define your Settings POCO
Use standard Data Annotations for validation and our custom attribute for DB naming.
[Options(DatabaseName = "AppSettings", CollectionName = "FeatureToggle")]
public class FeatureSettings
{
[Required]
public string Theme { get; set; } = "Light";
[Range(1, 100)]
public int MaxRetries { get; set; } = 5;
}
The Options attribute is completely optional, Database and Collections can be generated by default.
2. Register in Program.cs
Use the Fluent API to hook everything up.
builder.Services.AddMongoConfiguration(config =>
{
config.ConnectionString = "mongodb://localhost:27017";
config.DatabaseName = "MyProductionApp";
})
.RegisterOptions<FeatureSettings>();
3. Usage
Inject IOptionsSnapshot<T> for automatic updates every request, or IOptionsMonitor<T> for real-time changes.
public class MyService(IOptionsSnapshot<FeatureSettings> settings)
{
public void DoWork()
{
// Access the "Default" config
var theme = settings.Value.Theme;
// Access a "Named" config
var tenantSettings = settings.Get("Tenant_A");
}
}
🛡️ Validation & Resilience
This library ensures your app never runs with "garbage" data.
- Strict Lookups: If you request a named config that doesn't exist, it throws a
KeyNotFoundException. - Schema Protection: If a MongoDB document fails Data Annotation validation, an
OptionsValidationExceptionis thrown. - Database Downtime: If MongoDB goes offline, the library will continue to serve cached data from the cache for 1 minute before trying the DB again, preventing "request spam."
🔧 Management (CRUD)
Use IConfigManager to build your own admin dashboard.
// Get all available config names for a type
var keys = await _configManager.GetKeys<FeatureSettings>();
// Save or Update
await _configManager.UpdateConfigAsync("NewTenant", mySettingsObject);
// Remove
await _configManager.RemoveConfig<FeatureSettings>("OldTenant");
// Clone
await _configManager.CloneConfigAsync<FeatureSettings>("SourceTenant", "TargetTenant");
📚 API Reference
MongoOptionsExtensions
AddMongoConfiguration(Action<MongoConfigurationOptions>): Sets up MongoDB client and returns a builder.AddMongoOptions<T>(): Registers options for a type (alternative to builder).
MongoOptionsBuilder
RegisterOptions<T>(): Registers additional options types.
IConfigManager
UpdateConfigAsync<T>(T): Updates default config.UpdateConfigAsync<T>(string, T): Updates named config.GetKeys<T>(): Lists all config names for a type.RemoveConfig<T>(string): Deletes a named config.CloneConfigAsync<T>(string, string): Copies a config to a new name.
MongoConfigurationOptions
ConnectionString: MongoDB connection string.DatabaseName: Default database name.CacheSoftDuration: Cache refresh interval.CacheHardDuration: Max cache lifetime.CachePrefix: Cache key prefix.
OptionsAttribute
CollectionName: Custom collection name.DatabaseName: Custom database name.
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🆘 Support
If you encounter any issues or have questions, please open an issue on GitHub.
| 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.Caching.Memory (>= 10.0.2)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.2)
- MongoDB.Driver (>= 3.6.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Tenowg.MongoOptions:
| Package | Downloads |
|---|---|
|
Tenowg.MongoOptions.Blazor
A Blazor library to use with MongoOptions. |
GitHub repositories
This package is not used by any popular GitHub repositories.