Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I create Enumerable<Func<>> out of method instances
    primarykey
    data
    text
    <p>I am creating a rule set engine that looks kinda like a unit test framework.</p> <pre><code>[RuleSet(ContextA)] public class RuleSet1 { [Rule(TargetingA)] public Conclusion Rule1(SubjectA subject) { Create conclusion } [Rule(TargetingA)] public Conclusion Rule2(SubjectA subject) { Create conclusion } [Rule(TargetingB)] public Conclusion Rule3(SubjectB subject) { Create conclusion } } [RuleSet(ContextB)] public class RuleSet2 { [Rule(TargetingB)] public Conclusion Rule1(SubjectB subject) { Create conclusion } [Rule(TargetingA)] public Conclusion Rule2(SubjectA subject) { Create conclusion } [Rule(TargetingB)] public Conclusion Rule3(SubjectB subject) { Create conclusion } } public class Conclusion() { // Errorcode, Description and such } // contexts and targeting info are enums. </code></pre> <p>The goal is to create an extensible ruleset that doesn't alter the API from consumer POV while having good separation-of-concerns within the code files. Again: like a unit test framework.</p> <p>I am trying to create a library of these that expose the following API</p> <pre><code>public static class RuleEngine { public static IEnumerable&lt;IRuleSet&gt; RuleSets(contextFlags contexts) { { return from type in Assembly.GetExecutingAssembly().GetTypes() let attribute = type.GetCustomAttributes(typeof (RuleSetAttribute), true) .OfType&lt;RuleSetAttribute&gt;() .FirstOrDefault() where attribute != null select ?? I don't know how to convert the individual methods to Func's. } } } internal interface IRuleset { IEnumerable&lt;Func&lt;SubjectA, Conclusion&gt;&gt; SubjectARules { get; } IEnumerable&lt;Func&lt;SubjectB, Conclusion&gt;&gt; SubjectBRules { get; } } </code></pre> <p>...which allows consumers to simply use like this (using foreach instead of LINQ for readability in this example)</p> <pre><code>foreach (var ruleset in RuleEgine.RuleSets(context)) { foreach (var rule in ruleset.SubjectARules) { var conclusion = rule(myContextA); //handle the conclusion } } </code></pre> <p>Also, it would be very helpful if you could tell me how to get rid of "TargetingA" and "TargetingB" as RuleAttribute parameters and instead use reflection to inspect the parameter type of the decorated method directly. All the while maintaining the same simple external API.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload