Exemple #1
0
        /// <summary>
        /// Creates a new instance of the <see cref="Binding{T}"/> class.
        /// </summary>
        /// <param name="composition"></param>
        /// <param name="componentTypeId"></param>
        /// <param name="propertyBindings"></param>
        public Binding(INuPatternCompositionService composition, string componentTypeId, params PropertyBinding[] propertyBindings)
        {
            Guard.NotNull(() => composition, composition);
            Guard.NotNullOrEmpty(() => componentTypeId, componentTypeId);

            this.componentTypeId  = componentTypeId;
            this.composition      = composition;
            this.propertyBindings = propertyBindings;

            ResolveComponentTypeId();

            composition.SatisfyImportsOnce(this);
        }
Exemple #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);
        }