Update: I've updated my demo project, see my updated post here: http://travisjweber.blogspot.com/2011/02/how-to-make-your-view-models-disposable.html
I've put together a fairly simple example of some of the work I've done in an attempt to clean up endless property changed event handlers, switch statements, and string literals scattered throughout views and view models.
Think of a calculation that is performed on your view model, in it's getter, you simply return the result of a calculation made using values of other properties on the view model. This means though, that in order to notify the UI that the value of the calculation has changed, each property that affects the calculation must execute a property changed event for itself as well as the result property. This is pretty common practice, one set action may fire multiple property changed events.
But what if the property the calculation depends on is actually a property of another object (model) on the view model? This means you need to not only notify the result changed when the object changes, but also when a specific property of the object changes, meaning you need to wire up an event handler to the object, create a switch statement in the event handler to determine what property changed, and execute notifications accordingly.
Step 1: Define a custom attribute, used to decorate a property [usually/often read-only] that relies on one or more other properties.
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple=true)]
public class NotifyPropertyAttribute : Attribute
{
public string PropertyName { get; set; }
public NotifyPropertyAttribute(string propertyName)
{
PropertyName = propertyName;
}
}
Step 2: Auto-wire event handlers for property changed events in view models and models. I am using an abstract base class - NotifiableObject in my example for this. In the constructor, I am using reflection to loop through all properties of the class, when the NotifyProperty attribute is present an anonymous handler is added to the property changed event so a notification is also generated for the decorated property.
Here is the main block of code I'm using to loop through the properties with the notify property attributes, AttachChildren is a recursive method I'm using to do the dirty work, see example solution for more details.
private void WireUpBubblingPropertyChangeNotifications()
{
//this gets all properties, public and private, so you can use the INotifyPropertyChanged interface even for private fields
var props = GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
foreach (var prop in props)
{
var attrs = prop.GetCustomAttributes(true);
foreach (var attr in attrs.OfType<NotifyPropertyAttribute>())
{
AttachChildren(prop.Name, this, attr.PropertyName);
}
}
}
Sample Solution: NotifyPropertyAttributeExample.zip
Edit: Above link is not working, try downloading from here instead:
http://cid-edc8bd4a11230d7b.office.live.com/browse.aspx/.Public/blog
Feel free to shoot me an email with questions/suggestions, I still have some work to do to refine this a bit and provide more functionality. I am working on a project that has a very hierarchical data structure, and found myself constantly needing to wire up event handlers for child objects' property changed notifications, and bubble them up to parent models & view models. This approach has already cleaned up much of our code.
Saturday, October 23, 2010
Thursday, October 21, 2010
Background
I spend my time working with .NET and SQL Server technologies, currently I'm working with WPF 4 & SQL Server 2008 R2, and would eventually like to shift some [more] of my focus into the Silverlight space. Our current project is using Unity and Prism, and I spend much of my time finding creative ways to implement these technologies on large scale without compromising quality or maintainability.
I have a couple ideas/demos I will post soon, one is how I am implementing an undo handler in WPF using MVVM, and a second somewhat related, how to use reflection and property attributes to clean up your spaghetti factory of property change notifications and handlers.
Stay tuned!
Subscribe to:
Posts (Atom)