PostSharp & classes that (are supposed to) implement INotifyPropertyChanged

北慕城南 提交于 2019-12-22 09:50:04

问题


I'm new to PostSharp and am trying to figure out whether it can make my life implementing property changed events a bit easier. However, I'm facing the first issue already. I have my interfaces in one Project library, interfaces.dll:

public interface IPerson : INotifyPropertyChanged
{
  string firstName { get; set; }
}

and the implementation in another one, implementations.dll :

[NotifyPropertyChanged] // Postsharp attribute
public class Person : IPerson
{
  public string firstName { get; set; }
}

However, this doesn't compile as PostSharp only interjects is code after compilation, so the INotifyPropertyChanged implementation is missing at compile time and hence, that aborts with an error.

I'm aware I could just set up the interface IPerson without dependency on INotifyPropertyChanged and everything will work just as intended, but isn't this pretty bad code then? Of course I (or rather, postsharp) will implement the INotifyPropertyChanged methods by itself when set up correctly, but there is no formal definition that IPerson must implement this, so it'd be possible to create an implementation without this, which would lead to odd and potentially hard-to-trace errors. And whenever I want to use INotifyPropertyChanged methods, I'd have to cast Person to this type expiclitly, without having any guarantees it does actually implement the interface.

I'm also aware I could just implement the PropertyChanged handler in Person myself, and PostSharp would at least take care of raising the event. But then again I'm using PostSharp for the sole purpose of managing all that by itself, so it I have to start coding half of that stuff again by myself, I'm not sure it'd be worth using.

So how do you guys handle this? Is there any best practice for this?


回答1:


If your class implements INotifyPropertyChange then PostSharp requires OnPropertyChanged to be present in the class and expects that you implement it on your own.

Edit: The implementation of OnPropertyChanged could be something like this:

[NotifyPropertyChanged]
public class Person : IPerson
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

PostSharp cannot raise event universally because of CLI limitations. So it has to use this workaround.

NotifyPropertyChanged can be applied to interfaces as well - all interface implementations will implement INotifyPropertyChanged:

[NotifyPropertyChanged]
public interface IPerson
...
public class Person : IPerson
...

Unfortunately you have to cast to INotifyPropertyChange explicitly but at least you can do that in a safe way:

var person = new Person();
Post.Cast<Person, INotifyPropertyChanged>(person).PropertyChanged += ...

There will be a compile time error when the person instance doesn't implement INotifyPropertyChanged and there will be no warning when it does.




回答2:


I don't think it's bad code to not inherit INotifyPropertyChanged in the interface. To the contrary, most of the time I have an interface for my data, like your IPerson and then two to three implementations, where at least one does explicitly not implement INotifyPropertyChanged because it's a console or server implementation.



来源:https://stackoverflow.com/questions/25362102/postsharp-classes-that-are-supposed-to-implement-inotifypropertychanged

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!