Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of compiler magic do we need more?

I develop lot view models which are:

1) All have to implement INotifyPropertyChanged to be bindable to UI.

2) Property setters have to raise PropertyChanged on change.

3) PropertyChanged event has to provide proper property name.

If you (like me) tied of writing something like this:


public string Name 
{
  get 
  { 
    return _name; 
  }
  set 
  { 
    if (_name != value) 
    {
      _name = value;
      RaisePropertyChanged("Name");
    }
  }
} 

Then refactor this method like this and sometimes forget to update property name literal:


string _fundName;
public string FundName 
{
  get 
  { 
    return _fundName; 
  }
  set 
  { 
    if (_fundName != value) 
    {
      _fundName = value;
      RaisePropertyChanged("Name");
    }
  }
} 

And then spend a day to debug why your UI is not refreshing and databinding doesn't work properly.

Then all we need is some kind of magic.

What if I just need to write this:


[Magic] // implicit transformation
public string FundName { get; set; }

or if I have many properties:


[Magic]
public class MyViewModel
{
  public string FundName { get; set; }
  public string FundType { get; set; }

  [NoMagic] // suppress transformation
  public int InternalId { get; set; }
}

So I have just developed a MSBuild task to do this magic after the build (http://kindofmagic.codeplex.com).

The question is, what kind of magical postprocessing would you like more?

Does automatic implementation of INotifyPropertyChanging makes sense?

like image 313
Lex Lavnikov Avatar asked Dec 14 '25 22:12

Lex Lavnikov


1 Answers

Try this

http://code.google.com/p/notifypropertyweaver/

  • No attributes required
  • No references required
  • No base class required

Here is my blog article about it

http://codesimonsays.blogspot.com/2010/11/attempting-to-solve-inotifypropertychan.html

It supports the attributes you request

  • NotifyPropertyAttribute (notify for a property)
  • NotifyForAllAttribute (notify for all properties on a type)
  • NotifyIgnoreAttribute (do not notify for a property or type)
  • AlsoNotifyFor (Allows the injection of notify code that points to a different property)

Although these are option and designed for fine tuning. Most injection is done by convention through analyzing the existing IL.

like image 152
Simon Avatar answered Dec 16 '25 15:12

Simon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!