PostSharp NotifyPropertyChanged settings Depends.On

279 Views Asked by At

I have problem with PostSharp NotifyPropertyChanged aspect. I have implementation model architecture as composite pattern. My problem is the PropertyChanged is not raised for TaskGroup.PercentDone.

The Tasks property is List of ITask so there can be TaskGroup or SimpleTask. SimpleTask have auto property PercentDone and fire PropertyChanged event correctly.

For TaskGroup.PercentDone I must use [SafeForDependencyAnalysis], if I don't use I get compile error. But I can't figure out how to use Depends.On()

I try many solutions like these.

Depends.On(this.Tasks.Sum(task => task.PercentDone)); 

foreach (var task in this.Tasks)
{
    Depends.On(task.PercentDone);
}

Depends.On(this.Tasks); 

but I couldn't figure.

class TaskGroup : TaskBase, IComposite<ITaskModel>
{
    [SafeForDependencyAnalysis]
    public override int PercentDone
    {
        get
        {
            //BUG event ProperityChanges is not raised
            int percentDone = this.Tasks.Sum(task => task.PercentDone);
            return percentDone / this.tasks.Count;
        }
        set
        {
            foreach (var task in this.tasks)
            {
                task.PercentDone = value;
            }
        }
    }

    ...

}

I use last version of PostSharp 4.0.36.0.

I failed to find a similar problem on google, but this was quite a common thing, I'm Googling wrong? You can steer me in the right direction please. I very much appreciate your help.

1

There are 1 best solutions below

0
On BEST ANSWER

Currently, the NotifyPropertyChangedAspect supports only property chains in Depends.On, i.e. origin.Property1.Property2 and similar as stated in the documentation. First two expressions would be ignored because the expression needs to begin with a field or this and contain nothing else than property accesses chained once after another.

Generally all "vector" operations are not possible. Hence, you would need to implement such behavior manually. For instance you may want to register to INotifyCollectionChanged of the collection

It is true that PostSharp should at least produce an error message, currently it just ignores the Depends.On. It would be a really nice feature, we may consider it in the future (consider voting for it on our UserVoice Page).