This file contains hidden or 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
| public class RuleEngine<T> where T : class, new() | |
| { | |
| public Dictionary<Type, object> SideEffect { get; set; } | |
| = new Dictionary<Type, object>(); | |
| public RuleResultWithSideEffect FirstFailsWithSideEffect( | |
| IList<Func<T, RuleResultWithSideEffect>> sequentialRules, T ruleCtx) | |
| { | |
| RuleResultWithSideEffect firstFailed = null; |
This file contains hidden or 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
| public class RuleResultWithSideEffect | |
| { | |
| public bool IsValid { get; set; } | |
| public string Reason { get; set; } | |
| public (Type type, object val) SideEffect { get; set; } | |
| } |
This file contains hidden or 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
| public static RuleResult FirstFails<T> ( | |
| this IList<Func<T, RuleResult>> sequentialRules, T ruleCtx) | |
| where T : class, new() | |
| { | |
| return new RuleEngine<T>().FirstFails(sequentialRules, ruleCtx); | |
| } |
This file contains hidden or 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
| public class MarsService | |
| { | |
| public RuleResult ApplyCreditCard() | |
| { | |
| var ctx = new ClusterRuleCtx(); | |
| return new RuleCluster().Rules.FirstFails(ctx); | |
| } | |
| } |
This file contains hidden or 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
| public class RuleCluster | |
| { | |
| public List<Func<ClusterRuleCtx, RuleResult>> Rules { get; set; } | |
| public RuleCluster() | |
| { | |
| Rules = new List<Func<ClusterRuleCtx, RuleResult>>() {}; | |
| Rules.AddRange(new EligibilityRule().Rules); |
This file contains hidden or 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
| public class MarsRule | |
| { | |
| public List<Func<ClusterRuleCtx, RuleResult>> Rules => | |
| new List<Func<ClusterRuleCtx, RuleResult>> { | |
| NumOfYearsOnMarsRule, | |
| AddressRule_CSharp_8 | |
| }; | |
| public RuleResult NumOfYearsOnMarsRule(ClusterRuleCtx ctx) | |
| { |
This file contains hidden or 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
| public class EligibilityRule | |
| { | |
| public List<Func<ClusterRuleCtx, RuleResult>> Rules => | |
| new List<Func<ClusterRuleCtx, RuleResult>> { | |
| TermsConditionsRule, | |
| AgeRule | |
| }; | |
| public RuleResult TermsConditionsRule(ClusterRuleCtx ctx) | |
| { |
This file contains hidden or 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
| public class ClusterRuleCtx | |
| { | |
| public Application App { get; set; } | |
| public List<Reference> References { get; set; } | |
| } |
This file contains hidden or 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
| static async Task ProcessTaskAsTheyCompleteAsyncAndProcessingInOneUnit() | |
| { | |
| var resultContext = new Result(); | |
| Func<Task> t1 = async () => | |
| { | |
| var r = await APICall1(); | |
| resultContext.Result1 = ProcessResult1(r); | |
| }; |
This file contains hidden or 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
| static async Task ProcessTasksAsTheyCompleteV1() | |
| { | |
| var result = new Result(); | |
| async Task<Action> t1() | |
| { | |
| var r = await APICall1(); | |
| return () => result.Result1 = ProcessResult1(r); | |
| } |