0
votes

I am using the PostSharp solution for INotifyPropertyChanged by decorating my business classes with the [NotifyPropertyChanged] attribute.

All works fine.

Now I wrote a custom aspect that handles property changes so that I get some custom flags set when some special properties change. This aspects is named [HandlePropertyChanged] and works when used alone.

Now I try to use both aspects in combination. As I read on the PostSharp page I can manually order them to ensure a fixed order by using

  [NotifyPropertyChanged(AspectPriority = 0)]
  [HandlePropertyChanged(AspectPriority = 1)]

In this case, I can build my solution, but because "NotifyPropertyChanged" runs before "HandlePropertyChanged", the changes on my properties are already done and the custom logic does not run correctly.

If I try this

  [HandlePropertyChanged(AspectPriority = 0)]
  [NotifyPropertyChanged(AspectPriority = 1)]

my build fails with the error at the bottm of the text (see below).

Best would be to simply do what NotifyPropertyChanged does in my custom aspect and forget about the PostSharp aspect

Is this possible?

0: Error C:\Source\WAVE\WAVE.Data.Contracts\Entities\Base\EntityBase.cs (17,16) PS0115: Conflicting aspects on "TopMotive.WAVE.Data.Contracts.Entities.Base.EntityBase`1": according to aspect dependencies, transformation "Instantiation of aspect PostSharp.Patterns.Model.NotifyPropertyChangedAttribute" should be located both before and after transformation "Instantiates binding collection for field "PostSharp.Patterns.Model.NotifyPropertyChangedAttribute/LocationBindings".".

2
The NotifyPropertyChanged aspect included in PostSharp Model library is quite complex and it's probably not economical to re-implement it in full. I believe it's worth to look for the solution with aspect ordering first. What does your HandlePropertyChanged aspect look like? Are you using location interception? Any other advises? It would be nice to have a sample that demonstrates the issue. - AlexD
Yes I was using Location interception and that may have caused the problem. Just to make sure: is there any chance to get the source code of NotifyPropertyChanged ? Maybe I could add my stuff there ... - klawusel
The PostSharp team investigated the issue with NPC aspect ordering and filed a corresponding internal bug. We will notify you once the bug is fixed. In the meantime you can try to use aspect dependencies instead of AspectPriority to order the aspects (doc.postsharp.net/aspect-dependencies). - AlexD

2 Answers

0
votes

This bug is fixed in PostSharp 5.0.52 and PostSharp 6.0.16 RC.

0
votes

Try superior and free alternative: Stepen Cleary's Calculated Properties. https://github.com/StephenCleary/CalculatedProperties/blob/master/README.md

I used both in production and found it to be far better than PostSharp's aspect.

Also from PostSharp docs:

"If a property getter calls a virtual method from its class or a delegate, or references a property of another object (without using canonical form this.field.Property), PostSharp will generate an error because it cannot resolve such a dependency at build time. The same limitations apply when your property getter contains complex data flows, such as loops, or calls to methods (except property getters) of other classes. When this happens, you can either refactor your code so that it can be automatically analyzed by PostSharp, or you can take over the responsibility for analyzing the code"

None of those limitations apply to Calculated Properties. It can do loops, virtual methods, LINQ to objects, basically any runtime dependencies that you can imagine doesn't matter how indirect. Dependency graph rewires itself at runtime and just works without any ceremony. They are also fast.