Table of Contents

IRateAggregationStrategy Interface

Definition

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

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

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.

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.

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