Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic INotifyPropertyChanged Implementation through T4 code generation?

I'm currently working on setting up a new project of mine and was wondering how I could achieve that my ViewModel classes do have INotifyPropertyChanged support while not having to handcode all the properties myself.

I looked into AOP frameworks but I think they would just blow up my project with another dependency.

So I thought about generating the property implementations with T4.

The setup would be this: I have a ViewModel class that declares just its Properties background variables and then I use T4 to generate the Property Implementations from it.

For example this would be my ViewModel:

public partial class ViewModel
{
    private string p_SomeProperty;
}

Then T4 would go over the source file and look for member declarations named "p_" and generate a file like this:

public partial class ViewModel
{
    public string SomeProperty
    {
        get
        {
            return p_SomeProperty;
        }
        set
        {
            p_SomeProperty= value;
            NotifyPropertyChanged("SomeProperty");
        }
    }
}

This approach has some advantages but I'm not sure if it can really work. So I wanted to post my idea here on StackOverflow as a question to get some feedback on it and maybe some advice how it can be done better/easier/safer.

like image 789
chrischu Avatar asked Jun 03 '10 17:06

chrischu


People also ask

How does the INotifyPropertyChanged generator work?

This generator is generating the property on your behalf, and automatically implements the INotifyPropertyChanged as well as a partial method that you can optionally implement to get local notification on changes. For a more detailed explanation of the generation process, head to this earlier article.

How do I implement INotifyPropertyChanged for the firstname property?

So here’s the basic implementation of INotifyPropertyChanged for the FirstName property: #region Implement INotifyPropertyChanged We won’t go over the specifics, but it’s a fairly easy interface to implement, just fire the PropertyChanged event.

What is the purpose of the different syntaxes for INotifyPropertyChanged?

Each of these syntaxes affords us total control of our INotifyPropertyChanged scenario without having to change the look, feel and flow of our code.

How does autonotifypropertychange work with codedom?

The AutoNotifyPropertyChange.TypeFactory class uses CodeDom to internally generate a subclass of the model and wire the code in the property setter. Class is internally compiled and returned as a type. and see the autogenerated class (you will never have to use this code, it is just to show what happens behind the scenes):


1 Answers

Here's a great post by Colin Eberhardt on generating Dependency Properties from a T4 by inspecting custom attributes directly from Visual Studio with EnvDTE. It shouldn't be difficult to adapt it to inspect fields and generate code appropriately since the post contains simple utility methods to browse code nodes.

Note that when using T4 from VS, you shouldn't use Reflection on your own assemblies or they'll get locked and you'll have to restart Visual Studio in order to rebuild.

like image 90
Julien Lebosquain Avatar answered Oct 26 '22 11:10

Julien Lebosquain