Skip to content

Instantly share code, notes, and snippets.

Created February 2, 2017 18:55
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 anonymous/71d7207d2e4e326e482816eb7bc83ba6 to your computer and use it in GitHub Desktop.
Save anonymous/71d7207d2e4e326e482816eb7bc83ba6 to your computer and use it in GitHub Desktop.
Memento - An example
public class CAMemberServiceCredit
{
public string MemberSeparator { get; private set; }
// ReoccurCharge 1 in the format stored in database
public string ReoccurChrg1 { get; private set; }
// ReoccurCharge 2 in the format stored in database
public string ReoccurChrg2 { get; private set; }
// ReoccurCharge 3 in the format stored in database
public string ReoccurChrg3 { get; private set; }
private static readonly BcdConverter _bcdConverter = new BcdConverter(precision: 5);
#region parsed fields
public string Code1
{
get
{
if (string.IsNullOrWhiteSpace(ReoccurChrg1) || ReoccurChrg1.Length != 6)
throw new InvalidOperationException("ReoccuChrg1 is not in correct format: '" + ReoccurChrg1 + "'");
return ReoccurChrg1.Substring(0, 2);
}
set
{
if (string.IsNullOrWhiteSpace(ReoccurChrg1) || ReoccurChrg1.Length != 6)
throw new InvalidOperationException("ReoccurChrg1 is not in correct format: '" + ReoccurChrg1 + "'");
if (string.IsNullOrWhiteSpace(value) || value.Length != 2)
throw new InvalidOperationException("Invalid value: '" + value + "'");
ReoccurChrg1 = value + ReoccurChrg1.Substring(2, 4);
}
}
public string Code2
{
get
{
if (string.IsNullOrWhiteSpace(ReoccurChrg2) || ReoccurChrg2.Length != 6)
throw new InvalidOperationException("ReoccuChrg1 is not in correct format: '" + ReoccurChrg2 + "'");
return ReoccurChrg2.Substring(0, 2);
}
set
{
if (string.IsNullOrWhiteSpace(ReoccurChrg2) || ReoccurChrg2.Length != 6)
throw new InvalidOperationException("ReoccurChrg2 is not in correct format: '" + ReoccurChrg2 + "'");
if (string.IsNullOrWhiteSpace(value) || value.Length != 2)
throw new InvalidOperationException("Invalid value: '" + value + "'");
ReoccurChrg2 = value + ReoccurChrg2.Substring(2, 4);
}
}
public string Code3
{
get
{
if (string.IsNullOrWhiteSpace(ReoccurChrg3) || ReoccurChrg3.Length != 6)
throw new InvalidOperationException("ReoccuChrg1 is not in correct format: '" + ReoccurChrg3 + "'");
return ReoccurChrg3.Substring(0, 2);
}
set
{
if (string.IsNullOrWhiteSpace(ReoccurChrg3) || ReoccurChrg3.Length != 6)
throw new InvalidOperationException("ReoccurChrg3 is not in correct format: '" + ReoccurChrg3 + "'");
if (string.IsNullOrWhiteSpace(value) || value.Length != 2)
throw new InvalidOperationException("Invalid value: '" + value + "'");
ReoccurChrg3 = value + ReoccurChrg3.Substring(2, 4);
}
}
public decimal ReoccurAmt1
{
get
{
if (string.IsNullOrWhiteSpace(ReoccurChrg1) || ReoccurChrg1.Length != 6)
throw new InvalidOperationException("ReoccuChrg1 is not in correct format: '" + ReoccurChrg1 + "'");
return _bcdConverter.UnpackAmount(ReoccurChrg1.Substring(2, 4));
}
set
{
if (string.IsNullOrWhiteSpace(ReoccurChrg1) || ReoccurChrg1.Length != 6)
throw new InvalidOperationException("ReoccurChrg1 is not in correct format: '" + ReoccurChrg1 + "'");
ReoccurChrg1 = Code1 + _bcdConverter.PackAmount(value);
if (ReoccurChrg1.Length != 6)
throw new InvalidOperationException("ReoccurChrg1 length has to be 6. It is: " + ReoccurChrg1.Length);
}
}
public decimal ReoccurAmt2
{
get
{
if (string.IsNullOrWhiteSpace(ReoccurChrg1) || ReoccurChrg1.Length != 6)
throw new InvalidOperationException("ReoccurChrg1 is not in correct format: '" + ReoccurChrg1 + "'");
return _bcdConverter.UnpackAmount(ReoccurChrg2.Substring(2, 4));
}
set
{
if (string.IsNullOrWhiteSpace(ReoccurChrg1) || ReoccurChrg1.Length != 6)
throw new InvalidOperationException("ReoccurChrg1 is not in correct format: '" + ReoccurChrg1 + "'");
ReoccurChrg2 = Code2 + _bcdConverter.PackAmount(value);
}
}
public decimal ReoccurAmt3
{
get
{
if (string.IsNullOrWhiteSpace(ReoccurChrg3) || ReoccurChrg3.Length != 6)
throw new InvalidOperationException("ReoccurChrg3 is not in correct format: '" + ReoccurChrg3 + "'");
return _bcdConverter.UnpackAmount(ReoccurChrg3.Substring(2, 4));
}
set
{
if (string.IsNullOrWhiteSpace(ReoccurChrg3) || ReoccurChrg3.Length != 6)
throw new InvalidOperationException("ReoccurChrg3 is not in correct format: '" + ReoccurChrg3 + "'");
ReoccurChrg3 = Code3 + _bcdConverter.PackAmount(value);
}
}
#endregion
public CAAuditEntry RecordPaymentForPastDues(decimal amount, string chargeCode)
{
Debug.Assert(string.IsNullOrWhiteSpace(chargeCode) == false);
CAMemberServicePaymentMemento memento = new CAMemberServicePaymentMemento(this);
if (amount == 0)
return null;
if (amount < 0)
throw new ArgumentOutOfRangeException("Negative credits cannot be posted to MemberDetl: " + amount);
// Note: Amount should be deducted
if (chargeCode == Code1)
{
ReoccurAmt1 -= amount;
}
else if (chargeCode == Code2)
{
ReoccurAmt2 -= amount;
}
else if (chargeCode == Code3)
{
ReoccurAmt3 -= amount;
}
else if (Code1 == "00" && ReoccurAmt1 == 0)
{
ReoccurAmt1 = amount;
Code1 = chargeCode;
}
else if (Code2 == "00" && ReoccurAmt2 == 0)
{
ReoccurAmt2 = amount;
Code2 = chargeCode;
}
else if (Code3 == "00" && ReoccurAmt3 == 0)
{
ReoccurAmt3 = amount;
Code3 = chargeCode;
}
else
{
throw new InvalidOperationException(string.Format(
"Cannot credit ${0} to MemberSeparator {1} " +
" because all three ReoccurChrg fields are populated with other charges. Namely, {0}, {1}, {2}",
Code1,
Code2,
Code3));
}
return memento.GetAuditEntryForChanges(this);
}
private class CAMemberServicePaymentMemento
{
private CAMemberServiceCredit _original;
public CAMemberServicePaymentMemento(CAMemberServiceCredit original)
{
_original = original.MemberwiseClone() as CAMemberServiceCredit;
}
public CAAuditEntry GetAuditEntryForChanges(CAMemberServiceCredit modified)
{
if (_original.MemberSeparator != modified.MemberSeparator)
throw new InvalidOperationException(
string.Format("Cannot audit entries related to different member seps. Original:{0} modified:{1}",
_original.MemberSeparator,
modified.MemberSeparator));
var entry = CAAuditEntry.CreateNewAuditEntryForPastDuesRecovery(_original.MemberSeparator);
if (_original.ReoccurChrg1 != modified.ReoccurChrg1)
{
entry.CAField = ReOccurCharge.REOCCURCHG_001.Description();
entry.OldValue = _original.ReoccurChrg1;
entry.NewValue = modified.ReoccurChrg1;
}
else if (_original.ReoccurChrg2 != modified.ReoccurChrg2)
{
entry.CAField = ReOccurCharge.REOCCURCHG_002.Description();
entry.OldValue = _original.ReoccurChrg2;
entry.NewValue = modified.ReoccurChrg2;
}
else if (_original.ReoccurChrg3 != modified.ReoccurChrg3)
{
entry.CAField = ReOccurCharge.REOCCURCHG_003.Description();
entry.OldValue = _original.ReoccurChrg3;
entry.NewValue = modified.ReoccurChrg3;
}
return entry.CAField == null ? null : entry;
}
}
public override string ToString()
{
return string.Format("MemberSeparator: {0} ReoccurChrg1: {1} ReoccurChrg2:{2} ReoccurChrg3:{3} ", MemberSeparator, ReoccurChrg1, ReoccurChrg2, ReoccurChrg3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment