コード例 #1
0
ファイル: CustomerFactory.cs プロジェクト: girish66/REM
        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);
        }
コード例 #2
0
ファイル: SectionABasicRules.cs プロジェクト: girish66/REM
        public void Example2Test()
        {
            var subject = new Example2Subject {
                IsTrue = false
            };
            var ruleEngine = RuleEngine <Example2Subject> .CreateTypedRuleEngine();

            ruleEngine.ExecuteAllRules(subject);

            Assert.AreEqual("goodbye cruel world", subject.Greeting);
        }
コード例 #3
0
ファイル: SectionABasicRules.cs プロジェクト: girish66/REM
        public void Example1Test()
        {
            var subject = new Example1Subject {
                IsTrue = true
            };
            var ruleEngine = RuleEngine <Example1Subject> .CreateTypedRuleEngine();

            ruleEngine.ExecuteAllRules(subject);

            Assert.AreEqual("hello world", subject.Greeting);
        }
コード例 #4
0
        public void Example1Test()
        {
            var subject = new Example1Subject
            {
                Text = "Text"
            };
            var ruleEngine = RuleEngine <Example1Subject> .CreateTypedRuleEngine();

            var result = ruleEngine.ExecuteRuleSet(subject, "RuleSet1");

            Assert.IsFalse(result.HasRuleViolation);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        /// <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);
        }
コード例 #8
0
        /// <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);
        }
コード例 #9
0
ファイル: SectionABasicRules.cs プロジェクト: girish66/REM
        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);
        }
コード例 #10
0
        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);
        }
コード例 #11
0
        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
                });
            }
        }
コード例 #12
0
        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);
        }
コード例 #13
0
        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);
        }
コード例 #14
0
        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());
        }
コード例 #15
0
        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);
        }
コード例 #16
0
        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);
        }
コード例 #17
0
        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);
        }