Class ExchangeRateService
- Namespace
- Integrations.Application.Fx
- Assembly
- Integrations.Application.dll
Daily rates, locks and conversions.
Rates are stored as published (every currency against the provider's pivot) and cross rates are derived: USD→MXN = (USD→CAD) / (MXN→CAD). The rate for a business date is the latest publication on or before it.
Imports and new locks commit in their own DI scope (own DbContext and outbox), independent of the caller's unit of work: losing an insert race just discards that scope.
Locks: the first lock for a pair and date fixes the rate; every later lock for that pair and date returns the same row (enforced by a unique index), so all lines committed on one day share one rate.
public sealed class ExchangeRateService
- Inheritance
-
ExchangeRateService
- Inherited Members
Constructors
ExchangeRateService(IAppDbContext, IExchangeRateProvider, IServiceScopeFactory, IDistributedLock, ICallerContext, BusinessClock, IMemoryCache, IOptions<FxOptions>, ILogger<ExchangeRateService>)
Daily rates, locks and conversions.
Rates are stored as published (every currency against the provider's pivot) and cross rates are derived: USD→MXN = (USD→CAD) / (MXN→CAD). The rate for a business date is the latest publication on or before it.
Imports and new locks commit in their own DI scope (own DbContext and outbox), independent of the caller's unit of work: losing an insert race just discards that scope.
Locks: the first lock for a pair and date fixes the rate; every later lock for that pair and date returns the same row (enforced by a unique index), so all lines committed on one day share one rate.
public ExchangeRateService(IAppDbContext db, IExchangeRateProvider provider, IServiceScopeFactory scopes, IDistributedLock distributedLock, ICallerContext caller, BusinessClock clock, IMemoryCache cache, IOptions<FxOptions> options, ILogger<ExchangeRateService> logger)
Parameters
dbIAppDbContextproviderIExchangeRateProviderscopesIServiceScopeFactorydistributedLockIDistributedLockcallerICallerContextclockBusinessClockcacheIMemoryCacheoptionsIOptions<FxOptions>loggerILogger<ExchangeRateService>
Fields
RefreshThrottle
How often a missing day's rate is fetched on demand for the same dates.
public static readonly TimeSpan RefreshThrottle
Field Value
Methods
ConvertAsync(ConvertRequest, CancellationToken)
Converts an amount at a lock's rate, or at the daily rate for a date; rounds only at the end.
public Task<ConvertResponse> ConvertAsync(ConvertRequest request, CancellationToken ct)
Parameters
requestConvertRequestctCancellationToken
Returns
GetLockAsync(Guid, CancellationToken)
A lock by id.
public Task<RateLockDto> GetLockAsync(Guid lockId, CancellationToken ct)
Parameters
lockIdGuidctCancellationToken
Returns
GetRateAsync(string, string, DateOnly?, CancellationToken)
Daily rate for the pair on date (default today), falling back to the latest prior business day.
public Task<ExchangeRateDto> GetRateAsync(string from, string to, DateOnly? date, CancellationToken ct)
Parameters
fromstringtostringdateDateOnly?ctCancellationToken
Returns
ImportAsync(DateOnly, DateOnly, CancellationToken)
Fetches and stores publications for the range. Existing observations are never overwritten.
public Task<ImportRatesResponse> ImportAsync(DateOnly from, DateOnly to, CancellationToken ct)
Parameters
fromDateOnlytoDateOnlyctCancellationToken
Returns
LockAsync(string, string, DateOnly?, CancellationToken)
Locks the daily rate. Idempotent per pair and rate date: every line committed the same day shares one lock.
public Task<(RateLockDto Lock, bool Created)> LockAsync(string from, string to, DateOnly? date, CancellationToken ct)
Parameters
fromstringtostringdateDateOnly?ctCancellationToken
Returns
- Task<(RateLockDto Lock, bool Created)>