DataObjectBase Modfications

Topics: Feature requests
Jul 8, 2012 at 5:46 PM

Hi Geert,

could you please change the following in DataObjectBase:

private void OnObjectPropertyChanged(object sender, PropertyChangedEventArgs e) ->

protected virtual void OnObjectPropertyChanged(object sender, PropertyChangedEventArgs e)

and

protected sealed override void RaisePropertyChanged(object sender, AdvancedPropertyChangedEventArgs e) ->

protected override void RaisePropertyChanged(object sender, AdvancedPropertyChangedEventArgs e)

Question:

Is there a possibility to suspend all notifications for a DataObject like suspendValidations? At startup i have to create large object hierarchies and i think i could regain a big speed improvement if i could also suspend the notifications while loading/creating the structures.

Btw: Using the FastObservableCollection for child collections helped me a lot to regain some of the performance. It would be very helpful if the DataObjectBase could also offer such a functionality like .... IDisposable SuspendValidations() and IDisposable SuspendNotifications()... and so on....

   [Serializable]
    public class FastObservableCollection<T> : ObservableCollection<T>
    {
        private bool suspendChangeNotifications;

        public FastObservableCollection()
        {
        }

        public FastObservableCollection(IEnumerable<T> collection)
            : base(collection)
        {
        }

        public void AddItems(IEnumerable<T> collection)
        {
            Contract.Requires(collection != null);

            using (this.SuspendChangeNotifications())
            {
                foreach (var item in collection)
                {
                    this.Add(item);
                }
            }
        }

        public void AddItems(IEnumerable collection)
        {
            Contract.Requires(collection != null);

            using (this.SuspendChangeNotifications())
            {
                foreach (var item in collection)
                {
                    ((IList)this).Add(item);
                }
            }
        }
        public void NotifyChanges()
        {
            this.OnPropertyChanged(new PropertyChangedEventArgs(string.Empty));
            this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        public void RemoveItems(IEnumerable<T> collection)
        {
            Contract.Requires(collection != null);

            using (this.SuspendChangeNotifications())
            {
                foreach (var item in collection)
                {
                    this.Remove(item);
                }
            }
        }

        public void RemoveItems(IEnumerable collection)
        {
            Contract.Requires(collection != null);

            using (this.SuspendChangeNotifications())
            {
                foreach (var item in collection)
                {
                    ((IList)this).Remove(item);
                }
            }
        }
        public void ResumeChangeNotifications()
        {
            this.suspendChangeNotifications = false;
            this.NotifyChanges();
        }

        public IDisposable SuspendChangeNotifications()
        {
            return new SuspendChangeNotificationsToken(this);
        }

        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (!this.suspendChangeNotifications)
            {
                base.OnCollectionChanged(e);
            }
        }
        protected override void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (!this.suspendChangeNotifications)
            {
                base.OnPropertyChanged(e);
            }
        }

        private class SuspendChangeNotificationsToken : IDisposable
        {
            private readonly bool oldSuspendChangeNotifications;
            private FastObservableCollection<T> collection;

            public SuspendChangeNotificationsToken(FastObservableCollection<T> collection)
            {
                Contract.Requires(collection != null);

                this.collection = collection;
                this.oldSuspendChangeNotifications = this.collection.suspendChangeNotification;
                this.collection.suspendChangeNotification = true;
            }

            public void Dispose()
            {
                if (this.collection != null)
                {
                    this.collection.suspendChangeNotification = this.oldSuspendChangeNotifications;
                    this.collection.NotifyChanges();
                    this.collection = null;
                }
            }
        }
    }

Many Thanks!

Coordinator
Jul 9, 2012 at 5:39 PM

I changed the first 2 requests. There is no way to suspend notifying of property changes at the moment, but I understand your requirement. You can create and issue if you want, then we will put it on the backlog.

Jul 9, 2012 at 10:25 PM
Edited Jul 9, 2012 at 10:33 PM

Thanks a lot for doing the modifications. I'm sorry but I forgot to mention the following change wish:

private void RaisePropertyChanged(object sender, PropertyChangedEventArgs e, bool setDirtyAndAllowAutomaticValidation, bool isRefreshCallOnly) ->

protected virtual void RaisePropertyChanged(object sender, PropertyChangedEventArgs e, bool setDirtyAndAllowAutomaticValidation, bool isRefreshCallOnly)

and

internal void RaisePropertyChangedForAllRegisteredProperties() ->

protected void RaisePropertyChangedForAllRegisteredProperties()

I will do some tests and profiling and i will post the results and the source code....

Thanks a lot!