reactivemarbles / DynamicData

Reactive collections based on Rx.Net
http://dynamic-data.org
MIT License
1.71k stars 182 forks source link

Handling Cache of INotifyPropertyChanged objects #106

Closed kkneip closed 6 years ago

kkneip commented 6 years ago

Hi,

I have a question regarding a SourceCache containing objects of instances that implement INotifyPropertyChanged. I'd like to use aggregate function to sum up a decimal property called "Price". I expect the sum to change if the "Price" of one included object instances in the cache changes. But it doesn't!

What am I doing wrong? Does DynamicData not support a scenario like this? Running "Refresh" on an object seems not to have any effect, either.

Many thanks in advance!

modplug commented 6 years ago

I'm pretty sure that the AutoRefresh() operator will give you updates whenever an underlying property changes.

Check out the examples in the snippets repo.

https://github.com/RolandPheasant/DynamicData.Snippets/tree/master/DynamicData.Snippets/AutoRefresh

kkneip commented 6 years ago

Thanks modplug,

unfortunately it's not working on my side. I'll try to get the snippet working to see what's going wrong.

Additional ideas are much appreciated.

kkneip commented 6 years ago

I'll checked out "AutoRefresh" which works fine on sorting and filtering. But seems not to influence aggregation.

Any ideas how to get it working, though?

RolandPheasant commented 6 years ago

The aggregation operators were intentionally written to be light weight and do not maintain any state. That is why Refresh / AutoRefresh have no effect.

However there is an easy workaround. Use ToCollection() and use linq to objects to sum the items

myDataSource.ToCollection().Select(items = items.Select(x=>x.Price).Sum());

You will still need to use Refresh / AutoRefresh to trigger on notify property change.

Please let me know if that works.

kkneip commented 6 years ago

Thanks a lot, Roland!

Now it's behaving as expected!

RolandPheasant commented 6 years ago

I'll close this one then.

If you want an invite to the Slack forum send me your email address to roland@dynamic-data.org and I will invite you - it's easier to ask questions there,