ChiliYago
ChiliYago

Reputation: 12329

BindableBase vs INotifyChanged

Does anyone know if BindableBase is still a viable or should we stick with INotifyChanged events? It seems like BindableBase has lost its luster quickly. Thanks for any info you can provide.

Upvotes: 14

Views: 31692

Answers (3)

Rohit
Rohit

Reputation: 10296

INotifyPropertyChanged

The ViewModel should implement the INotifyPropertyChanged interface and should raise it whenever the propertychanges

public class MyViewModel : INotifyPropertyChanged
{
    private string _firstName;


    public event PropertyChangedEventHandler PropertyChanged;

    public string FirstName
    {
        get { return _firstName; }
        set
        {
            if (_firstName == value)
                return;

            _firstName = value;
            PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
        }
    }


    }
}

Problem is with ICommand interface as most of the code is duplicated also since it passes string it becomes error prone.

Whereas Bindablebase is an abstract class that implements INotifyPropertyChanged interface and provide SetProperty<T>.You can reduce the set method to just one line also ref parameter allows you to update its value. The BindableBase code below comes from INotifyPropertyChanged, The .NET 4.5 Way - Revisited

   public class MyViewModel : BindableBase
{
    private string _firstName;
    private string _lastName;

    public string FirstName
    {
        get { return _firstName; }
        set { SetProperty(ref _firstName, value); }
    }


}

     //Inside Bindable Base
    public abstract class BindableBase : INotifyPropertyChanged
    {

       public event PropertyChangedEventHandler PropertyChanged;

       protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
       {
          if (Equals(storage, value))
          {
             return false;
          }

          storage = value;
          this.OnPropertyChanged(propertyName);
          return true;
       }

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
      PropertyChangedEventHandler eventHandler = this.PropertyChanged;
      if (eventHandler != null)
      {
          eventHandler(this, new PropertyChangedEventArgs(propertyName));
      }
    }
}

Upvotes: 30

Mariusz Bialobrzeski
Mariusz Bialobrzeski

Reputation: 307

To expand on Rohit's answer, if you are using .NET 4.6 you can take advantage of the Null-conditional operator and simplify the OnPropertyChanged method in the following way:

protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
    this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

INotifyPropertyChanged, The .NET 4.6 Way explains it in more detail.

Upvotes: 3

Emond
Emond

Reputation: 50712

It is not a choice between these two.

BindableBase implements INotifyPropertyChanged.

So if you use BindableBase you will be using INotifyPropertyChanged.

INotifyPropertyChanged is more or less mandatory when implementing MVVM using DataBinding.

Whether to use BindableBase or an other implementation depends on preference and use of Prism.

Upvotes: 7

Related Questions