PriorityFallbackStrategy Class
Definition
- Namespace
- Bodu.Financial.ExchangeRates.Caching
- Assembly
- Bodu.Financial.ExchangeRates.Caching.dll
- Package
- Bodu.Financial.ExchangeRates.Caching 1.0.0
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
fromIsoCodestringThe source-currency ISO code.
toIsoCodestringThe destination-currency ISO code.
startDateDateOnlyThe inclusive start of the range.
endDateDateOnlyThe inclusive end of the range.
candidatesIReadOnlyList<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
fromIsoCodestringThe source-currency ISO code.
toIsoCodestringThe destination-currency ISO code.
startDateDateOnlyThe inclusive start of the range.
endDateDateOnlyThe inclusive end of the range.
candidatesIReadOnlyList<NamedDatedRateProvider>The ordered candidate providers to combine.
cancellationTokenCancellationTokenA 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
fromIsoCodestringThe source-currency ISO code.
toIsoCodestringThe destination-currency ISO code.
dateDateOnlyThe calendar date for which a rate is required.
optionsRateLookupOptionsThe lookup rules to apply; never null.
candidatesIReadOnlyList<NamedDatedRateProvider>The ordered candidate providers to combine.
resultRateLookupResultWhen this method returns true, the resolved result.
Returns
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |