Exemplo n.º 1
0
        protected bool Adding(TEntityBusiness businessEntity, TEntityModel modelEntity, DbContext dbContext)
        {
            bool cancel = false;

            try {
                // Check Unique constraints
                ValidateUniqueEntity(modelEntity, businessEntity);

                if (OnAdding != null)
                {
                    OnAdding(businessEntity, modelEntity, dbContext, out cancel);
                }

                if (AddingHandlers.Count > 0)
                {
                    foreach (BusinessRuleElement handler in AddingHandlers)
                    {
                        bool cancelVal = BusinessRuleEngine.InvokeMethod(handler.Type, handler.MethodName, businessEntity, modelEntity, dbContext, false);

                        if (cancelVal)
                        {
                            return(false);
                        }
                    }
                }
            }
            catch (Exception ex) {
                bool rethrow = LibraryExceptionHandler.HandleException(ref ex, System.Diagnostics.TraceEventType.Error);
                if (rethrow)
                {
                    throw ex;
                }
            }
            return(!cancel);
        }
Exemplo n.º 2
0
        public void AddRule_Should_Add_Rule_ToEngine()
        {
            IBussinessRuleEngine ruleEngine = new BusinessRuleEngine();
            IRule rule = new PhysicalProductRule();

            ruleEngine.AddRule(rule);

            ruleEngine.Rules.Should().Contain(rule);
        }
Exemplo n.º 3
0
        public void RemoveRule_Should_Remove_Rule_FromEngine()
        {
            IBussinessRuleEngine ruleEngine = new BusinessRuleEngine();
            IRule rule = new PhysicalProductRule();

            ruleEngine.AddRule(rule);
            ruleEngine.RemoveRule(rule);

            ruleEngine.Rules.Should().NotContain(rule);
        }
Exemplo n.º 4
0
 protected void GetSingle(TEntityBusiness businessEntity, TEntityModel modelEntity, DbContext dbContext)
 {
     if (OnGotSingle != null)
     {
         OnGotSingle(businessEntity, modelEntity, dbContext);
     }
     if (GotSingleHandlers.Count > 0)
     {
         foreach (BusinessRuleElement handler in GotSingleHandlers)
         {
             BusinessRuleEngine.InvokeMethod(handler.Type, handler.MethodName, businessEntity, modelEntity, dbContext);
         }
     }
 }
Exemplo n.º 5
0
 protected void GotMultiple(IEnumerable <TEntityBusiness> businessEntities, IEnumerable <TEntityModel> modelEntities, DbContext dbContext)
 {
     if (OnGotMultiple != null)
     {
         OnGotMultiple(businessEntities, modelEntities, dbContext);
     }
     if (GotMultipleHandlers.Count > 0)
     {
         foreach (BusinessRuleElement handler in GotMultipleHandlers)
         {
             BusinessRuleEngine.InvokeMethod(handler.Type, handler.MethodName, businessEntities, modelEntities, dbContext);
         }
     }
 }
Exemplo n.º 6
0
 protected void Modified(TEntityBusiness businessEntity, TEntityModel modelEntity, DbContext dbContext)
 {
     if (OnModified != null)
     {
         OnModified(businessEntity, modelEntity, dbContext);
     }
     if (ModifiedHandlers.Count > 0)
     {
         foreach (BusinessRuleElement handler in ModifiedHandlers)
         {
             BusinessRuleEngine.InvokeMethod(handler.Type, handler.MethodName, businessEntity, modelEntity, dbContext);
         }
     }
 }
Exemplo n.º 7
0
        private static IBussinessRuleEngine SetUpBusinessRuleEngine()
        {
            IBussinessRuleEngine businessRuleEngine = new BusinessRuleEngine();

            businessRuleEngine
            .AddRule(new PhysicalProductRule())
            .AddRule(new CommissionRule())
            .AddRule(new EmailRule())
            .AddRule(new MembershipProductRule())
            .AddRule(new BookProductRule())
            .AddRule(new UpgradeProductRule())
            .AddRule(new VideoProductRule());

            return(businessRuleEngine);
        }
Exemplo n.º 8
0
        protected bool Deleting(TEntityBusiness businessEntity, TEntityModel modelEntity, DbContext dbContext)
        {
            bool cancel = false;

            if (OnDeleting != null)
            {
                OnDeleting(businessEntity, modelEntity, dbContext, out cancel);
            }

            if (DeletingHandlers.Count > 0)
            {
                foreach (BusinessRuleElement handler in DeletingHandlers)
                {
                    bool cancelVal = BusinessRuleEngine.InvokeMethod(handler.Type, handler.MethodName, businessEntity, modelEntity, dbContext, false);
                    if (cancelVal)
                    {
                        return(false);
                    }
                }
            }
            return(!cancel);
        }
Exemplo n.º 9
0
        public void Process_Should_Not_Execute_Rule()
        {
            IBussinessRuleEngine ruleEngine = new BusinessRuleEngine();
            IRule rule  = new PhysicalProductRule();
            var   order = new Order(Guid.NewGuid(), new Agent {
                Name = "Test"
            });
            var product = new Product();

            order.Add(new OrderLine(product, 1));

            var ruleMock = new Mock <IRule>();

            ruleMock.Setup(x => x.IsApplicable(product)).Returns(false);
            ruleMock.Setup(x => x.Apply()).Returns("Applied Rule");

            ruleEngine.AddRule(ruleMock.Object);
            ruleEngine.Process(order);

            ruleMock.Verify(x => x.IsApplicable(product), Times.Once);
            ruleMock.Verify(x => x.Apply(), Times.Never);
        }
Exemplo n.º 10
0
        public void Upgrade_Product_Rule_Test()
        {
            IBussinessRuleEngine businessEngine = new BusinessRuleEngine();
            var rule = new UpgradeProductRule();

            businessEngine.AddRule(rule);
            var agent = new Agent {
                Name = "Test"
            };
            var product = new Product
            {
                Attribute = ProductAttribute.NONPHYSICAL,
                Id        = 1,
                Type      = ProductType.Upgrade,
                Name      = "Upgrade to Premium"
            };

            var orderEngine = new OrderProcessor(businessEngine, agent);

            orderEngine.Add(new OrderLine(product, 1));
            orderEngine.Process().Single().Should().Be("Applying the upgrade");
        }
Exemplo n.º 11
0
        public void Membership_Product_Rule_Test()
        {
            IBussinessRuleEngine businessEngine = new BusinessRuleEngine();
            var rule = new MembershipProductRule();

            businessEngine.AddRule(rule);
            var agent = new Agent {
                Name = "Test"
            };
            var product = new Product
            {
                Attribute = ProductAttribute.NONPHYSICAL,
                Id        = 1,
                Type      = ProductType.Membership,
                Name      = "5 months subscription"
            };

            var orderEngine = new OrderProcessor(businessEngine, agent);

            orderEngine.Add(new OrderLine(product, 1));
            orderEngine.Process().Single().Should().Be("Activating membership");
        }
Exemplo n.º 12
0
        public void Book_Product_Rule_Test()
        {
            IBussinessRuleEngine businessEngine = new BusinessRuleEngine();
            var rule = new BookProductRule();

            businessEngine.AddRule(rule);
            var agent = new Agent {
                Name = "Test"
            };
            var product = new Product
            {
                Attribute = ProductAttribute.PHYSICAL,
                Id        = 1,
                Type      = ProductType.Book,
                Name      = "Hitchhiker's Guide to the Galaxy"
            };

            var orderEngine = new OrderProcessor(businessEngine, agent);

            orderEngine.Add(new OrderLine(product, 1));
            orderEngine.Process().Single().Should().Be("Creating a duplicate packing slip for the royalty department");
        }
Exemplo n.º 13
0
        public void Physical_Product_Rule_Test()
        {
            IBussinessRuleEngine businessEngine = new BusinessRuleEngine();
            var rule = new PhysicalProductRule();

            businessEngine.AddRule(rule);
            var agent = new Agent {
                Name = "Test"
            };
            var product = new Product
            {
                Attribute = ProductAttribute.PHYSICAL,
                Id        = 1,
                Type      = "Book",
                Name      = "Hitchhiker's Guide to the Galaxy"
            };

            var orderEngine = new OrderProcessor(businessEngine, agent);

            orderEngine.Add(new OrderLine(product, 1));
            orderEngine.Process().Single().Should().Be("Generating a packing slip for shipping.");
        }
Exemplo n.º 14
0
        public void Commision_Rule_Test()
        {
            IBussinessRuleEngine businessEngine = new BusinessRuleEngine();
            var rule = new CommissionRule();

            businessEngine.AddRule(rule);
            var agent = new Agent {
                Name = "Test"
            };
            var product = new Product
            {
                Attribute = ProductAttribute.PHYSICAL,
                Id        = 1,
                Type      = ProductType.Book,
                Name      = "Learning Python"
            };

            var orderEngine = new OrderProcessor(businessEngine, agent);

            orderEngine.Add(new OrderLine(product, 1));
            orderEngine.Process().Single().Should().Be("Generating commision payment to the agent");
        }
Exemplo n.º 15
0
        public void VideoProduct_Rule_Test()
        {
            IBussinessRuleEngine businessEngine = new BusinessRuleEngine();
            var rule = new VideoProductRule();

            businessEngine.AddRule(rule);
            var agent = new Agent {
                Name = "Test"
            };
            var product = new Product
            {
                Attribute = ProductAttribute.NONPHYSICAL,
                Id        = 1,
                Type      = ProductType.Video,
                Name      = "Learning to Ski"
            };

            var orderEngine = new OrderProcessor(businessEngine, agent);

            orderEngine.Add(new OrderLine(product, 1));
            orderEngine.Process().Single().Should().Be("Adding a free \"First Aid\" video to the packing slip (the result of a court decision in 1997)");
        }
Exemplo n.º 16
0
        public void Email_Rule_Test()
        {
            IBussinessRuleEngine businessEngine = new BusinessRuleEngine();
            var rule = new EmailRule();

            businessEngine.AddRule(rule);
            var agent = new Agent {
                Name = "Test"
            };
            var product = new Product
            {
                Attribute = ProductAttribute.NONPHYSICAL,
                Id        = 1,
                Type      = ProductType.Upgrade,
                Name      = "Upgrade to Premium"
            };

            var orderEngine = new OrderProcessor(businessEngine, agent);

            orderEngine.Add(new OrderLine(product, 1));
            orderEngine.Process().Single().Should().Be("Sending email to the owner for activation/upgrade");
        }