Overview
Exchange rates define the conversion ratio between two currencies. The Currency Exchange API manages exchange rates with strict validation, supports automatic updates from the Central Bank of Russia (CBR), and provides flexible rate management capabilities.ExchangeRate Model Structure
TheExchangeRate class represents a currency pair with its conversion rate:
CurrencyExchange.Core/Models/ExchangeRate.cs
Properties
Unique identifier for the exchange rate
The source currency for conversion
The destination currency for conversion
The conversion rate from base to target currency. Must be greater than 0 and less than or equal to 99,999,999.
Rate Constraints
Exchange rates must satisfy strict numeric constraints to prevent invalid conversions:Minimum Rate Constraint
Minimum Rate Constraint
Value: Greater than 0Exchange rates cannot be zero or negative. This prevents division by zero errors and ensures meaningful conversions.
Maximum Rate Constraint
Maximum Rate Constraint
Value: 99,999,999 or lessThis upper limit prevents overflow issues and constrains rates to realistic values.
Creating Exchange Rates
Exchange rates are created using the staticCreate method with automatic validation:
CurrencyExchange.Core/Models/ExchangeRate.cs
The validation method is called automatically within the
Create method, ensuring all exchange rates meet the required constraints.Automatic Updates from CBR
The Currency Exchange API includes a background service that automatically fetches and updates exchange rates from the Central Bank of Russia (CBR) web service.CBRExchangeRate Background Service
TheCBRExchangeRate class implements IHostedService to run as a background worker:
CurrencyExchange.Data/ExternalServices/Clients/CBRExchangeRate.cs
Service Configuration
Service Configuration
The background service is configured with a delay interval (in seconds) that determines how frequently rates are updated.Parameters:
scopeFactory: Service scope factory for dependency injectiondelaySeconds: Update interval in seconds
TimeSpan.Zero) and repeats at the configured interval.Fetching Rates from CBR
The service retrieves daily currency rates from the CBR SOAP API:CurrencyExchange.Data/ExternalServices/Clients/CBRExchangeRate.cs
Updating Existing Rates
The update process checks for existing currency pairs and updates their rates:CurrencyExchange.Data/ExternalServices/Clients/CBRExchangeRate.cs
The service only updates existing currency pairs. New pairs from CBR are not automatically added to the database.
Managing Exchange Rates
Checking Rate Existence
Before updating or inserting rates, verify they exist in the database:CurrencyExchange.Data/Repositories/ExchangeRatesRepository.cs
Updating Rates
Update existing exchange rates by currency codes:CurrencyExchange.Data/Repositories/ExchangeRatesRepository.cs
Same Currency Exchange Rate
When base and target currencies are identical, the API automatically returns a rate of 1.0:CurrencyExchange.Data/Repositories/ExchangeRatesRepository.cs
Best Practices
Monitor Background Updates
Configure the CBR update interval based on your application’s needs. More frequent updates increase accuracy but consume more resources.
Validate Before Insert
Always check if a currency pair already exists before inserting to avoid duplicate key errors.
Handle Update Failures
Implement proper error handling for CBR service failures, as network issues or API changes can cause update errors.