What kind of compiler magic do we need more?

I develop models for viewing lots, which:

1) Everyone should implement INotifyPropertyChanged to bind to the user interface.

2) Property installers must raise PropertyChanged upon change.

3) The PropertyChanged event must contain the correct property name.

If you (like me) are involved in writing something like this:


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

Then reformat this method like this, and sometimes forget to update the property name literal:


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

And then spend a day debugging why your user interface is not refreshing and data binding is not working 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; }
}

, MSBuild (http://kindofmagic.codeplex.com).

, ?

INotifyPropertyChanging?

+3
5

, , DependancyProperties . , , , , , . , .

: , . , , .

:

[DpDefault("The Void")]
[DpCoerce(new CoerceValueCallback(MainWindow.CoerceAddress))]
[DpChanged(new PropertyChangedCallback(MainWindow.ChangeAddress1))]
[DpChanged(new PropertyChangedCallback(MainWindow.ChangeAddress2))]
[DpOptions(FrameworkPropertyMetadataOptions.Inherits)]
public string Address {
    get { return Dp.Get<string>(); }
    set {
        if (Dp.Get<string>() != value) {
            Dp.Set(value);
            PostOffice.SendMailToTheBoss("I moved!");
        }
    }
}

:

public string Address {
    get { return (string)GetValue(AddressProperty); }
    set {
        if ((string)GetValue(AddressProperty) != value) {
            SetValue(AddressProperty, value);
            PostOffice.SendMailToTheBoss("I moved!");
        }
    }
}

public static readonly DependencyProperty AddressProperty =
    DependencyProperty.Register("Address", typeof(string), typeof(MainWindow),
        new FrameworkPropertyMetadata((string)"The Void",
            FrameworkPropertyMetadataOptions.Inherits,
            new PropertyChangedCallback(MainWindow.ChangeAddress1)
                + new PropertyChangedCallback(MainWindow.ChangeAddress2),
            new CoerceValueCallback(MainWindow.CoerceAddress)));

, "DpDefault", , , , . :

:

[DpDefault("The Void")]
public string Address { get; set; }

:

public string Address {
    get { return (string)GetValue(AddressProperty); }
    set { SetValue(AddressProperty, value); }
}

public static readonly DependencyProperty AddressProperty =
    DependencyProperty.Register("Address", typeof(string), typeof(MainWindow),
        new UIPropertyMetadata((string)"The Void"));
+1

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

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

  • NotifyPropertyAttribute ( )
  • NotifyForAllAttribute ( )
  • NotifyIgnoreAttribute ( )
  • AlsoNotifyFor ( , )

. .

+4

"" , . . , - "", ? :)

, , . .NET.

+1

-, , - ... ( Caliburn Micro).

 public virtual void NotifyOfPropertyChange<TProperty>(Expression<Func<TProperty>> property) {
            NotifyOfPropertyChange(property.GetMemberInfo().Name);
        }

.

NotifyOfProperyChange (() = > this.PropertyName);

, .

Caliburn Micro - , , , MVVM Silverlight/WPF!

0

AOP (Aspect Oriented Programming), PostSharp: http://www.richard-banks.org/2009/02/aspect-oriented-programming.html ( v1.x) http://www.sharpcrafters.com/solutions/ui#data-binding ( v2.0)

I used PostSharp to implement INPC in several projects, and it worked out very well, the code is much cleaner and more convenient (it adds a few seconds to compile)

0
source

Source: https://habr.com/ru/post/1777945/


All Articles