NodaMoney.DependencyInjection
2.5.0
dotnet add package NodaMoney.DependencyInjection --version 2.5.0
NuGet\Install-Package NodaMoney.DependencyInjection -Version 2.5.0
<PackageReference Include="NodaMoney.DependencyInjection" Version="2.5.0" />
<PackageVersion Include="NodaMoney.DependencyInjection" Version="2.5.0" />
<PackageReference Include="NodaMoney.DependencyInjection" />
paket add NodaMoney.DependencyInjection --version 2.5.0
#r "nuget: NodaMoney.DependencyInjection, 2.5.0"
#:package NodaMoney.DependencyInjection@2.5.0
#addin nuget:?package=NodaMoney.DependencyInjection&version=2.5.0
#tool nuget:?package=NodaMoney.DependencyInjection&version=2.5.0
NodaMoney Dependency Injection Extensions
About
This package provides extension methods to easily integrate NodaMoney's MoneyContext with .NET's dependency injection system.
Usage
// Add default MoneyContext with standard configuration
services.AddMoneyContext();
// Access the MoneyContext in your services
public class PaymentService
{
private readonly MoneyContext _moneyContext;
public PaymentService(MoneyContext moneyContext)
{
_moneyContext = moneyContext;
}
// Use _moneyContext for calculations
}
Configuration Options
NodaMoney provides several ways to configure the MoneyContext, following Microsoft's recommended patterns for library authors.
Using Action-based Configuration
services.AddMoneyContext(options => {
options.DefaultCurrency = Currency.FromCode("USD");
options.RoundingStrategy = new HalfUpRounding();
options.Precision = 28;
options.MaxScale = 2;
});
Using Configuration System
// Option 1: Using pre-scoped configuration section
var moneyContextSection = Configuration.GetSection("MoneyContext");
services.AddMoneyContext(moneyContextSection);
// Option 2: Using root configuration with section path
services.AddMoneyContext(
Configuration,
"MoneyContext" // or any custom path like "App:Finance:MoneyContext"
);
Example appsettings.json
:
{
"MoneyContext": {
"DefaultCurrency": "EUR",
"Precision": 28,
"MaxScale": 2
}
}
Using Direct Options Instance
var options = new MoneyContextOptions {
DefaultCurrency = Currency.FromCode("USD"),
RoundingStrategy = new HalfUpRounding()
};
services.AddMoneyContext(options);
Named Contexts
For applications that need to work with multiple currencies or rounding rules:
// Register multiple named contexts
services.AddMoneyContext(options => {
options.DefaultCurrency = Currency.FromCode("USD");
options.RoundingStrategy = new HalfUpRounding();
}, name: "US");
services.AddMoneyContext(options => {
options.DefaultCurrency = Currency.FromCode("EUR");
options.RoundingStrategy = new HalfEvenRounding();
}, name: "EU");
// Access named contexts in your services
public class InternationalPaymentService
{
private readonly IMoneyContextResolver _contextResolver;
public InternationalPaymentService(IMoneyContextResolver contextResolver)
{
_contextResolver = contextResolver;
}
public Money CalculatePrice(string region, decimal amount)
{
// Get appropriate context based on region
var contextName = region == "US" ? "US" : "EU";
var context = _contextResolver.GetContext(contextName);
// Use context for calculation
using var scope = MoneyContext.CreateScope(context);
return new Money(amount); // Uses the scoped context
}
}
Real-world Scenarios for Named Contexts
Named contexts are particularly useful in scenarios where different parts of an application need to handle money with varying rules, such as different currencies, rounding strategies, or precision settings. Here are some real-world scenarios where named contexts would be beneficial:
- Multi-regional Applications: Applications that operate in multiple countries and need to handle different currencies and rounding rules.
- Financial Services: Companies that process transactions in multiple currencies with different rounding rules for different types of operations.
- Accounting Systems: Systems that need to maintain accounts in both local and reporting currencies.
- E-commerce Platforms: Platforms that sell to customers worldwide and need to calculate prices, taxes, and shipping costs in different currencies.
- Microservices Architecture: When different services might need different money handling configurations.
Named contexts provide flexibility and isolation, allowing different parts of an application to work with different money-handling rules simultaneously without interfering with each other.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.Extensions.Configuration.Abstractions (>= 5.0.0 && < 10.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 5.0.0 && < 10.0.0)
- Microsoft.Extensions.Options (>= 5.0.0 && < 10.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 5.0.0 && < 10.0.0)
- NodaMoney (>= 2.5.0)
-
.NETStandard 2.1
- Microsoft.Extensions.Configuration.Abstractions (>= 5.0.0 && < 10.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 5.0.0 && < 10.0.0)
- Microsoft.Extensions.Options (>= 5.0.0 && < 10.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 5.0.0 && < 10.0.0)
- NodaMoney (>= 2.5.0)
-
net8.0
- Microsoft.Extensions.Configuration.Abstractions (>= 5.0.0 && < 10.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 5.0.0 && < 10.0.0)
- Microsoft.Extensions.Options (>= 5.0.0 && < 10.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 5.0.0 && < 10.0.0)
- NodaMoney (>= 2.5.0)
-
net9.0
- Microsoft.Extensions.Configuration.Abstractions (>= 5.0.0 && < 10.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 5.0.0 && < 10.0.0)
- Microsoft.Extensions.Options (>= 5.0.0 && < 10.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 5.0.0 && < 10.0.0)
- NodaMoney (>= 2.5.0)
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 |
---|---|---|
2.5.0 | 125 | 9/24/2025 |