AverageStrategy Class
Definition
- Namespace
- Bodu.Financial.ExchangeRates.Caching
- Assembly
- Bodu.Financial.ExchangeRates.Caching.dll
- Package
- Bodu.Financial.ExchangeRates.Caching 1.0.0
- Source
- AverageStrategy.cs
An IRateAggregationStrategy that returns the arithmetic mean of every candidate that can resolve the request, tagged with a synthetic provider label.
public sealed class AverageStrategy : IRateAggregationStrategy
- Inheritance
-
AverageStrategy
- Implements
- Inherited Members
- Extension Methods
Remarks
The mean is computed in decimal over the forward rates each candidate returns (each candidate has already resolved its own inverse, so the synthesized rate is never itself inverted) and is not pre-rounded; rounding is deferred to the money boundary. The synthesized observation is dated at the requested date, and its reported offset is the largest offset among the contributing candidates.
Averaging is most meaningful with Exact, where every contributor shares the requested date; under fallback resolutions the contributors may have resolved different dates. The range overload averages only the dates present in every contributing candidate (an inner join by date).
The synthesized mean is an analytical, composite value, not an authoritative observation: it can differ from every contributor and so may equal a rate that no source actually published, and under a fallback resolution it can blend observations the contributors resolved on different dates. It is well suited to smoothing or cross-source comparison, but a consumer that needs a rate a specific source actually published - for tax, accounting, audit, or other compliance use - should prefer a single source (for example through PriorityFallbackStrategy or per-pair routing) rather than this strategy.
using Bodu.Financial.ExchangeRates;
using Bodu.Financial.ExchangeRates.Caching;
var aggregate = new AggregatingRateProvider(
new[]
{
new NamedDatedRateProvider("BankA", bankA),
new NamedDatedRateProvider("BankB", bankB),
},
new RateAggregationOptions { DefaultStrategy = new AverageStrategy() });
// The mean of every contributor, under the synthetic "Average" provider label.
RateLookupResult averaged = aggregate.GetRate("AUD", "USD", new DateOnly(2024, 3, 15));
Constructors
AverageStrategy(string)
Initializes a new instance of the AverageStrategy class.
public AverageStrategy(string providerLabel = "Average")
Parameters
providerLabelstringThe provider label applied to each synthesized average rate.
Exceptions
- ArgumentNullException
Thrown when
providerLabelis null.- ArgumentException
Thrown when
providerLabelis empty or white space.
Fields
DefaultProviderLabel
The default provider label applied to a synthesized average rate.
public const string DefaultProviderLabel = "Average"
Field Value
Properties
ProviderLabel
Gets the provider label applied to each synthesized average rate.
public string ProviderLabel { get; }
Property Value
- string
The synthetic provider label.
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 |