public Customer CreateCustomer(Name name, Gender gender, bool isPreferred = false) { var customer = new Customer(name, gender, isPreferred); var ruleEngine = RuleEngine <Customer> .CreateTypedRuleEngine(); ruleEngine.ExecuteRuleSet(customer, "CustomerCreationRules"); return(customer); }
public void Example2Test() { var subject = new Example2Subject { IsTrue = false }; var ruleEngine = RuleEngine <Example2Subject> .CreateTypedRuleEngine(); ruleEngine.ExecuteAllRules(subject); Assert.AreEqual("goodbye cruel world", subject.Greeting); }
public void Example1Test() { var subject = new Example1Subject { IsTrue = true }; var ruleEngine = RuleEngine <Example1Subject> .CreateTypedRuleEngine(); ruleEngine.ExecuteAllRules(subject); Assert.AreEqual("hello world", subject.Greeting); }
public void Example1Test() { var subject = new Example1Subject { Text = "Text" }; var ruleEngine = RuleEngine <Example1Subject> .CreateTypedRuleEngine(); var result = ruleEngine.ExecuteRuleSet(subject, "RuleSet1"); Assert.IsFalse(result.HasRuleViolation); }
public void Example1Test() { var subject = new Example1Subject { DueDate = DateTime.Today.AddDays(-1) }; var ruleEngine = RuleEngine <Example1Subject> .CreateTypedRuleEngine(); var results = ruleEngine.ExecuteAllRules(subject); Assert.IsFalse(results.HasRuleViolation); }
public void Example3Test() { var subject = new Example3Subject { FavoriteFruit = "Apple" }; var ruleEngine = RuleEngine <Example3Subject> .CreateTypedRuleEngine(); var result = ruleEngine.ExecuteAllRules(subject); // Since FavoriteFruit is in the list of approved fruits there is no rule violation Assert.IsFalse(result.HasRuleViolation); }
/// <summary> /// Creates the rule engine. /// </summary> /// <typeparam name="TSubject">The type of the subject.</typeparam> /// <param name="subject">The subject.</param> /// <param name="ruleSetName">Name of the rule set.</param> /// <returns>A <see cref="DomainRuleEngine"/></returns> public static DomainRuleEngine CreateRuleEngine <TSubject> (TSubject subject, string ruleSetName = null) { var ruleEngine = RuleEngine <TSubject> .CreateTypedRuleEngine(); var domainRuleEngine = new DomainRuleEngine { _ruleEngineContext = ruleSetName == null ? new RuleEngineContext <TSubject> (subject) : new RuleEngineContext <TSubject> (subject, new SelectAllRulesInRuleSetSelector(ruleSetName)), _executeRules = ruleEngine.ExecuteRules }; return(domainRuleEngine); }
/// <summary> /// Processes the single aggregate. /// </summary> /// <param name="dto">The dto to process.</param> /// <param name="entity">The entity.</param> /// <returns>A <see cref="System.Boolean"/></returns> protected override bool ProcessSingleAggregate(PatientDocumentDto dto, PatientDocument entity) { var result = MapProperties(dto, entity); //TODO: Why these rules are here? var ruleEngine = RuleEngine <PatientDocument> .CreateTypedRuleEngine(); var executionResults = ruleEngine.ExecuteRuleSet(entity, "SavePatientDocument"); result &= !executionResults.HasRuleViolation; dto.MapViolations(executionResults.RuleViolations); return(result); }
public void Example3Test() { var subject = new Example3Subject { IsTrue = true }; var ruleEngine = RuleEngine <Example3Subject> .CreateTypedRuleEngine(); var ruleEngineContext = new RuleEngineContext <Example3Subject>(subject); var example3ContextObject = new Example3ContextObject(); ruleEngineContext.WorkingMemory.AddContextObject(example3ContextObject); ruleEngine.ExecuteRules(ruleEngineContext); Assert.AreEqual("hello world", example3ContextObject.Greeting); }
public void Example2Test() { var subject = new Example2Subject { StartDate = new DateTime(2000, 1, 1), EndDate = new DateTime(1999, 1, 1) }; var ruleEngine = RuleEngine <Example2Subject> .CreateTypedRuleEngine(); var result = ruleEngine.ExecuteAllRules(subject); Assert.IsTrue(result.HasRuleViolation); var ruleViolation = result.RuleViolations.Single(); Assert.AreEqual(Resource.SectionB_StartDateMustComeAfterEndDate, ruleViolation.Message); }
public void RegisterForMaternitySale() { var ruleEngine = RuleEngine <Customer> .CreateTypedRuleEngine(); var result = ruleEngine.ExecuteRuleSet(this, "RegisterForMaternitySalesRules"); if (!result.HasRuleViolation) { var followUpService = ServiceLocator.Current.GetInstance <IPreferredCustomerService>(); followUpService.RegisterForMaternitySale(this); } else { DomainEvent.Raise(new RuleViolationEvent { RuleViolations = result.RuleViolations }); } }
public void Example2Test() { var subject = new Example2Subject { EmailAddress = "InvalidEmailAddress" }; var ruleEngine = RuleEngine <Example2Subject> .CreateTypedRuleEngine(); var result = ruleEngine.ExecuteAllRules(subject); // Since FavoriteFruit is not null but it isn't a valid email address Assert.IsTrue(result.HasRuleViolation); // The rule engine will let you know rule was violated var ruleViolation = result.RuleViolations.Single(); Assert.AreEqual("MustBeValidEmailAddress", ruleViolation.Rule.Name); }
public void Example5Test() { var subject = new Example5Subject { Address = new Example5Address { Street1 = "1 Elm Street", Street2 = "Apt C", City = "Orlando", State = "FL", Zipcode = "32801" } }; var ruleEngine = RuleEngine <Example5Subject> .CreateTypedRuleEngine(); var result = ruleEngine.ExecuteAllRules(subject); Assert.IsFalse(result.HasRuleViolation); }
public void Example1Test() { var subject = new Example1Subject { Text = null }; var ruleEngine = RuleEngine <Example1Subject> .CreateTypedRuleEngine(); var result = ruleEngine.ExecuteAllRules(subject); Assert.IsTrue(result.HasRuleViolation); // The rule engine will let you know what object violated the rule var ruleViolation = result.RuleViolations.Single(); Assert.AreSame(subject, ruleViolation.OffendingObject); // It will also let you know what properties are involved in the rule violation Assert.AreEqual("Text", ruleViolation.PropertyNames.Single()); }
public void Example3Test() { var newCustomer = new Example3Customer { IsNewCustomer = true }; var ruleEngine = RuleEngine <Example3Customer> .CreateTypedRuleEngine(); ruleEngine.ExecuteAllRules(newCustomer); Assert.IsTrue(newCustomer.GreetingHasBeenSent); Assert.IsNotNull(newCustomer.Account); var oldCustomer = new Example3Customer { IsNewCustomer = false }; ruleEngine.ExecuteAllRules(oldCustomer); Assert.IsFalse(oldCustomer.GreetingHasBeenSent); }
public void Example4Test() { var subject = new Example4Subject(); var example4Address1 = new Example4Address { Street1 = "1 Elm Street", Street2 = "Apt C", City = "Orlando", State = "FL", Zipcode = "32801" }; var example4Address2 = new Example4Address { Street1 = "1 Elm Street", Street2 = "Apt C", City = "Orlando", State = "FL", Zipcode = "32801" }; var example4Address3 = new Example4Address(); subject.Addresses.Add(example4Address1); subject.Addresses.Add(example4Address2); subject.Addresses.Add(example4Address3); var ruleEngine = RuleEngine <Example4Subject> .CreateTypedRuleEngine(); var result = ruleEngine.ExecuteAllRules(subject); Assert.IsTrue(result.HasRuleViolation); var ruleViolationCount = result.RuleViolations .Where(rv => object.ReferenceEquals(rv.OffendingObject, example4Address3)) .Count(); Assert.AreEqual(4, ruleViolationCount); }
public void Example2Test() { var milk = new Example2LineItem { Product = "Milk", Price = 4.5 }; var bread = new Example2LineItem { Product = "Bread", Price = 3.75 }; var eggs = new Example2LineItem { Product = "Eggs", Price = 2.55 }; var purchaseOrder = new Example2PurchaseOrder(); purchaseOrder.LineItems.Add(milk); purchaseOrder.LineItems.Add(bread); purchaseOrder.LineItems.Add(eggs); var account = new Example2Account { Balance = 10 }; var ruleEngine = RuleEngine <Example2PurchaseOrder> .CreateTypedRuleEngine(); var ruleEngineContext = new RuleEngineContext <Example2PurchaseOrder>(purchaseOrder); ruleEngineContext.WorkingMemory.AddContextObject(account); var result = ruleEngine.ExecuteRules(ruleEngineContext); Assert.IsTrue(result.HasRuleViolation); }