Skip to content

Instantly share code, notes, and snippets.

@zharro
Created December 29, 2016 12:53
Show Gist options
  • Save zharro/80b5aa849c277f2cd62e6064be3ccd87 to your computer and use it in GitHub Desktop.
Save zharro/80b5aa849c277f2cd62e6064be3ccd87 to your computer and use it in GitHub Desktop.
Separation of immutable core (ClearingManager) and mutable shell (ApplicationService and Persister)
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace ImmutableClearing
{
class ApplicationService
{
private readonly string _directoryName;
private readonly Persister _persister;
private readonly ClearingManager _clearingManager;
public ApplicationService(string directoryName)
{
_directoryName = directoryName;
_persister = new Persister();
_clearingManager = new ClearingManager();
}
public void DoClearing()
{
FileInfo fileInfo = new DirectoryInfo(_directoryName)
.GetFiles()
.OrderByDescending(x => x.LastWriteTime)
.First();
FileContent file = _persister.ReadFile(fileInfo.Name);
List<Authorization> authorizations = _persister.GetAllAuthorizations();
ClearingResult result = _clearingManager.ClearFile(file, authorizations);
_persister.ApplyChange(result);
}
}
}
namespace ImmutableClearing
{
class Authorization
{
public readonly long Rrn;
public readonly long CardId;
public readonly bool IsCleared;
public Authorization(long rrn, long cardId, bool isCleared)
{
Rrn = rrn;
CardId = cardId;
IsCleared = isCleared;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
namespace ImmutableClearing
{
class ClearingManager
{
public ClearingResult ClearFile(FileContent fileContent, List<Authorization> authorizations)
{
List<Transaction> transactions = ParseFile(fileContent.Content);
var clearedAuthorizations = authorizations
.Where(a => transactions.Any(t => t.CardId == a.CardId && t.Rrn == a.Rrn))
.Select(a => new Authorization(a.Rrn, a.CardId, true))
.ToList();
ClearingStatus fileStatus = ClearingStatus.Failed;
if (transactions.Count == clearedAuthorizations.Count)
{
fileStatus = ClearingStatus.FullyProcessed;
}
else if (clearedAuthorizations.Count > 0)
{
fileStatus = ClearingStatus.PartiallyProcessed;
}
return new ClearingResult(fileContent.FileName, fileStatus, clearedAuthorizations);
}
private List<Transaction> ParseFile(string[] content)
{
throw new NotImplementedException();
}
}
}
using System.Collections.Generic;
namespace ImmutableClearing
{
class ClearingResult
{
public readonly string FileName;
public readonly ClearingStatus Status;
public readonly List<Authorization> Authorizations;
public ClearingResult(string fileName, ClearingStatus status, List<Authorization> authorizations)
{
FileName = fileName;
Status = status;
Authorizations = authorizations;
}
}
enum ClearingStatus
{
FullyProcessed,
PartiallyProcessed,
Failed
}
}
namespace ImmutableClearing
{
class FileContent
{
public readonly string FileName;
public readonly string[] Content;
public FileContent(string fileName, string[] content)
{
FileName = fileName;
Content = content;
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
namespace ImmutableClearing
{
class Persister
{
public FileContent ReadFile(string fileName)
{
return new FileContent(fileName, File.ReadAllLines(fileName));
}
public List<Authorization> GetAllAuthorizations()
{
throw new NotImplementedException();
}
public void ApplyChange(ClearingResult result)
{
throw new NotImplementedException();
}
}
}
namespace ImmutableClearing
{
class Transaction
{
public readonly long Rrn;
public readonly long CardId;
public Transaction(long rrn, long cardId)
{
Rrn = rrn;
CardId = cardId;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment