Continuing on with our series on using the dynamic features of C#, let me share how I’ve used it to address an issue that bugged me several years ago.

When creating ViewModels for WPF applications, I got fed up with having to implement INotifyPropertyChanged and pollute the classes like this:

public class InvoiceItemViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

private string _productName;

public string ProductName
{
get { return _productName; }
set
{
if (_productName != value)
{
_productName = value;
OnPropertyChanged("ProductName");
}
}
}
}

I didn’t want to see 12 lines in the code for each property exposed. Instead, I wanted to have something like this:

    public class InvoiceItemViewModel
{
public string ProductName { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
public decimal Total => Price * Quantity;
}

That’s as simple as the class can get. That’s how I wanted my ViewModels to look like. Notice it doesn’t even inherit from any baseclass, and it doesn’t have any attributes decorating the class (the way I also did at some point when using IL weaving tools such as PostSharp).

WPF databinding is late-bound, so as long as the property exists on the object during runtime, everything works just fine. When it comes to changing data in the UI and having WPF broadcast the message that the some properties have changed, well, the ViewModel needs to raise the PropertyChanged event. So, I decided to go ahead and create a DynamicDecorator class, which would decorate my ViewModels with the “notify property changed” behavior.

Before assigning my ViewModel to the UI’s DataContext property, I’d use the decorator, somewhat like this:

[code language=”plain”]DataContext = new DynamicDecorator(new InvoiceItemViewModel());[/code]

In reality, I had an interceptor in my repository that’d do the decoration part automatically for me, but that’s beyond the point here.

In other words, say I had the following code somewhere in my WPF app:

[code language=”plain”]var item = GetItem();
item.ProductName = “Banana”;[/code]

The implementation of GetItem could look like this:

[code language=”plain”]InvoiceItemViewModel GetItem()
{
    return new InvoiceItemViewModel();   
}[/code]

But then again, since WPF’s databinding is late-bound, GetItem, in my case, looked something like this:

[code language=”plain”]dynamic GetItem()
{
    return new DynamicDtoDecorator(new InvoiceItemViewModel());
}[/code]

The DynamicDtoDecorator simply intercepts access to the properties on the ViewModel it decorates, and it raises PropertyChanged when appropriate. I’ll show and explain the implementation of that class on my next post.

Wondering why the class is named Decorator, instead of Proxy? That’s also coming up in another post. Stay tuned!

4 responses to “Fun with C#: How to get rid of INPC using Dynamic – Part 1”

  1. […] « Fun with C#: How to get rid of INPC using Dynamic – Part 1 […]

  2. […] case you missed any part of this series, here are the links to Part 1, Part 2, Part 3 and Part […]

  3. […] I mentioned back in this post, I wanted to leverage WPF’s data-binding capabilities. Using those capabilities to simply show […]

  4. […] you’ve been following my series on “How to get rid of INPC using Dynamic” (starting here), you know my example app here uses a DynamicDtoDecorator that takes a POCO ViewModel, enhances it […]

Leave a Reply

Trending

Discover more from Claudio Lassala's Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading