Example #1
0
        private bool Validate()
        {
            var isValid = true;

            foreach (var validationResult in this.GetValidator().Validate(this.Value))
            {
                BindingResult bindResult;
                if (!this.evaluationResults.TryGetValue(validationResult.PropertyName, out bindResult))
                {
                    bindResult = new BindingResult(validationResult.PropertyName);
                    this.evaluationResults.Add(validationResult.PropertyName, bindResult);
                }

                bindResult.Errors.Add(validationResult.ErrorMessage);
                isValid = false;
            }

            return(isValid);
        }
Example #2
0
        /// <summary>
        /// Evaluates this binding.
        /// </summary>
        /// <returns><see langword="true"/>, if the evaluation of the binding succeded; otherwise <see langword="false"/>.</returns>
        public virtual bool Evaluate()
        {
            if (GuidanceManagerSettings.VerboseBindingTracing)
            {
                tracer.Verbose(Resources.Binding_TraceEvaluate,
                               this.lazyValue == null ? this.ToString() : this.lazyValue.Metadata.Id, this);
            }
            this.evaluationResults.Clear();

            if (this.lazyValue == null)
            {
                // Re-evaluate component from MEF as new available exports
                // might have made it available whereas previously it was not.
                ResolveComponentTypeId();
            }

            bool evaluationResult;

            if (this.Value == null)
            {
                evaluationResult = false;

                var bindingResult = new BindingResult(@"this");
                bindingResult.Errors.Add(string.Format(CultureInfo.CurrentCulture,
                                                       Resources.Binding_EvaluateBindingError,
                                                       this.componentTypeId, typeof(T).Name));

                this.evaluationResults.Add(@"this", bindingResult);
            }
            else
            {
                var initializable = this.Value as ISupportInitialize;
                var editable      = this.Value as IEditableObject;
                if (initializable != null)
                {
                    initializable.BeginInit();
                }
                if (editable != null)
                {
                    editable.BeginEdit();
                }

                composition.SatisfyImportsOnce(this.Value);
                evaluationResult = this.EvaluateProperties();

                if (evaluationResult)
                {
                    evaluationResult = this.Validate();
                    if (evaluationResult)
                    {
                        if (editable != null)
                        {
                            editable.EndEdit();
                        }
                        if (initializable != null)
                        {
                            initializable.EndInit();
                        }
                    }
                    else
                    {
                        if (editable != null)
                        {
                            editable.CancelEdit();
                        }
                    }
                }
                else
                {
                    // We don't call EndInit as we didn't end.
                    if (editable != null)
                    {
                        editable.CancelEdit();
                    }
                }
            }

            this.HasErrors = !evaluationResult;

            if (GuidanceManagerSettings.VerboseBindingTracing)
            {
                tracer.Verbose(ObjectDumper.ToString(this, 5));
            }

            return(evaluationResult);
        }