Skip to content

Repository files navigation

πŸ’± Exchange Rate Solution

Multi-layered .NET solution for fetching, processing, and synchronizing exchange rate data using staging tables and SQL MERGE

.NET Framework SQL Server Architecture

🎯 Overview

ExchangeRateSolution is a modular, layered .NET solution designed for working with exchange rate data. The project includes domain layer, data access layer, service layer, and Windows Service component.

Key Goals

  • Fetch exchange rate data from external sources
  • Write to staging tables for validation
  • Safely transfer to main tables using MERGE
  • Automated background data synchronization
  • Maintainable layered architecture

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         ExchangeTransferWinService                  β”‚
β”‚         (Windows Service - Timer)                   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                   β”‚
                   ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         ExchangeRate.Service                        β”‚
β”‚         (Business Logic)                            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                   β”‚
                   ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         ExchangeRate.Data                           β”‚
β”‚         (Repository Pattern)                        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                   β”‚
                   ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         ExchangeRate.Core                           β”‚
β”‚         (Domain Models & Interfaces)                β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

✨ Features

  • βœ… Layered Architecture - Clean separation of concerns
  • βœ… Repository Pattern - Abstracted data access
  • βœ… Staging Tables - Safe data validation before merge
  • βœ… SQL MERGE - Automatic INSERT/UPDATE logic
  • βœ… Transaction Support - Rollback on errors
  • βœ… Bulk Operations - High-performance data transfer
  • βœ… Windows Service - Automated background processing
  • βœ… Testable - Dependency injection ready

πŸ“¦ Project Structure

ExchangeRateSolution/
β”‚
β”œβ”€β”€ ExchangeRate.Core/              # Domain Layer
β”‚   β”œβ”€β”€ Entities/                   # Domain models
β”‚   β”œβ”€β”€ Interfaces/                 # Repository interfaces
β”‚   └── DTOs/                       # Data transfer objects
β”‚
β”œβ”€β”€ ExchangeRate.Data/              # Data Access Layer
β”‚   β”œβ”€β”€ Repositories/               # Repository implementations
β”‚   β”œβ”€β”€ Context/                    # Database context
β”‚   └── Migrations/                 # Database migrations
β”‚
β”œβ”€β”€ ExchangeRate.Service/           # Service Layer
β”‚   β”œβ”€β”€ Services/                   # Business logic
β”‚   β”œβ”€β”€ Validators/                 # Data validation
β”‚   └── Mappers/                    # Object mapping
β”‚
└── ExchangeTransferWinService/     # Windows Service
    β”œβ”€β”€ Worker.cs                   # Service worker
    └── App.config                  # Configuration

πŸ”„ Data Flow

External API
    ↓
[Fetch Rates]
    ↓
ExchangeRate_Staging (Temp)
    ↓
[Validation]
    ↓
[SQL MERGE]
    ↓
ExchangeRate (Main Table)

πŸš€ Installation

Prerequisites

  • .NET Framework 4.7.2+
  • SQL Server 2016+
  • Visual Studio 2019+
  • Administrator privileges (for Windows Service)

1. Database Setup

-- Create main table
CREATE TABLE ExchangeRate (
    Id INT PRIMARY KEY IDENTITY(1,1),
    CurrencyCode NVARCHAR(3) NOT NULL,
    Rate DECIMAL(18,6) NOT NULL,
    RateDate DATE NOT NULL,
    CreatedAt DATETIME NOT NULL DEFAULT GETDATE(),
    UpdatedAt DATETIME,
    CONSTRAINT UQ_Currency_Date UNIQUE (CurrencyCode, RateDate)
)

-- Create staging table
CREATE TABLE ExchangeRate_Staging (
    Id INT PRIMARY KEY IDENTITY(1,1),
    CurrencyCode NVARCHAR(3) NOT NULL,
    Rate DECIMAL(18,6) NOT NULL,
    RateDate DATE NOT NULL,
    CreatedAt DATETIME NOT NULL DEFAULT GETDATE()
)

-- Service settings
CREATE TABLE ServiceSettings (
    Id INT PRIMARY KEY IDENTITY(1,1),
    IntervalMinutes INT NOT NULL,
    IsActive BIT NOT NULL,
    ApiUrl NVARCHAR(500)
)

INSERT INTO ServiceSettings VALUES (60, 1, 'https://api.exchangerate.com/rates')

2. Configuration

Edit App.config:

<connectionStrings>
  <add name="ExchangeRateDb" 
       connectionString="Server=SERVER;Database=ExchangeRateDB;Integrated Security=true;" />
</connectionStrings>

<appSettings>
  <add key="ApiUrl" value="https://api.exchangerate.com/rates" />
  <add key="ApiKey" value="YOUR_API_KEY" />
</appSettings>

3. Build & Service Install

# Run CMD as Administrator
sc create ExchangeRateService binPath="C:\Services\ExchangeRateService\ExchangeTransferWinService.exe" start=auto
sc start ExchangeRateService

πŸ§ͺ Testing

-- Check staging table
SELECT * FROM ExchangeRate_Staging ORDER BY CreatedAt DESC

-- Check main table
SELECT * FROM ExchangeRate ORDER BY RateDate DESC, CurrencyCode

-- Verify sync
SELECT 
    CurrencyCode,
    Rate,
    RateDate,
    UpdatedAt
FROM ExchangeRate
WHERE RateDate = CAST(GETDATE() AS DATE)

βš™οΈ Configuration Options

-- Change fetch interval (2 hours)
UPDATE ServiceSettings SET IntervalMinutes = 120

-- Pause service
UPDATE ServiceSettings SET IsActive = 0

-- Resume service
UPDATE ServiceSettings SET IsActive = 1

-- Update API URL
UPDATE ServiceSettings SET ApiUrl = 'https://new-api.com/rates'

πŸ”§ Troubleshooting

Service Won't Start

eventvwr.msc β†’ Application logs

API Connection Failed

  • Check ApiUrl and ApiKey in config
  • Verify network connectivity
  • Check API rate limits

Data Not Syncing

-- Check staging table
SELECT COUNT(*) FROM ExchangeRate_Staging

-- Check for errors in logs
-- Review transaction rollbacks

Duplicate Key Error

-- Check existing records
SELECT CurrencyCode, RateDate, COUNT(*) 
FROM ExchangeRate 
GROUP BY CurrencyCode, RateDate 
HAVING COUNT(*) > 1

πŸ” Security

  • βœ… API keys in configuration (not hardcoded)
  • βœ… SQL parameterized queries (no injection risk)
  • βœ… Least privilege database access
  • βœ… Transaction rollback on errors
  • βœ… Encrypted connection strings (optional)

πŸ“ž Contact


About

Multi-layered .NET solution for automated exchange rate synchronization using staging tables and SQL MERGE. Features Windows Service for background processing, repository pattern for data access, and transaction-safe bulk operations.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages