Esempio n. 1
0
        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 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);
        }