1

I would like to avoid code duplication. Because of C#'s single inheritance, I can't just create BaseNotifyPropertyChanged class:

class BaseNotifyPropertyChanged : INotifyPropertyChanged {
        public event PropertyChangedEventHandler PropertyChanged;

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

Because some classes that need to implement INotifyPropertyChanged also need to inherit from other classes. And I can't make this my very base class because some classes inherit from .NET classes, and this would also pollute all classes in my solution because only some classes actually need the INotifyPropertyChanged(and others just don't need it). One example is DependencyObject. Can't inherit from BaseNotifyPropertyChanged class and DependencyObject at the same time.

In all the books and courses I take, people just keep duplicating the INotifyPropertyChanged implementation in all the base classes: BaseViewModel, BaseModel, BaseDataModel, BaseSynchronizableDataModel, just to name a few.

So I tried to do this:

interface IImplementNotifyPropertyChanged : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;

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

In order to then be able to do this:

class BaseViewModel : IImplementNotifyPropertyChanged {

}

And not actually have to implement INotifyPropertyChanged members in the BaseViewModel, because the default implementation is already in the IImplementNotifyPropertyChanged. And then do the same in all other classes: I'd just add IImplementNotifyPropertyChanged and that would be the end of INotifyPropertyChanged implementation in all the classes that actually need it, without duplicating code and polluting the solution by deriving all my classes from BaseNotifyPropertyChanged class.

But I get these errors when I try to do this: enter image description here

How to fix this error?

Please, help me to implement INotifyPropertyChanged without duplicating the implementation code. Maybe there's some magical way with attributes or something? Like:

[Implements:INotifyPropertyChanged, ImplementNotifyPropertyChanged.cs]
class BaseViewModel {

}

Where Implements:INotifyPropertyChanged would be the indicator of what interface this class implements and the ImplementNotifyPropertyChanged.cs would be the name of the file where it can find members with which to implement it.

KulaGGin
  • 943
  • 2
  • 12
  • 27
  • Have you checked fody? here is weaver using fody https://github.com/Fody/PropertyChanged – jjchiw Jun 16 '20 at 18:50
  • Does this answer your question? [Event Inheritance with C#8 Default Interface Implementation/Traits](https://stackoverflow.com/questions/58796545/event-inheritance-with-c8-default-interface-implementation-traits) as well as [The event can only appear on the left hand side of += or -= dotnetstandard 2.1](https://stackoverflow.com/questions/61558648/the-event-can-only-appear-on-the-left-hand-side-of-or-dotnetstandard-2-1) These questions seems to be an exact duplicates – Pavel Anikhouski Jun 16 '20 at 19:00
  • @PavelAnikhouski It isn't a duplicate because my question is about INotifyPropertyChanged without duplicating code and the question you linked as the duplicate is about Event Inheritance with C#8 Default Interface Implementation/Traits. For me it doesn't matter if the solution to remove duplication of INPC code is to use multiple inheritance, C# 8 interface DIMs, attribute parameters, some kind of injection or any other, while in the question you linked as the duplicate it's all about Event Inheritance with C#8 Default Interface Implementation/Traits. – KulaGGin Jun 16 '20 at 19:54
  • The first duplicate is exactly about INPC implementation in case of using DIM – Pavel Anikhouski Jun 16 '20 at 20:00
  • @PavelAnikhouski The first question you linked is about Event Inheritance with C#8 Default Interface Implementation/Traits and the second one is about the error The event can only appear on the left hand side of += or -=. My question is neither about DIMs nor about the error `The event can only appear on the left hand side of += or -=`. My question is about implementing INPC without duplicating code. These 2 questions only answer why I got the error `The event can only appear on the left hand side of += or -=` and why we can't use DIMs to implement INPC without duplicating code. – KulaGGin Jun 16 '20 at 20:06
  • @PavelAnikhouski But answers in these 2 questions you linked don't answer the main question you see in the title of this question:"How to implement INotifyPropertyChanged without duplicating code in all classes that need it(and not any other)?". They don't answer this question, therefore my question is not a duplicate of those 2. – KulaGGin Jun 16 '20 at 20:07

0 Answers0