Repeated execution of the same event

I am writing several classes, and I want to make them "data binding compatible" (for WPF or even, probably, more rare WinForms) by implementing INotifyPropertyChanged .

The problem is re-code. I really copy-paste the same method over and over again (I'm not joking).

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

I had this problem for a while, but today it just keeps repeating itself again and again, so I hope you could help me with a solution. I have almost a dozen classes that have this method, and I really hate repeating this piece of code.

I was thinking of creating a base class that will implement it ( NotifyPropertyChangedObject for the name may be), but this is probably a bad idea that will really limit my classes without multiple inheritance.
I also thought of an extension method, but I would like to use it as a protected method, so this will not work either.

What can be done to fix this problem?

+1
source share
3 answers

Having a base class is a method that even MVVM MVVM libraries do. There is no shortage of this.

Yes, you can only have one base class for the C # class, but it implements several interfaces. For your case, all you have to do is say that the base class implements INPC and calls it ViewModelBase

Now, if you currently have class X inherit from Class A , just make A inherit from ViewModelBase .

Thus, you are thus forcing your current base classes to inherit from this new INPC rendering class, and you do not have code duplication to implement INPC in any of your derived classes

Update

In your special case, when for some reason you are tied to one already having a different base class, and with the restriction of not saying something like a public implementation of INPC passed to this object as a member variable,

You can try a look at this:

Fody and in particular addon PropertyChanged - Addon

This will hopefully help you, since it implements the INPC implementations themselves, so you don’t need to copy the paste code, and then it allows you to get some kind of custom base class (you still need to specify INPC, but this is just the interface here)

+3
source

I usually bind my view to an object such as a view model, that is, to an object that contains all the data that will be needed for the view. This simplifies the operation of the system, since the view must be associated with only one object.

I will usually have my business objects map data to the view model and expose one event to notify the view model that its state has changed, and at that point the view model will trigger the corresponding property notifications for the view. i.e. you only need to implement the method described above once for the view / view model.

Take a look at the MVVM model if you haven't already. The above is just one of many approaches and really is my interpretation, with which some may disagree and which may or may not correspond to your specific scenario.

0
source

To add @viv to the answer. If you have already decided to use Fody + PropertyChanged I recommend avoiding the base class. Since PropertyChanged implements the entire implementation of INPC, you really have very little to have a base class. Actually more pain than value. Just add the PropertyChanged.ImplementPropertyChanged attribute to your class, and the rest will be done for you.

 [ImplementPropertyChanged] public class Person { public string Name { get; set; } } 

Regarding classes that you do not own, and therefore can be done to implement INPC. The best approach is to create a duplicate class, which is a simplified version of each class that you want to bind to. You can read and write values ​​when loading and ending browsing. Or, if you want to be attached to certain properties, just put these claims in your main representation model.

0
source

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


All Articles