Last active
August 5, 2016 15:31
-
-
Save davidsavagejr/d22ab80289a01656c96f6931744775ae to your computer and use it in GitHub Desktop.
Implementation of a validator using a simple strategy pattern
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; | |
namespace Sample | |
{ | |
public class SubLedger | |
{ | |
public string Subledger { get; set; } | |
public string SubledgerType { get; set; } | |
public string SubledgerJobType { get; set; } | |
public string ObjectAccount { get; set; } | |
public string JobType { get; set; } | |
public string LotStatus { get; set; } | |
public bool IsPosted { get; set; } | |
public bool SubledgerIsValidOption { get; set; } | |
public bool SubledgerIsLot { get; set; } | |
public bool SubledgerRequired { get; set; } | |
} | |
public class SomeImplementation | |
{ | |
public void SomeConsumer(SubLedger ledger) | |
{ | |
var validator = new SubledgerValidator(); | |
validator.Validate(ledger); | |
} | |
} | |
public class SubledgerValidator | |
{ | |
public class SubledgerRule | |
{ | |
private readonly Func<SubLedger, Result> _logic; | |
public SubledgerRule(Func<SubLedger, Result> logic) | |
{ | |
_logic = logic; | |
} | |
public Result IsValid(SubLedger subLedger) | |
{ | |
return _logic(subLedger); | |
} | |
public static SubledgerRule LedgerRequired = new SubledgerRule(l => | |
{ | |
if(string.IsNullOrEmpty(l.Subledger)) | |
return Result.Invalid("Subledger is required"); | |
return Result.Valid; | |
}); | |
public static SubledgerRule LedgerMustBeCommunityOrJobNumber = new SubledgerRule(l => | |
{ | |
if(new List<string> { "CM", "LD", "LT" }.Contains(l.JobType) | |
&& !new List<string> { "JB", "LJ", "CM", "LD", "LT" }.Contains(l.SubledgerJobType)) | |
return Result.Invalid("Subledger must be a community or job number"); | |
return Result.Valid; | |
}); | |
public static SubledgerRule LedgerMustBeAValidLot = new SubledgerRule(l => | |
{ | |
if (new List<string> { "CM", "LD", "LT" }.Contains(l.JobType) | |
&& l.ObjectAccount == "1670" | |
&& !l.SubledgerIsLot) | |
return Result.Invalid("Must be a valid lot"); | |
return Result.Valid; | |
}); | |
} | |
public List<SubledgerRule> Rules { get; } | |
public SubledgerValidator() | |
{ | |
Rules = new List<SubledgerRule> | |
{ | |
SubledgerRule.LedgerRequired, | |
SubledgerRule.LedgerMustBeCommunityOrJobNumber, | |
SubledgerRule.LedgerMustBeAValidLot | |
// add more | |
}; | |
} | |
public Result Validate(SubLedger subLedger) | |
{ | |
foreach (var rule in Rules) | |
{ | |
var result = rule.IsValid(subLedger); | |
if (!result.IsValid) | |
return result; | |
} | |
return Result.Valid; | |
} | |
} | |
public class Result | |
{ | |
public static Result Valid = new Result(true); | |
public static Result Invalid(string message) | |
{ | |
return new Result(false, message); | |
} | |
public Result(bool isValid, string message) | |
: this(isValid) | |
{ | |
Message = message; | |
} | |
private Result(bool isValid) | |
{ | |
IsValid = isValid; | |
} | |
public bool IsValid { get; private set; } | |
public string Message { get; private set; } | |
} | |
} |
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 Accounting.Data.JDE.Validators | |
{ | |
public class SubledgerValidator : IValidator | |
{ | |
public string Subledger { get; set; } | |
public string SubledgerType { get; set; } | |
public string SubledgerJobType { get; set; } | |
public string ObjectAccount { get; set; } | |
public string JobType { get; set; } | |
public string LotStatus { get; set; } | |
public bool IsPosted { get; set; } | |
public bool SubledgerIsValidOption { get; set; } | |
public bool SubledgerIsLot { get; set; } | |
public bool SubledgerRequired { get; set; } | |
public string IsValid() | |
{ | |
if (string.IsNullOrWhiteSpace(Subledger)) | |
{ | |
if (SubledgerRequired) | |
{ | |
return "Subledger is required"; | |
} | |
} | |
else if (new List<string> { "CM", "LD", "LT" }.Contains(JobType)) | |
{ | |
if (!new List<string> { "JB", "LJ", "CM", "LD", "LT" }.Contains(SubledgerJobType)) | |
{ | |
return "Subledger must be a community or job number"; | |
} | |
else if (ObjectAccount == "1670") | |
{ | |
if (!SubledgerIsLot) | |
{ | |
return "Must be a valid lot"; | |
} | |
else if (new List<string> { "Z", "X" }.Contains(LotStatus)) | |
{ | |
return "Lot canceled or closed"; | |
} | |
else if (IsPosted) | |
{ | |
return "Job has been posted"; | |
} | |
} | |
} | |
else if (new List<string> { "JB", "LJ" }.Contains(JobType)) | |
{ | |
if (SubledgerIsValidOption) | |
{ | |
return "Subledger must be a selected option"; | |
} | |
} | |
return string.Empty; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment