Наследование: ExpressionVisitor
        public void Should_replace_array_index_with_correct_index_value()
        {
            Expression<Func<DrugTestForm, object>> expr = f => f.DrugTestDrugTestResults[int.MaxValue].SubstanceTested;

            var result = new IndexReplacerVisitor(5).Visit(expr);

            Assert.AreEqual("f => f.DrugTestDrugTestResults[5].SubstanceTested", result.ToString());
        }
Пример #2
0
        public ExecutionResult ValidateCommand(object commandMessage, ICommandConfiguration commandConfiguration)
        {
            var totalResult = new ExecutionResult();

            IEnumerable<ValidationRuleInstance> ruleInstances = commandConfiguration.GetValidationRules();

            foreach (ValidationRuleInstance instance in ruleInstances)
            {
                if (instance.ShouldApply != null)
                    if (!(bool) instance.ShouldApply.DynamicInvoke(commandMessage))
                        continue;

                Delegate compile = instance.ToCheckExpression.Compile();
                object input = compile.DynamicInvoke(new object[] {commandMessage});
                bool stopProcessing = false;

                if (instance.ArrayRule)
                {
                    var enumerable = (IEnumerable) input;

                    int i = 0;

                    foreach (object item in enumerable)
                    {
                        if (item == null) continue;

                        IValidationRule rule = _ruleFactory.ConstructRule(instance);
                        string result = rule.IsValid(item);

                        bool ruleFailed = result != null;

                        if (ruleFailed)
                        {
                            var indexedUiExpression = new IndexReplacerVisitor(i).Visit(instance.UIAttributeExpression);
                            totalResult.AddMessage(result, instance.ToCheckExpression, (LambdaExpression)indexedUiExpression, instance.ToCompareExpression);

                            if (rule.StopProcessing)
                            {
                                stopProcessing = true;
                                break;
                            }
                        }
                        i++;
                    }
                }
                else
                {
                    IValidationRule rule = _ruleFactory.ConstructRule(instance);

                    if (rule is ICrossReferencedValidationRule)
                    {
                        Delegate toCompareDelegate = instance.ToCompareExpression.Compile();
                        object toCompare = toCompareDelegate.DynamicInvoke(new object[] {commandMessage});
                        ((ICrossReferencedValidationRule) rule).ToCompare = toCompare;
                    }

                    string result = rule.IsValid(input);

                    bool ruleFailed = result != null;

                    if (ruleFailed)
                    {
                        totalResult.AddMessage(result, instance.ToCheckExpression, instance.UIAttributeExpression, instance.ToCompareExpression);

                        if (rule.StopProcessing)
                        {
                            break;
                        }
                    }
                }
                if (stopProcessing)
                    break;
            }

            return totalResult;
        }