ViewModel Tool

The tool automates the repetitive tasks require to have a rich model that can be used with data-binding.

Usage

Just set the CustomTool property of a class file (via the Property Browser) to "ViewModel", and you'll get a new .Generated class under your main class, with the necessary code to provide rich data binding.

Let's say you have the following classes:

	public class Customer
	{
		public string FirstName { get; set; }
		public Address Address { get; set; }
		// other properties
	}

	public class Address
	{
		public string City { get; set; }
		// other properties
	}

After assigning the ViewModel custom tool to the files, they will be changed to the following:

	public partial class Customer
	{
		Address _address;
		string _firstName;

		public string FirstName
		{
			get { return _firstName; }
			set { this.RaiseFirstNameChanging(); _firstName = value; this.RaiseFirstNameChanged(); }
		}
		public Address Address
		{
			get { return _address; }
			set { this.RaiseAddressChanging(); _address = value; this.RaiseAddressChanged(); }
		}
		// other properties
	}

	public partial class Address
	{
		string _city;

		public string City
		{
			get { return _city; }
			set { this.RaiseCityChanging(); _city = value; this.RaiseCityChanged(); }
		}
		// other properties
	}

The partial class will now implement INotifyPropertyChanged as well as INotifyPropertyChanging, and raise the events as appropriate.

A class consuming this model, can attach to strong-typed events like so:

    customer.AddressChanged += RefreshMap();

compare to the typical usage of the INotifyPropertyChanged interface:

   customer.PropertyChanged += OnPropertyChanged;

   // And the handler implementation
  private void OnPropertyChanged(object sender, PropertyChangedEventArgs args)
  {
      if (args.PropertyName == "Address")
          RefreshMap();
      // handle other properties
  }

Features

  • Automatic generation of INotifyPropertyChanged/INotifyPropertyChanging implementations for all your properties (the later is only generated for .NET 3.5+ projects)
  • Automatic generation of a strongly-typed event for each property of your model (i.e. NameChanged, DescriptionChanged, etc.). This essentially means no more magic strings with your property names! This makes it much easier to refactor and evolve your code that relies on property change notifications (i.e. your presenter reacts to changes in the model, rather than explicit view callbacks into it -a.k.a. Presentation Model)
  • Support for nested/hierarchical view models, with property change notifications throughout the hierarchy (i.e. a Customer view model contains a property Address; upon changing Address.City, the Customer model will raise both the typed AddressChanged event as well as the standard PropertyChanged event for the "Address" property)
  • Optional support for ISupportInitialize: just add the interface to your main class, and the tool will generate the implementation in the dependent partial class, including support for initializing the nested/child properties that also support the interface. While the object is being initialized, property change events are not raised, to optimize performance.
  • Optional support for IChangeTracking, which allows consumers to interrogate the object for its change status (works with hierarchies too).

Releases

Last edited Apr 8, 2009 at 5:34 PM by dcazzulino, version 4

Comments

No comments yet.