Swift MVVM

The purpose of Swift MVVM is twofold, first we wanted to solve the problem of invoking the PropertyChanged, and optionally PropertyChanging events of INotifyPropertyChanged, and INotifyChanging respectively. Secondly we wanted to see how well we could leverage the information generated by these events to monitor changes to the object graph from any point in time. Using that change data to log changes to the model, provide undo/redo logic, and even generate database change statements.

One of the problems many developers run into is making sure the property name that they pass to their changed/ing events properly match the property that just changed. Since it’s just a string they’re passing, it’s really easy to run into problems when refactoring. We’ve seen some frameworks that require you to derive from a base class that handles the notification. The approach we’ve taken is to only require that you implement INotifyPropertyChanged( and optionally INotifyPropertyChanging), then at runtime we generate a type that derives from your type overriding the property setter. Our overridden setter will then fire the property changed/ing events for you.

So rather than write code like this, where you need to define a backing field and make sure the string you pass to RaiseXXX matches your actual property name.

        private int premium;
        public int Premium
        {
            get
            {
                return this.premium;
            }
            set
            {
                if (this.premium != value)
                {
                    RaisePropertyChanging("Premium");
                    this.premium = value;
                    RaisePropertyChanged("Premium");
                }
            }
        }

You can now write code like this, and we generate the rest. By marking your property virtual (and as you can see we support automatic properties), we override the property_set to fire off property changed in a proxy class.

        /// <summary>
        /// Represents the premium of a policy
        /// </summary>

        [NotificationProperty]
        public virtual int Premium { get; set; }

Last edited Jul 5, 2012 at 5:18 AM by godzilla76, version 4