Skip to content

Instantly share code, notes, and snippets.

@ralfw
Last active August 29, 2015 14:14
Show Gist options
  • Save ralfw/e9d950ae1369141eeeda to your computer and use it in GitHub Desktop.
Save ralfw/e9d950ae1369141eeeda to your computer and use it in GitHub Desktop.
Inline flows
// flow defined inline
public ExportReport Export(DateTime fromMonth, DateTime toMonth) {
// create month range
// get balance sheets for month range
// extract transaction items
// create filename
// export
// create export report
}
// flow implemented inline
public ExportReport Export(DateTime fromMonth, DateTime toMonth) {
// create month range
if (toMonth == DateTime.MinValue) toMonth = fromMonth;
fromMonth = fromMonth.ToMonth ();
toMonth = toMonth.ToMonth ();
if (toMonth < fromMonth) {
var t = toMonth;
toMonth = fromMonth;
fromMonth = t;
}
var months = new List<DateTime> ();
var currMonth = fromMonth;
while (currMonth <= toMonth) {
months.Add (currMonth);
currMonth = currMonth.AddMonths (1);
}
// get balance sheets for month range
var allTx = this.repo.Load_all_transactions ();
var cb = this.cashbookFactory (allTx.ToArray());
var balanceSheets = months.Select (m => cb [m]);
// extract transaction items
var txItems = balanceSheets.SelectMany (bs => bs.TransactionItems).ToArray();
// create filename
var filename = string.Format ("cashbook-{0:yyyyMM}-{1:yyyyMM}.csv", fromMonth, toMonth);
// write items to file
using(var sw = new StreamWriter(filename)) {
foreach(var txi in txItems)
sw.WriteLine("{0:d};\"{1}\";{2:f}", txi.TransactionDate, txi.Description, txi.Value);
}
return new ExportReport{ Filename = filename, NumberOfTransactions = txItems.Length };
}
// flow implemented as true integration
public ExportReport Export(DateTime fromMonth, DateTime toMonth) {
var allTx = this.repo.Load_all_transactions ().ToArray();
var cashbook = this.cashbookFactory (allTx);
var months = fromMonth.ExtendTo (toMonth);
var txItems = cashbook.Get_balance_sheet_items_in_month_range (months);
var filepath = string.Format ("cashbook-{0:yyyyMM}-{1:yyyyMM}.csv", fromMonth, toMonth);
this.csvProvider.Export (txItems, filepath);
return new ExportReport{ Filename = filepath, NumberOfTransactions = txItems.Length };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment