Table of Contents

RateCachingExtensions Class

Definition

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

Provides fluent registration of caching and aggregating exchange-rate providers onto an IFinancialServiceBuilder.

public static class RateCachingExtensions
Inheritance
RateCachingExtensions
Inherited Members

Methods

AddAggregatedRateProvider(IFinancialServiceBuilder, Action<IAggregatedRateBuilder>, IConfiguration?, string, Action<CachingRateOptions>?)

Registers an AggregatingRateProvider that groups the cached children added through configure, resolvable as both IDatedRateProvider and the timeless IRateProvider. Each child is also registered as a keyed IDatedRateProvider so a specific source can be resolved by name.

public static IFinancialServiceBuilder AddAggregatedRateProvider(this IFinancialServiceBuilder builder, Action<IAggregatedRateBuilder> configure, IConfiguration? configuration = null, string sectionName = "Financial:RateCache", Action<CachingRateOptions>? configureCache = null)

Parameters

builder IFinancialServiceBuilder

The financial service builder.

configure Action<IAggregatedRateBuilder>

A callback that adds the cached children and configures routing and strategy.

configuration IConfiguration

An optional configuration root or section bound into the shared CachingRateOptions.

sectionName string

The configuration section name. Defaults to Financial:RateCache.

configureCache Action<CachingRateOptions>

An optional callback applied to the shared cache options after configuration binding.

Returns

IFinancialServiceBuilder

The builder, for chaining.

Examples

services.AddFinancialService()
        .AddAggregatedRateProvider(agg => agg
            .AddCachedChild<RbaRateProvider>("RBA")
            .AddCachedChild<EcbRateProvider>("ECB")
            .MapPair(new CurrencyPair(CurrencyCode.AUD, CurrencyCode.USD), "RBA", "ECB")
            .MapPair(new CurrencyPair(CurrencyCode.USD, CurrencyCode.GBP), "ECB", "RBA"));

// Resolve the aggregate, or a specific source by name:
var aggregate = provider.GetRequiredService<IDatedRateProvider>();
var rbaOnly = provider.GetRequiredKeyedService<IDatedRateProvider>("RBA");

Exceptions

ArgumentNullException

Thrown when builder or configure is null.

ArgumentException

Thrown when sectionName is empty or white space.

AddCachedRateProvider<TProvider>(IFinancialServiceBuilder, string, IConfiguration?, string, Action<CachingRateOptions>?, Func<IServiceProvider, string, IRateCache>?)

Registers a CachingRateProvider that wraps a single source TProvider over its own on-disk cache, resolvable as both IDatedRateProvider and the timeless IRateProvider.

public static IFinancialServiceBuilder AddCachedRateProvider<TProvider>(this IFinancialServiceBuilder builder, string providerName, IConfiguration? configuration = null, string sectionName = "Financial:RateCache", Action<CachingRateOptions>? configure = null, Func<IServiceProvider, string, IRateCache>? cacheFactory = null) where TProvider : class, IDatedRateProvider

Parameters

builder IFinancialServiceBuilder

The financial service builder.

providerName string

The name the source's rates are cached under.

configuration IConfiguration

An optional configuration root or section bound into CachingRateOptions.

sectionName string

The configuration section name. Defaults to Financial:RateCache.

configure Action<CachingRateOptions>

An optional callback applied after configuration binding.

cacheFactory Func<IServiceProvider, string, IRateCache>

An optional factory producing the IRateCache from the service provider and the provider name. When null, a default TomlFileRateCache bound to providerName under the options' CacheDirectory is used. Supply a factory to choose the storage structure - for example a JSON cache, a partitioned file layout, or a SQLite or distributed cache.

Returns

IFinancialServiceBuilder

The builder, for chaining.

Type Parameters

TProvider

The concrete source provider to cache.

Examples

services.AddFinancialService()
        .AddRbaExchangeRates(configuration)
        .AddCachedRateProvider<RbaRateProvider>("RBA", configuration,
            configure: o => o.DefaultExpiry = TimeSpan.FromHours(12));

// Consumers resolve IDatedRateProvider (or IRateProvider) and get cached lookups transparently.

Remarks

The source TProvider must already be registered - for example through its provider package's registration such as AddRbaExchangeRates. This method resolves the registered instance and wraps it in a caching decorator; it does not construct the source or its own dependencies (such as its HttpClient), so registering only the cache without the source fails when the provider is resolved.

Exceptions

ArgumentNullException

Thrown when builder is null.

ArgumentException

Thrown when providerName or sectionName is empty or white space.

Applies to

ProductVersions
.NET8, 10