NodaMoney.DependencyInjection 2.5.0

dotnet add package NodaMoney.DependencyInjection --version 2.5.0
                    
NuGet\Install-Package NodaMoney.DependencyInjection -Version 2.5.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NodaMoney.DependencyInjection" Version="2.5.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NodaMoney.DependencyInjection" Version="2.5.0" />
                    
Directory.Packages.props
<PackageReference Include="NodaMoney.DependencyInjection" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NodaMoney.DependencyInjection --version 2.5.0
                    
#r "nuget: NodaMoney.DependencyInjection, 2.5.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package NodaMoney.DependencyInjection@2.5.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NodaMoney.DependencyInjection&version=2.5.0
                    
Install as a Cake Addin
#tool nuget:?package=NodaMoney.DependencyInjection&version=2.5.0
                    
Install as a Cake Tool

NodaMoney Dependency Injection Extensions

NuGet NuGet Pre-release NuGet CI

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:

  1. Multi-regional Applications: Applications that operate in multiple countries and need to handle different currencies and rounding rules.
  2. Financial Services: Companies that process transactions in multiple currencies with different rounding rules for different types of operations.
  3. Accounting Systems: Systems that need to maintain accounts in both local and reporting currencies.
  4. E-commerce Platforms: Platforms that sell to customers worldwide and need to calculate prices, taxes, and shipping costs in different currencies.
  5. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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