Look At AOP : Getting Rid Of INPC Using Aspects

Some of you may know me, and know that I am quite into WPF, some of you may even be into WPF yourselves and come to my blog to see what posts I have here to do with WPF.

Well I just released a new article all about Aspect Orientated Programming (AOP).

I looked at 4 different AOP frameworks:

  • Castle
  • Unity
  • PostSharp
  • LinFu.AOP

Now as a WPF developer I use the INotifyPropertyChanged interface (INPC) an awful lot.

I have to say a lot of the time I am merely checking the new value does not equal current value and then calling the INotifyPropertyChanged interfaces PropertyChanged event. Not all the time mind, there are cases where something different happens in the setter….but 80% of it is bulk standard, check property and call PropertyChanged  changed event.

Which looks like this typically:

public class MainWindowViewModel : INotifyPropertyChanged
{

    private int someProperty;
    public int SomeProperty
    {
       get { return someProperty; }
       set
       {
         if (someProperty != value
         {
        someProperty=value;
        RaisePropertyChanged("SomeProperty");
         }
       }
    }


    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
       if (PropertyChanged != null)
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

}

 

Now I got thinking if we could use AOP to come up with something like this, where we use some special attribute to mark standard auto properties as INPC, where the AOP framework would fire the INPC PropertyChanged event whenever a property marked up with one of these attributes was set.

public class MainWindowViewModel : INotifyPropertyChanged
{

    [INPCAttribute]
    public virtual string DummyProp1 { get; set; }

    [INPCAttribute]
    public virtual string DummyProp2 { get; set; }


    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }Which got me thinking even more, perhaps if an AOP framework could do that, it may even be able to implement the entire INotifyPropertyChanged interface for me to, which would lead to classes like this:

public class DummyModel
{
    [AddINPCAttribute]
    public virtual string DummyModelProp1 { get; set; }

    [AddINPCAttribute]
    public virtual string DummyModelProp2 { get; set; }

    public virtual string DummyModelProp3 { get; set; }
}

 

The good news is that I did manage to achieve these results, and if you are interested you should read the entire article using this link:

http://www.codeproject.com/KB/miscctrl/Aspects.aspx

Please do not forget to vote…….thanks

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Advertisements

4 thoughts on “Look At AOP : Getting Rid Of INPC Using Aspects

  1. […] do with WPF. Well I just released a new article all about Aspect Orientated Programming (AOP). I… [full post] sacha sachabarber.net c#mvvmsilverlightwpf 0 0 0 0 […]

  2. Jouke van der Maas says:

    That is amazing. I will surely be using this in the future.

  3. Thanks few great info here keep up the good job. I really give a more constructive comment as I’m a bit outer of my deph but i will be checking back here for advance updates. Goodluck

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: