/// <summary> /// /// </summary> /// <param name="e"></param> protected virtual void OnGetValue(ValueTranslationEventArgs e) { if (Getting != null) { Getting(this, e); } }
protected override void OnSetValue(ValueTranslationEventArgs e) { ReferenceEntityT refEntity = (ReferenceEntityT)e.Input; if (refEntity == null) { e.Output = IdentityProperty.EmptyValue; // Remove any reference in case it's still there if (e.Entity.References.ContainsKey(this)) { e.Entity.References.Remove(this); } } else { e.Output = IdentityProperty.GetValue(refEntity); e.Entity.References[this] = refEntity; } }
/*=========================*/ #endregion #region Internal Methods /*=========================*/ protected override void OnGetValue(ValueTranslationEventArgs e) { // Get the raw reference ID ReferenceIdentityT refID = (ReferenceIdentityT)e.Input; if (refID.Equals(IdentityProperty.EmptyValue)) { // Empty ID means we return null value e.Output = null; // Remove any reference in case it's still there if (e.Entity.References.ContainsKey(this)) { e.Entity.References.Remove(this); } } else { ReferenceEntityT refEntity = (ReferenceEntityT)e.Entity.References[this]; if (refEntity == null || !IdentityProperty.GetValue(refEntity).Equals(refID)) { // Raise event for retrieving the entity ValueTranslationEventArgs e2 = new ValueTranslationEventArgs(e.Entity, this); e2.Input = refID; if (ReferenceEntityRequired != null) { ReferenceEntityRequired(this, e2); } refEntity = (ReferenceEntityT)e2.Output; e.Entity.References[this] = refEntity; } e.Output = refEntity; } base.OnGetValue(e); }
/*=========================*/ #endregion #region Public Methods /*=========================*/ /// <summary> /// /// </summary> /// <param name="parent"></param> /// <returns></returns> public ValueT GetValue(EntityT parent) { if (parent == null) { throw new ArgumentNullException("parent"); } // Try to retrieve current value, assign empty value if not object input; if (!parent.Values.TryGetValue(this, out input)) { input = this.EmptyValue; } ValueTranslationEventArgs e = new ValueTranslationEventArgs(parent, this); e.Input = input; e.Output = input is ValueT ? input : this.DefaultValue; OnGetValue(e); return((ValueT)e.Output); }
/// <summary> /// /// </summary> /// <param name="parent"></param> /// <param name="value"></param> public void SetValue(EntityT parent, ValueT value) { if (parent.DataState == EntityDataState.Deleted) { throw new InvalidOperationException("Cannot modify the value of an entity marked for deletion."); } // Validate the value and raise exception if necessary string message; // EXCEPTION: if (!Validate(parent, value, out message)) { throw new Exception(message); } // TODO: get last value without reinvoking GetValue ValueT previouslyAppliedVal = GetValue(parent); ValueTranslationEventArgs e = new ValueTranslationEventArgs(parent, this); e.Input = value; e.Output = value; OnSetValue(e); if (e.Cancel) { return; } // TODO: Dependency notifications if (e.Output == null && previouslyAppliedVal == null) { return; } if (e.Output != null && e.Output.Equals(previouslyAppliedVal)) { return; } // Apply value parent.Values[this] = e.Output; // Change data state to Modified if it was unchanged if (parent.DataState == EntityDataState.Unchanged) { parent.DataState = EntityDataState.Modified; } // Raise events if (ValueChanged != null) { ValueChanged(this, new ValueChangedEventArgs(parent, this, previouslyAppliedVal, value)); } if (parent.HasPropertyChangedHandlers) { parent.OnPropertyChanged(new PropertyChangedEventArgs(this.InlineName)); } }