Skip to content

Instantly share code, notes, and snippets.

@imumamaheswaran
Created November 17, 2020 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imumamaheswaran/5bf4fbc522cd355c3e837b84e6a3cd4e to your computer and use it in GitHub Desktop.
Save imumamaheswaran/5bf4fbc522cd355c3e837b84e6a3cd4e to your computer and use it in GitHub Desktop.
SRP code before refactoring
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace SalesProcessor
{
public class TradeProcessor
{
public void ProcessTrades(Stream stream)
{
// read rows
var lines = new List<string>();
using (var reader = new StreamReader(stream))
{
string line;
while ((line = reader.ReadLine()) != null)
{
lines.Add(line);
}
}
var trades = new List<TradeRecord>();
var lineCount = 1;
foreach (var fields in lines.Select(line => line.Split(new[] { ',' })))
{
if (fields.Length != 3)
{
Console.WriteLine("WARN: Line {0} malformed. Only {1} field(s) found.", lineCount, fields.Length);
continue;
}
if (fields[0].Length != 6)
{
Console.WriteLine("WARN: Trade currencies on line {0} malformed: '{1}'", lineCount, fields[0]);
continue;
}
if (!int.TryParse(fields[1], out var tradeAmount))
{
Console.WriteLine("WARN: Trade amount on line {0} not a valid integer: '{1}'", lineCount, fields[1]);
}
if (!decimal.TryParse(fields[2], out var tradePrice))
{
Console.WriteLine("WARN: Trade price on line {0} not a valid decimal: '{1}'", lineCount, fields[2]);
}
var sourceCurrencyCode = fields[0].Substring(0, 3);
var destinationCurrencyCode = fields[0].Substring(3, 3);
// calculate values
var trade = new TradeRecord
{
SourceCurrency = sourceCurrencyCode,
DestinationCurrency = destinationCurrencyCode,
Lots = tradeAmount / LotSize,
Price = tradePrice
};
trades.Add(trade);
lineCount++;
}
using (var connection = new System.Data.SqlClient.SqlConnection("Data Source=(local);Initial Catalog=TradeDatabase;Integrated Security=True;"))
{
connection.Open();
using (var transaction = connection.BeginTransaction())
{
foreach (var trade in trades)
{
var command = connection.CreateCommand();
command.Transaction = transaction;
command.CommandType = System.Data.CommandType.StoredProcedure;
command.CommandText = "dbo.insert_trade";
command.Parameters.AddWithValue("@sourceCurrency", trade.SourceCurrency);
command.Parameters.AddWithValue("@destinationCurrency", trade.DestinationCurrency);
command.Parameters.AddWithValue("@lots", trade.Lots);
command.Parameters.AddWithValue("@price", trade.Price);
command.ExecuteNonQuery();
}
transaction.Commit();
}
connection.Close();
}
Console.WriteLine("INFO: {0} trades processed", trades.Count);
}
private static float LotSize = 100000f;
}
internal class TradeRecord
{
internal string DestinationCurrency;
internal float Lots;
internal decimal Price;
internal string SourceCurrency;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment