Created
December 29, 2016 12:53
-
-
Save zharro/80b5aa849c277f2cd62e6064be3ccd87 to your computer and use it in GitHub Desktop.
Separation of immutable core (ClearingManager) and mutable shell (ApplicationService and Persister)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ImmutableClearing | |
{ | |
class FileContent | |
{ | |
public readonly string FileName; | |
public readonly string[] Content; | |
public FileContent(string fileName, string[] content) | |
{ | |
FileName = fileName; | |
Content = content; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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