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
builderIFinancialServiceBuilderThe financial service builder.
configureAction<IAggregatedRateBuilder>A callback that adds the cached children and configures routing and strategy.
configurationIConfigurationAn optional configuration root or section bound into the shared CachingRateOptions.
sectionNamestringThe configuration section name. Defaults to
Financial:RateCache.configureCacheAction<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
builderorconfigureis null.- ArgumentException
Thrown when
sectionNameis 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
builderIFinancialServiceBuilderThe financial service builder.
providerNamestringThe name the source's rates are cached under.
configurationIConfigurationAn optional configuration root or section bound into CachingRateOptions.
sectionNamestringThe configuration section name. Defaults to
Financial:RateCache.configureAction<CachingRateOptions>An optional callback applied after configuration binding.
cacheFactoryFunc<IServiceProvider, string, IRateCache>An optional factory producing the IRateCache from the service provider and the provider name. When null, a default TomlFileRateCache bound to
providerNameunder the options'CacheDirectoryis 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
TProviderThe 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
builderis null.- ArgumentException
Thrown when
providerNameorsectionNameis empty or white space.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |