ViewModelKit.Fody

Makes WPF ViewModel classes smart by default. Implements INotifyPropertyChanged and DelegateCommands for auto properties, recognises dependent properties, connects property changed handlers.

Probably every WPF project following the MVVM pattern (Model–View–ViewModel) has a class called “ViewModelBase” somewhere. It serves as base class for all ViewModel classes of the application and provides an implementation of the INotifyPropertyChanged interface in most cases. This is at least, besides the event, a protected method like “OnPropertyChanged” or “RaisePropertyChanged”.

But that alone doesn’t make the programmer’s life easier yet when there are numerous classes with equally numerous properties to be implemented, with dependencies among them (computed properties).

Largely replacing my runtime-based ViewModelBase class, I have made a Fody add-in package that manipulates the assembly after compilation and adds these features directly into the assembly, as if you had written them in your source code, just that you have written nothing at all.

For properties:

  • Raises the PropertyChanged event when the value of an auto-implemented property changes.
  • Also raises the PropertyChanged event for all dependent properties that access another notifying property or its backing field in the getter.
  • Calls OnPropertyNameChanged methods when the property has changed.
  • Calls OnPropertyNameChanging methods before the property has changed, providing the old and new value, with the option to reject or alter the new value.
  • Sets the IsModified property to true when another property changes, except when IsLoaded is false.
  • Raises the PropertyChanged event and calls other handler methods asynchronously (with SynchronizationContext.Current.Post()) if the property is virtual and the current instance is of a derived type. This allows Entity Framework to update foreign key/navigation properties in the dynamic proxy before the change events are raised.

For commands:

  • Connects DelegateCommand properties with similar-named OnCommandName and CanCommandName methods.
  • Raises the CanExecuteChanged event of all DelegateCommands that depend on a property, i. e. read the property or its backing field in their CanCommandName method.

Future ideas are about adding data validation support once I figured out the way I want to use it.

Please read the extended introduction with reference and examples on the repository page.

Compatibility: .NET Version 4.0 or newer

Download

master.zipLatest source code directly from GitHub

There’s a public Git repository of ViewModelKit on GitHub. Changes to the code are documented there.

Licence and terms of use

This software is released under the terms of the MIT licence. You can find the detailed terms and conditions in the download.

Statistic data

  • Created on 2016-08-28.
  • Ca. 1 800 lines of code, estimated development costs: 1 800 - 7 200 €