static void DelegateExample() { // Create EngineAttributes with Name var engineOptions = new EngineAttributes { Name = "MyEngine" }; // Instantiate the RulesEngine with option var myEngine = new RulesEngine(engineOptions); // Now add Lambda rule myEngine.AddRule(new Rule((c, o) => { Console.WriteLine("Hello, world"); return(true); })); // Next, add a delegate method myEngine.AddRule(new Rule(ruleDelegate)); myEngine.RunAllRules(); // Output: // Hello, world // ruleDelegate called }
static void RuleClassExample() { /** * Create the component to initialize the RulesEngine * We don't HAVE to have a component, the rules can still run */ var bigB = new Customer { ID = 1, Name = "Amitabh", Birthdate = new DateTime(1942, 10, 11), FirstPurchase = new DateTime(2005, 1, 2), }; /** * Create an output object for Rules Engine * We don't HAVE to have an output object */ var discount = new CustomerDiscount(); // Create Engine Attributes var engineAttributes = new EngineAttributes { Component = bigB, Output = discount }; // We can add individual rules or an array of rules var rulesArray = new Rule[] { new Rule(seniorCitizenDiscount, ruleName: "Senior") }; // Start the engine with attributes and initial rules list var engine = new RulesEngine(engineAttributes, rulesArray); // We are adding multiple rules based on the same class, so we must name them explicitly engine.AddRule(new Rule(new LoyaltyDiscount(1, 5), ruleName: "L1")); engine.AddRule(new Rule(new LoyaltyDiscount(5, 8), ruleName: "L2")); engine.AddRule(new Rule(new LoyaltyDiscount(10, 10), ruleName: "L3")); Console.WriteLine(engine.RunAllRules()); }