Skip to main content

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

The ExchangeRate class represents a currency pair with its conversion rate:
CurrencyExchange.Core/Models/ExchangeRate.cs

Properties

Id
int
required
Unique identifier for the exchange rate
BaseCurrency
Currency
required
The source currency for conversion
TargetCurrency
Currency
required
The destination currency for conversion
Rate
float
required
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:
Value: Greater than 0Exchange rates cannot be zero or negative. This prevents division by zero errors and ensures meaningful conversions.
Attempting to create an exchange rate with a value of 0 or below will throw an ArgumentException.
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 static Create 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

The CBRExchangeRate class implements IHostedService to run as a background worker:
CurrencyExchange.Data/ExternalServices/Clients/CBRExchangeRate.cs
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 injection
  • delaySeconds: Update interval in seconds
The timer starts immediately (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
The service uses CultureInfo.InvariantCulture when parsing rates to ensure consistent decimal formatting regardless of server locale.

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
This automatic handling simplifies currency conversion logic when dealing with same-currency scenarios.

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.