Beispiel #1
0
        /// <summary>
        /// Method that checks if bindings are correct for the validation method of this Validation rule
        /// and then invokes teh validation method.
        /// </summary>
        /// <param name="binding"></param>
        /// <returns></returns>
        internal TResult Evaluate(RuleBinding <TResult> binding)
        {
            var bindings = MakeArgumentBindings(binding);
            var result   = ValidationMethod.Invoke(binding.ValidationRule, bindings);

            return((TResult)result);
        }
        /// <summary>
        /// Callback method that is called when the result of a validation rule has changed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProcessValidationResult(RuleBinding <TResult> ruleBinding, TResult validationResult)
        {
            var rule = ruleBinding.ValidationRule;

            var bindingGroups = from depBinding in ruleBinding.DependencyBindings
                                where
                                depBinding.ValidationRuleDependency.InputOnly == false &&
                                depBinding.TargetOwnerObject is TEntity
                                group depBinding by depBinding.TargetOwnerObject as TEntity;

            TResult oldValidationResult = null;

            if (ValidationResults.ContainsKey(ruleBinding))
            {
                oldValidationResult = ValidationResults[ruleBinding];
            }
            foreach (var bindingGroup in bindingGroups)
            {
                var entity         = bindingGroup.Key;
                var membersInError = bindingGroup.Select(binding => binding.ValidationRuleDependency.TargetProperty.Name).Distinct().ToArray();
                if (HasValidationResult(entity, membersInError, oldValidationResult))
                {
                    ClearValidationResult(entity, membersInError, oldValidationResult);
                }
                if (HasValidationResult(entity, membersInError, validationResult) == false)
                {
                    SetValidationResult(entity, membersInError, validationResult);
                }
            }
            if (IsValidationSuccess(validationResult) && oldValidationResult != null)
            {
                ValidationResults.Remove(ruleBinding);
                if (EntitiesInErrorChanged != null)
                {
                    EntitiesInErrorChanged(EntitiesInError, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
                }
            }
            else
            {
                ValidationResults[ruleBinding] = validationResult;
                if (EntitiesInErrorChanged != null)
                {
                    EntitiesInErrorChanged(EntitiesInError, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
                }
            }
            if (ValidationResultChanged != null)
            {
                ValidationResultChanged(this, new ValidationResultChangedEventArgs <TEntity, TResult>(validationResult, bindingGroups.Select(g => g.Key)));
            }
        }
Beispiel #3
0
        /// <summary>
        /// Returns an array of objects that form the actual parameter bindings
        /// for the validation method call.
        /// </summary>
        /// <param name="binding"></param>
        /// <returns></returns>
        internal object[] MakeArgumentBindings(RuleBinding <TResult> binding)
        {
            var type = GetType();

            if (Signature.Count != binding.DependencyBindings.Count())
            {
                string msg = String.Format(@"Argument count mismatch between Signature and rule method ""{0}"" in class {1}.", ValidationMethod.Name, type.Name);
                throw new Exception(msg);
            }

            var bindings = new object[Signature.Count];

            for (int i = 0; i < Signature.Count; i++)
            {
                var dBinding = binding.DependencyBindings[i];
                bindings[i] = dBinding.ValidationRuleDependency.TargetProperty.GetValue(dBinding.TargetOwnerObject, null);
            }
            return(bindings);
        }