Table of Contents

CurrencyResolution Class

Definition

Namespace
Bodu.Financial.Currencies
Assembly
Bodu.Financial.dll
Package
Bodu.Financial 1.0.0
Source
CurrencyResolution.cs

Supplies the ambient ICurrencyLookup that the runtime-tagged Money and CalculatedMoney consult when they resolve a currency by ISO code. The seam lets a host or test substitute a custom currency catalogue without threading a lookup dependency through every value-type construction, while leaving the default behaviour identical to a direct CurrencyRegistry lookup.

public static class CurrencyResolution
Inheritance
CurrencyResolution
Inherited Members

Remarks

Current resolves to the active scoped override, if any, otherwise the process-wide default. The default is the registry-backed CurrencyLookupService; replace it once at composition root via SetDefault(ICurrencyLookup) (the Bodu.Financial.DependencyInjection integration does this), or install a temporary, flow-scoped override for a test or a single logical operation via PushScoped(ICurrencyLookup).

The scoped override is stored in an AsyncLocal<T>, so it is isolated per asynchronous control flow and does not leak across parallel test cases.

using Bodu.Financial;
using Bodu.Financial.Currencies;

// A lookup restricted to the currencies this system settles in (a delegating
// ICurrencyLookup decorator over CurrencyResolution.Current, for example).
ICurrencyLookup restricted = new RestrictedCurrencyLookup(CurrencyResolution.Current, "AUD", "USD", "EUR");

using (CurrencyResolution.PushScoped(restricted))
{
    // Inside the scope, runtime Money resolves through the override:
    // unsupported ISO codes now fail parsing and metadata resolution.
    var accepted = Money.TryParse("THB 25.00", CultureInfo.InvariantCulture, out _);   // false
}

// Disposing the scope restores the previous lookup. At composition root, prefer
// SetDefault (or the DI package's provider.UseCurrencyResolution()) instead.

Properties

Current

Gets the currency lookup currently in effect: the active scoped override, or the process-wide default.

public static ICurrencyLookup Current { get; }

Property Value

ICurrencyLookup

The effective ICurrencyLookup.

Methods

PushScoped(ICurrencyLookup)

Installs lookup as a flow-scoped override until the returned token is disposed.

public static IDisposable PushScoped(ICurrencyLookup lookup)

Parameters

lookup ICurrencyLookup

The lookup to install for the current control flow.

Returns

IDisposable

A token that restores the previous override when disposed.

Exceptions

ArgumentNullException

lookup is null.

SetDefault(ICurrencyLookup)

Replaces the process-wide default currency lookup. Intended to be called once during application start-up.

public static void SetDefault(ICurrencyLookup lookup)

Parameters

lookup ICurrencyLookup

The lookup to install as the default.

Exceptions

ArgumentNullException

lookup is null.

Applies to

ProductVersions
.NET8, 10