Request for PropertyChangedBase tweak

Topics: Feature Requests, Framework Services
Jun 21, 2011 at 2:51 AM
Edited Jun 21, 2011 at 2:53 AM

Just wondering if we could add a Disposable object to PropertyChangedBase to allow simple and dummy proof disabling of IsNotifying for a block of code.  I would like to do the following:

 

// do some initialization tasks on the VM
using(TaskListFilter.BypassNotifyPropertyChanged)
{
     TaskListFilter.CurrentProtocolTask = defaultProtocolTask;
     ..
     ..
}

 

Thanks

Jack

 

Here is a snippet I copied from another framework I use and plugged in:

 

 #region Bypass NotifyPropertyChanged
        /// <summary>
        /// By wrapping this property inside Using block
        /// you can set property values on current business object
        /// without raising PropertyChanged events
        /// </summary>
        public BypassNotifyPropertyChangedObject BypassNotifyPropertyChanged
        {
            get { return new BypassNotifyPropertyChangedObject(this); }
        }

        /// <summary>
        /// Class that allows setting of property values on 
        /// current objectwithout raising PropertyChanged events
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        public class BypassNotifyPropertyChangedObject : IDisposable
        {
            private PropertyChangedBase _baseObject;
            private BypassNotifyPropertyChangedObject() { }
            internal BypassNotifyPropertyChangedObject(PropertyChangedBase baseObject)
            {
                _baseObject = baseObject;
                _baseObject.IsNotifying = false;
            }
            #region IDisposable Members

            /// <summary>
            /// Disposes the object.
            /// </summary>
            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }

            /// <summary>
            /// Disposes the object.
            /// </summary>
            /// <param name="dispose">Dispose flag.</param>
            protected void Dispose(bool dispose)
            {
                _baseObject.IsNotifying = true;
                _baseObject = null;
            }

            #endregion
        }

        #endregion

 

Jun 21, 2011 at 7:41 AM

I would define the 'cookie' as a struct, instead of a class, to avoid the heap allocation.

I use a similar approach to leverage the usage of ReaderWriterLockSlim, and I must say that using structs is the way to go, if you want to avoid any possible performance hit.

Coordinator
Jun 21, 2011 at 1:05 PM

You should be able to add this as an extension method on INotifyPropertyChangedEx without any modifications to the core necessary.