Table of Contents

PriorityFallbackStrategy Class

Definition

Namespace
Bodu.Financial.ExchangeRates.Caching
Assembly
Bodu.Financial.ExchangeRates.Caching.dll
Package
Bodu.Financial.ExchangeRates.Caching 1.0.0
Source
PriorityFallbackStrategy.cs

An IRateAggregationStrategy that returns the first successful result from the ordered candidates, giving deterministic, auditable fallback.

public sealed class PriorityFallbackStrategy : IRateAggregationStrategy
Inheritance
PriorityFallbackStrategy
Implements
Inherited Members
Extension Methods

Remarks

On every lookup the candidates are consulted in order and the first to satisfy the request wins, so a preferred provider's fallback-date hit beats a lower-priority provider's exact-date hit. This is the default strategy and the successor to the former CompositeDatedRateProvider.

using Bodu.Financial.ExchangeRates;
using Bodu.Financial.ExchangeRates.Caching;

// Children in priority order: the first source that can serve a pair wins,
// so a pair the primary does not quote falls through to the next source.
var aggregate = new AggregatingRateProvider(new[]
{
    new NamedDatedRateProvider("RBA", rbaProvider),
    new NamedDatedRateProvider("ECB", ecbProvider),
});

// Provenance names the child that answered.
RateLookupResult result = aggregate.GetRate("AUD", "USD", new DateOnly(2024, 3, 15));
var servedBy = result.Rate.Provider;

Properties

Instance

Gets the shared stateless instance of the strategy.

public static PriorityFallbackStrategy Instance { get; }

Property Value

PriorityFallbackStrategy

The singleton PriorityFallbackStrategy.

Methods

AggregateRange(string, string, DateOnly, DateOnly, IReadOnlyList<NamedDatedRateProvider>)

Combines the candidates' rates over the inclusive date range startDate to endDate synchronously.

public IReadOnlyList<ExchangeRate> AggregateRange(string fromIsoCode, string toIsoCode, DateOnly startDate, DateOnly endDate, IReadOnlyList<NamedDatedRateProvider> candidates)

Parameters

fromIsoCode string

The source-currency ISO code.

toIsoCode string

The destination-currency ISO code.

startDate DateOnly

The inclusive start of the range.

endDate DateOnly

The inclusive end of the range.

candidates IReadOnlyList<NamedDatedRateProvider>

The ordered candidate providers to combine.

Returns

IReadOnlyList<ExchangeRate>

The combined rates ordered by date, or an empty list when none are available.

Remarks

This member lets the aggregator's synchronous range surface stay synchronous rather than block on AggregateRangeAsync(string, string, DateOnly, DateOnly, IReadOnlyList<NamedDatedRateProvider>, CancellationToken). The default implementation blocks on the asynchronous overload as a compatibility fallback for strategies that supply only the asynchronous combination; the built-in strategies override it with a genuinely synchronous implementation. Override it whenever the candidates expose a synchronous range surface.

AggregateRangeAsync(string, string, DateOnly, DateOnly, IReadOnlyList<NamedDatedRateProvider>, CancellationToken)

Combines the candidates' rates over the inclusive date range startDate to endDate.

public ValueTask<IReadOnlyList<ExchangeRate>> AggregateRangeAsync(string fromIsoCode, string toIsoCode, DateOnly startDate, DateOnly endDate, IReadOnlyList<NamedDatedRateProvider> candidates, CancellationToken cancellationToken)

Parameters

fromIsoCode string

The source-currency ISO code.

toIsoCode string

The destination-currency ISO code.

startDate DateOnly

The inclusive start of the range.

endDate DateOnly

The inclusive end of the range.

candidates IReadOnlyList<NamedDatedRateProvider>

The ordered candidate providers to combine.

cancellationToken CancellationToken

A token to observe while awaiting the operation.

Returns

ValueTask<IReadOnlyList<ExchangeRate>>

The combined rates ordered by date, or an empty list when none are available.

TryAggregate(string, string, DateOnly, RateLookupOptions, IReadOnlyList<NamedDatedRateProvider>, out RateLookupResult)

Attempts to resolve a single-date rate from the supplied candidates.

public bool TryAggregate(string fromIsoCode, string toIsoCode, DateOnly date, RateLookupOptions options, IReadOnlyList<NamedDatedRateProvider> candidates, out RateLookupResult result)

Parameters

fromIsoCode string

The source-currency ISO code.

toIsoCode string

The destination-currency ISO code.

date DateOnly

The calendar date for which a rate is required.

options RateLookupOptions

The lookup rules to apply; never null.

candidates IReadOnlyList<NamedDatedRateProvider>

The ordered candidate providers to combine.

result RateLookupResult

When this method returns true, the resolved result.

Returns

bool

true when the candidates yielded a rate; otherwise false.

Applies to

ProductVersions
.NET8, 10