protected virtual bool Set<T>(ref T field, T value, [CallerMemberName]string? automaticPropertyName = null) { if (EqualityComparer<T>.Default.Equals(field, value)) return false; PropertyInfo pi = GetPropertyInfo(automaticPropertyName!); if (pi == null) throw new ArgumentException("No PropertyInfo with name {0} found in {1} or any implemented interface".FormatWith(automaticPropertyName, this.GetType().TypeName())); if (value is IMListPrivate && !((IMListPrivate)value).IsNew && !object.ReferenceEquals(value, field)) throw new InvalidOperationException("Only MList<T> with IsNew = true can be assigned to an entity"); if (field is INotifyCollectionChanged colb) { if (AttributeManager<NotifyCollectionChangedAttribute>.FieldContainsAttribute(GetType(), pi)) colb.CollectionChanged -= ChildCollectionChanged; if (AttributeManager<NotifyChildPropertyAttribute>.FieldContainsAttribute(GetType(), pi)) foreach (ModifiableEntity item in (IEnumerable)colb) item.SetParentEntity(null); } if (field is ModifiableEntity modb) { if (AttributeManager<NotifyChildPropertyAttribute>.FieldContainsAttribute(GetType(), pi)) modb.SetParentEntity(null); } SetSelfModified(); field = value; if (field is INotifyCollectionChanged cola) { if (AttributeManager<NotifyCollectionChangedAttribute>.FieldContainsAttribute(GetType(), pi)) cola.CollectionChanged += ChildCollectionChanged; if (AttributeManager<NotifyChildPropertyAttribute>.FieldContainsAttribute(GetType(), pi)) foreach (ModifiableEntity item in (IEnumerable)cola) item.SetParentEntity(this); } if (field is ModifiableEntity moda) { if (AttributeManager<NotifyChildPropertyAttribute>.FieldContainsAttribute(GetType(), pi)) moda.SetParentEntity(this); } NotifyPrivate(pi.Name); NotifyPrivate("Error"); NotifyToString(); ClearTemporalError(pi.Name); return true; }
protected virtual void RebindEvents() { foreach (INotifyCollectionChanged notify in AttributeManager <NotifyCollectionChangedAttribute> .FieldsWithAttribute(this)) { if (notify == null) { continue; } notify.CollectionChanged += ChildCollectionChanged; } foreach (object field in AttributeManager <NotifyChildPropertyAttribute> .FieldsWithAttribute(this)) { if (field == null) { continue; } var entity = field as ModifiableEntity; if (entity != null) { entity.PropertyChanged += ChildPropertyChanged; } else { foreach (INotifyPropertyChanged item in (IEnumerable)field) { item.PropertyChanged += ChildPropertyChanged; } } } foreach (object field in AttributeManager <ValidateChildPropertyAttribute> .FieldsWithAttribute(this)) { if (field == null) { continue; } var entity = field as ModifiableEntity; if (entity != null) { entity.ExternalPropertyValidation += ChildPropertyValidation; } else { foreach (ModifiableEntity item in (IEnumerable)field) { item.ExternalPropertyValidation += ChildPropertyValidation; } } } }
protected virtual void ChildCollectionChanged(object sender, NotifyCollectionChangedEventArgs args) { string propertyName = AttributeManager <NotifyCollectionChangedAttribute> .FindPropertyName(this, sender); if (propertyName != null) { NotifyPrivate(propertyName); } if (AttributeManager <NotifyChildPropertyAttribute> .FieldsWithAttribute(this).Contains(sender)) { if (args.NewItems != null) { foreach (var p in args.NewItems.Cast <INotifyPropertyChanged>()) { p.PropertyChanged += ChildPropertyChanged; } } if (args.OldItems != null) { foreach (var p in args.OldItems.Cast <INotifyPropertyChanged>()) { p.PropertyChanged -= ChildPropertyChanged; } } } if (AttributeManager <ValidateChildPropertyAttribute> .FieldsWithAttribute(this).Contains(sender)) { if (args.NewItems != null) { foreach (var p in args.NewItems.Cast <ModifiableEntity>()) { p.ExternalPropertyValidation += ChildPropertyValidation; } } if (args.OldItems != null) { foreach (var p in args.OldItems.Cast <ModifiableEntity>()) { p.ExternalPropertyValidation -= ChildPropertyValidation; } } } }
//[OnDeserialized] //private void OnDeserialized(StreamingContext context) //{ // RebindEvents(); //} protected virtual void ChildCollectionChanged(object sender, NotifyCollectionChangedEventArgs args) { string? propertyName = AttributeManager<NotifyCollectionChangedAttribute>.FindPropertyName(this, sender); if (propertyName != null) NotifyPrivate(propertyName); if (AttributeManager<NotifyChildPropertyAttribute>.FieldsWithAttribute(this).Contains(sender)) { if (args.NewItems != null) { foreach (var p in args.NewItems.Cast<ModifiableEntity>()) p.SetParentEntity(this); } if (args.OldItems != null) { foreach (var p in args.OldItems.Cast<ModifiableEntity>()) p.SetParentEntity(this); } } }
protected virtual void RebindEvents() { foreach (INotifyCollectionChanged? notify in AttributeManager<NotifyCollectionChangedAttribute>.FieldsWithAttribute(this)) { if (notify == null) continue; notify.CollectionChanged += ChildCollectionChanged; } foreach (object? field in AttributeManager<NotifyChildPropertyAttribute>.FieldsWithAttribute(this)) { if (field == null) continue; if (field is ModifiableEntity entity) entity.SetParentEntity(this); else { foreach (ModifiableEntity item in (IEnumerable)field!) item.SetParentEntity(this); } } }
protected virtual bool Set <T>(ref T field, T value, [CallerMemberNameAttribute] string automaticPropertyName = null) { if (EqualityComparer <T> .Default.Equals(field, value)) { return(false); } PropertyInfo pi = GetPropertyInfo(automaticPropertyName); if (pi == null) { throw new ArgumentException("No PropertyInfo with name {0} found in {1} or any implemented interface".FormatWith(automaticPropertyName, this.GetType().TypeName())); } if (value is IMListPrivate && !((IMListPrivate)value).IsNew && !object.ReferenceEquals(value, field)) { throw new InvalidOperationException("Only MList<T> with IsNew = true can be assigned to an entity"); } INotifyCollectionChanged col = field as INotifyCollectionChanged; if (col != null) { if (AttributeManager <NotifyCollectionChangedAttribute> .FieldContainsAttribute(GetType(), pi)) { col.CollectionChanged -= ChildCollectionChanged; } if (AttributeManager <NotifyChildPropertyAttribute> .FieldContainsAttribute(GetType(), pi)) { foreach (INotifyPropertyChanged item in (IEnumerable)col) { item.PropertyChanged -= ChildPropertyChanged; } } if (AttributeManager <ValidateChildPropertyAttribute> .FieldContainsAttribute(GetType(), pi)) { foreach (ModifiableEntity item in (IEnumerable)col) { item.ExternalPropertyValidation -= ChildPropertyValidation; } } } ModifiableEntity mod = field as ModifiableEntity; if (mod != null) { if (AttributeManager <NotifyChildPropertyAttribute> .FieldContainsAttribute(GetType(), pi)) { mod.PropertyChanged -= ChildPropertyChanged; } if (AttributeManager <ValidateChildPropertyAttribute> .FieldContainsAttribute(GetType(), pi)) { mod.ExternalPropertyValidation -= ChildPropertyValidation; } } SetSelfModified(); field = value; col = field as INotifyCollectionChanged; if (col != null) { if (AttributeManager <NotifyCollectionChangedAttribute> .FieldContainsAttribute(GetType(), pi)) { col.CollectionChanged += ChildCollectionChanged; } if (AttributeManager <NotifyChildPropertyAttribute> .FieldContainsAttribute(GetType(), pi)) { foreach (INotifyPropertyChanged item in (IEnumerable)col) { item.PropertyChanged += ChildPropertyChanged; } } if (AttributeManager <ValidateChildPropertyAttribute> .FieldContainsAttribute(GetType(), pi)) { foreach (ModifiableEntity item in (IEnumerable)col) { item.ExternalPropertyValidation += ChildPropertyValidation; } } } mod = field as ModifiableEntity; if (mod != null) { if (AttributeManager <NotifyChildPropertyAttribute> .FieldContainsAttribute(GetType(), pi)) { mod.PropertyChanged += ChildPropertyChanged; } if (AttributeManager <ValidateChildPropertyAttribute> .FieldContainsAttribute(GetType(), pi)) { mod.ExternalPropertyValidation += ChildPropertyValidation; } } NotifyPrivate(pi.Name); NotifyPrivate("Error"); NotifyToString(); ClearTemporalError(pi.Name); return(true); }