コード例 #1
0
 public EntityState(EntityRules entityRules)
 {
     m_entityRules = entityRules;
     Tags          = new TagCollection(this);
     m_actions     = entityRules.Actions.Select(x => x.Clone()).ToList().AsReadOnly();
     Key           = Guid.NewGuid();
 }
コード例 #2
0
ファイル: AttributeRule.cs プロジェクト: xBimTeam/XbimMvdXML
        /// <summary>
        /// Logs debug information about the tree in Log4Net.
        /// </summary>
        /// <param name="indentation">the level of indentation of the current branch</param>
        /// <param name="prefix">prefix for variable names</param>
        public void DebugTree(int indentation = 0, string prefix = "")
        {
#if DEBUG
            var ind = new string('\t', indentation);
            if (!string.IsNullOrEmpty(RuleID))
            {
                Log.Debug($"{ind}{AttributeName} => {RuleID}");
            }
#endif

            foreach (var entityRule in EntityRules.NotNullEnumerable())
            {
#if DEBUG
                Log.DebugFormat("{0}{1}{2}{3}", ind, AttributeName,
                                string.IsNullOrEmpty(entityRule.EntityName) // conditional parameters are passed to #2 and #3
                        ? ""
                        : $" ({entityRule.EntityName})",
                                string.IsNullOrEmpty(RuleID)
                        ? ""
                        : $" => {RuleID}"
                                );
#endif
                entityRule.DebugTree(indentation + 1, prefix);
            }
        }
コード例 #3
0
        /// <summary>
        /// Adds a rule that specifies that the string property must be at least a specified length.
        /// </summary>
        /// <param name="minimumLength">The minimum length that the string property can be.</param>
        /// <param name="messageLambda">A lambda or function that will determine the error message to use should this validation rule fail.</param>
        /// <returns>A <see cref="FluentStringRuleSet{TEntity}"/> object that can be used to chain string rules together.</returns>
        public FluentStringRuleSet <TEntity> MinimumLength(int minimumLength, Func <TEntity, string> messageLambda)
        {
            var name = String.Format(Resources.FluentMinimumLengthName, Property.Name);

            EntityRules.AddRule(x => ((string)Property.GetValue(x, null)) == null || ((string)Property.GetValue(x, null)).Length >= minimumLength, name, messageLambda);

            return(this);
        }
コード例 #4
0
        /// <summary>
        /// Adds a rule that specifies that the string property must be set and not empty.
        /// </summary>
        /// <param name="messageLambda">A lambda or function that will determine the error message to use should this validation rule fail.</param>
        /// <returns>A <see cref="FluentStringRuleSet{TEntity}"/> object that can be used to chain string rules together.</returns>
        public FluentStringRuleSet <TEntity> Required(Func <TEntity, string> messageLambda)
        {
            var name = String.Format(Resources.FluentIsRequiredName, Property.Name);

            EntityRules.AddRule(x => !String.IsNullOrEmpty((string)Property.GetValue(x, null)), name, messageLambda);

            return(this);
        }
コード例 #5
0
        /// <summary>
        /// Adds a rule that specifies that the string property must match a specific regular expression pattern.
        /// </summary>
        /// <param name="pattern">The regular expression pattern that the property must match.</param>
        /// <param name="messageLambda">A lambda or function that will determine the error message to use should this validation rule fail.</param>
        /// <returns>A <see cref="FluentStringRuleSet{TEntity}"/> object that can be used to chain string rules together.</returns>
        public FluentStringRuleSet <TEntity> MatchesPattern(string pattern, Func <TEntity, string> messageLambda)
        {
            var name = String.Format(Resources.FluentMatchesPatternName, Property.Name);

            EntityRules.AddRule(x => ((string)Property.GetValue(x, null)) == null || Regex.IsMatch((string)Property.GetValue(x, null), pattern), name, messageLambda);

            return(this);
        }
コード例 #6
0
        public override void RunRules(IBaseEntity Entity, EntityRules Entities, ModelStateDictionary modelState)
        {
            var e = (SubCategoryType)Entity;

            if (string.IsNullOrEmpty(e.SubCategoryTypeName))
            {
                modelState.AddModelError("", "Sub Cat Type Name cannot be emoty");
            }
        }
コード例 #7
0
            private void rules_should(bool choice)
            {
                var MockModelState = new Moq.Mock <ModelStateDictionary>();
                var MockEdit       = new Moq.Mock <TestDomainEdit>();
                var MockRules      = new Moq.Mock <IRule>();
                //_Context.Object
                var MockRepos = new Mock <IRepository <TestEntity> >();

                var Ent = new EntityRules(null);

                Ent.Rules.Add("TestEntity", new TestEntity());
                MockRepos.Setup(s => s.ModifiedEntities).Returns(Ent);
                MockRepos.Setup(s => s.Add(It.IsAny <TestEntity>()));
                //MockRepos.Setup(s =>
                //  s.RunRules(It.IsAny<ServiceRuleFunc<bool>>()
                //           , It.IsAny<ModelStateDictionary>()
                //           , It.IsAny<object>())).Returns(true);


                var _service = new Moq.Mock <TestServices <TestEntity> >(
                    MockRepos.Object
                    , new Moq.Mock <TestDomainEdit>().Object
                    , MockRules.Object
                    )
                {
                    CallBase = true
                };

                _service.Setup(s =>
                               s.RunRules(It.IsAny <ServiceRuleFunc <bool> >()
                                          , It.IsAny <object>()
                                          , It.IsAny <object>())).Returns(true);


                var te = new TestEntity();

                te.RulesEnabled = choice;
                _service.Object.Add(te);
                _service.Object.Verify(MockModelState.Object);


                if (choice)
                {
                    _service.Verify(mock =>
                                    mock.RunRules(It.IsAny <ServiceRuleFunc <bool> >()
                                                  , It.IsAny <ModelStateDictionary>()
                                                  , It.IsAny <object>()), Times.Once, "Rules Should Run");
                }
                else
                {
                    _service.Verify(mock =>
                                    mock.RunRules(It.IsAny <ServiceRuleFunc <bool> >()
                                                  , It.IsAny <ModelStateDictionary>()
                                                  , It.IsAny <object>()), Times.Never, "Rules Should Not Run");
                }
            }
コード例 #8
0
        public FluentNumberRuleSet <TEntity> Minimum(decimal minimum, Func <TEntity, string> messageLambda)
        {
            var name = String.Format(Resources.FluentMinimumValueName, Property.Name);

            EntityRules.AddRule(x => (Property.PropertyType.IsGenericType && Property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>) ?
                                      (Property.GetValue(x, null) != null ? Convert.ToDecimal(Property.GetValue(x, null)) >= minimum : true) :
                                      Convert.ToDecimal(Property.GetValue(x, null)) >= minimum), name, messageLambda);

            return(this);
        }
コード例 #9
0
        /// <summary>
        /// Adds a rule that specifies that the numeric property must be set.
        /// </summary>
        /// <param name="messageLambda">A lambda or function that will determine the error message to use should this validation rule fail.</param>
        /// <returns>A <see cref="FluentNumberRuleSet{TEntity}"/> object that can be used to chain numeric rules together.</returns>
        public FluentNumberRuleSet <TEntity> Required(Func <TEntity, string> messageLambda)
        {
            var name = String.Format(Resources.FluentIsRequiredName, Property.Name);

            EntityRules.AddRule(x =>
                                Property.PropertyType.IsGenericType && Property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>) ?
                                ((decimal?)Property.GetValue(x, null)).HasValue :
                                true, name, messageLambda);

            return(this);
        }
コード例 #10
0
            public bool Test_Rules(string RuleName)
            {
                var ruleEntity = new TestEntityError();

                var context = new ReposContext();

                var uow = new Mock <IUnitOfWork>();


                ModelStateDictionary modelState = new ModelStateDictionary();
                var MockRepos = new Mock <IRepository <TestEntityError> >();



                MockRepos.Setup(s => s.Add(new Moq.Mock <TestEntityError>().Object));

                var Ent = new EntityRules(null);

                switch (RuleName)
                {
                case "TestEntity":
                    Ent.Rules.Add("TestEntity", new TestEntity());
                    break;

                case "TestEntityError":
                    Ent.Rules.Add("TestEntityError", new TestEntityError());
                    break;
                }

                MockRepos.Setup(s => s.Add(ruleEntity));
                MockRepos.Setup(s => s.ModifiedEntities).Returns(Ent);

                var MockRules = new Moq.Mock <TestRulesFactory>()
                {
                    CallBase = true
                };

                var _service = new Moq.Mock <TestServicesError>(
                    MockRepos.Object
                    , new Moq.Mock <TestDomainEdit>().Object
                    , MockRules.Object
                    )
                {
                    CallBase = true
                };

                _service.Object.Add(ruleEntity);
                _service.Object.Verify(modelState);

                var res = modelState.IsValid;

                return(res);
            }
コード例 #11
0
        public static EntityRulesViewModel CreateFromData(EntityRules entity)
        {
            var stringLookup         = StringLookupViewModel.CreateFromData(entity.StringLookup);
            var preBattleEffects     = entity.PreBattleEffects.Select(EffectViewModelBase.CreateFromData);
            var postBattleEffects    = entity.PostBattleEffects.Select(EffectViewModelBase.CreateFromData);
            var preTurnEffects       = entity.PreTurnEffects.Select(EffectViewModelBase.CreateFromData);
            var postTurnEffects      = entity.PostTurnEffects.Select(EffectViewModelBase.CreateFromData);
            var eliminationCondition = ConditionViewModelBase.CreateFromData(entity.EliminationCondition);
            var actions      = entity.Actions.Select(x => ActionViewModel.CreateFromData(x, stringLookup));
            var results      = entity.Results.Select(x => ResultViewModel.CreateFromData(x, stringLookup));
            var actionMatrix = ActionMatrixViewModel.CreateFromData(entity.ActionMatrix.Values);

            return(new EntityRulesViewModel(entity.Name, preBattleEffects, postBattleEffects, preTurnEffects, postTurnEffects, eliminationCondition, actions, results, actionMatrix, stringLookup));
        }
コード例 #12
0
        public EntityRules ToEntityRules()
        {
            var entity = new EntityRules(m_name, StringLookup.ToStringLookup());

            entity.PreBattleEffects     = m_preBattleEffectsNode.GetChildren().Select(x => x.ToEffectBase()).ToList().AsReadOnly();
            entity.PostBattleEffects    = m_postBattleEffectsNode.GetChildren().Select(x => x.ToEffectBase()).ToList().AsReadOnly();
            entity.PreTurnEffects       = m_preTurnEffectsNode.GetChildren().Select(x => x.ToEffectBase()).ToList().AsReadOnly();
            entity.PostTurnEffects      = m_postTurnEffectsNode.GetChildren().Select(x => x.ToEffectBase()).ToList().AsReadOnly();
            entity.EliminationCondition = m_eliminationCondition.ToConditionBase();
            entity.Actions = m_actionsNode.GetChildren().Select(x => x.ToAction()).ToList().AsReadOnly();
            entity.Results = m_resultsNode.GetChildren().Select(x => x.ToResult()).ToList().AsReadOnly();
            entity.SetActionMatrix(ActionMatrix.ToActionMatrixEntries());
            return(entity);
        }
コード例 #13
0
        public override void RunRules(IBaseEntity Entity
                                      , EntityRules entitiesRules
                                      , ModelStateDictionary modelState)
        {
            var subCategoryType = GetEntity <SubCategoryType>(entitiesRules);
            var subCategoryItem = GetEntity <SubCategoryClassItem>(entitiesRules);

            if (subCategoryType == null)
            {
                modelState.AddModelError("", "New Order SubCategory must contain one Sub Type");
            }
            if (subCategoryItem == null)
            {
                modelState.AddModelError("", "New Order SubCategory must contain one Sub Item");
            }
        }
コード例 #14
0
        /// <summary>
        /// Execute rule by client or user
        /// </summary>
        /// <param name="Entity"></param>
        /// <param name="factory"></param>
        /// <param name="Entities"></param>
        /// <param name="modelState"></param>
        private static void ExecuteRule(IBaseEntity Entity
                                        , IRule Rulefactory
                                        , EntityRules Entities
                                        , ModelStateDictionary modelState)
        {
            var client = _clientInfo ?? Rulefactory.Client;

            var rules = Rulefactory.GetDomainRules(Entity, client);

            foreach (var rule in rules)
            {
                if (rule.Required)
                {
                    rule.RunRules(Entity, Entities, modelState);
                }
            }
        }
コード例 #15
0
        private static bool RunEntityRules(EntityRules Entities
                                           , ModelStateDictionary modelState
                                           , IRule RuleFac
                                           )
        {
            foreach (KeyValuePair <string, IBaseEntity> entity in Entities.Rules)
            {
                if (!entity.Value.RulesEnabled)
                {
                    continue;
                }

                ExecuteRule(entity.Value, RuleFac, Entities, modelState);
            }

            return(modelState.IsValid);
        }
コード例 #16
0
        public override void RunRules(IBaseEntity Entity
                                      , EntityRules entitiesRules
                                      , ModelStateDictionary modelState)
        {
            var name = string.Empty;

            var SubCat = GetEntity <SubCategory>(entitiesRules);

            var exists = _ServiceHandlerFactory.Using <ISubCategoryHandler>().Get()
                         .Where(c => c.SubCategoryName.Value == SubCat.SubCategoryName.Value)
                         .AsEnumerable().Any(w => SubCat.IsNew ||
                                             (SubCat.OldPicture != null &&
                                              SubCat.OldPicture.SubCategoryName.Value != w.SubCategoryName.Value));


            if (exists)
            {
                modelState.AddModelError("", "Found Duplicate Sub Category Name");
            }
        }
コード例 #17
0
 public override void RunRules(IBaseEntity Entity, EntityRules Entities, ModelStateDictionary modelState)
 {
 }
コード例 #18
0
 public override void RunRules(IBaseEntity Entity, EntityRules Entities, Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary modelState)
 {
     throw new System.NotImplementedException();
 }