Quantcast
Channel: Nove Rosso
Viewing all articles
Browse latest Browse all 10

INotifyPropertyChanged

$
0
0

Act 1 Scene 2 – Naming Conventions

Thinking a lot about INotifyPropertyChanged I visited Dan Wahlin’s and John Papa’s talks on the DevConnections in Karlsruhe/Germany. They also “discussed” about the Naming convention 😉

I, for my opinion, see the following naming as the best:

  class MyViewModelBase : INotifyPropertyChanged

  {

    public event PropertyChangedEventHandler PropertyChanged;

 

    public void NotifyPropertyChanged(string name)

    {

      var handeler = PropertyChanged;

      if(handeler != null)

        handeler(this, new PropertyChangedEventArgs(name));

    }

  }


The consumer of the event uses OnPropertyChanged as of a button click, the handler would be OnClick.

  class MyViewModel : MyViewModelBase

  {

    public MyViewModel()

    {

      PropertyChanged += OnPropertyChanged;

    }

    void OnPropertyChanged(object sender, PropertyChangedEventArgs e)

    {

      throw new NotImplementedException();

    }

  }

So why I think it should be this way? Think about an interface ISayHello.

  interface ISayHello

  {

    event Action Hello;

  }

The method to fire the event should be SayHello. Nobody would say FireSayHello, RaiseSayHello or anything in the real world. What else useful than raise/fire could be done with an event? So those words are redundant.

  class HelloSayer : ISayHello

  {

    public event Action Hello;

    public void SayHello()

    {

      var handler = Hello;

      if(handler != null)

        handler();

    }

  }
The listener of again uses OnHello to handle its listening.
  class Listener

  {

    private readonly HelloSayer friend;

    public Listener()

    {

      friend = new HelloSayer();

      friend.Hello += OnHello;

    }

    private static void OnHello()

    {

      Debug.Print("Goodby");

    }

  }

Conclusion

Every time an interface is like “INotifyPropertyChanged” => “I notify (you for every) property changed” the rising method should be the name without the interface-I and the event itself should be named like the fact the event informs about.

To be discussed (#nossued)


Viewing all articles
Browse latest Browse all 10