IRateAggregationStrategy Interface
Definition
- Namespace
- Bodu.Financial.ExchangeRates.Caching
- Assembly
- Bodu.Financial.ExchangeRates.Caching.dll
- Package
- Bodu.Financial.ExchangeRates.Caching 1.0.0
Decides how an AggregatingRateProvider combines the results of an ordered set of candidate providers into a single answer.
public interface IRateAggregationStrategy
- Extension Methods
Examples
// A custom strategy that prefers the last candidate able to resolve (reverse priority).
public sealed class LastAvailableStrategy : IRateAggregationStrategy
{
public bool TryAggregate(string fromIsoCode, string toIsoCode, DateOnly date,
RateLookupOptions options, IReadOnlyList<NamedDatedRateProvider> candidates,
out RateLookupResult result)
{
for (int i = candidates.Count - 1; i >= 0; i--)
{
if (candidates[i].Provider.TryGetRate(fromIsoCode, toIsoCode, date, options, out result))
return true;
}
result = default;
return false;
}
public async ValueTask<IReadOnlyList<ExchangeRate>> AggregateRangeAsync(string fromIsoCode, string toIsoCode,
DateOnly startDate, DateOnly endDate, IReadOnlyList<NamedDatedRateProvider> candidates,
CancellationToken cancellationToken)
{
for (int i = candidates.Count - 1; i >= 0; i--)
{
IReadOnlyList<ExchangeRate> rates = [.. await candidates[i].Provider
.GetRatesAsync(fromIsoCode, toIsoCode, startDate, endDate, cancellationToken)];
if (rates.Count > 0)
return rates;
}
return Array.Empty<ExchangeRate>();
}
}
Remarks
This is the extensibility seam of the aggregation design: ship strategies such as PriorityFallbackStrategy and AverageStrategy cover the common cases, and consumers can implement this interface to craft their own (weighted, median, first-non-stale, and so on). The aggregator resolves the candidate set and per-pair strategy, and never passes a null options value; same-currency identity is handled by the aggregator before the strategy is consulted.
Methods
AggregateRange(string, string, DateOnly, DateOnly, IReadOnlyList<NamedDatedRateProvider>)
Combines the candidates' rates over the inclusive date range startDate to
endDate synchronously.
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.
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.
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 |