Allesad
Allesad

Reputation: 256

Tracking of collection changes with Rx in MVVM

I have a cross-platform project written using MVVM-pattern (no specific frameworks used, just self-written implementation). Project has several independent modules each of which has several pages. Each of pages has ViewModel and some sort of manager who's responsible for data-oriented logic (get, save, delete, transform etc.). So the data-flow looks about this:

VM -> Manager -> Service -> Manager -> VM

When VM is loaded it's asking manager for data. Manager perform service call, get data, construct collection of models from DTOs, return this collection to ViewModel which converts collection of models to collection of ViewModels to be rendered in the list.

Now I'm looking for a way to implement this kind of logic using Rx. Most of pages have one main list to be edited (items inserted, deleted, modified) and several support collections (providers for some combo-box to choose value from). Support collections can be easily retrieved via standard async/await calls or via converting tasks to Rx - they are no problem. But the modifiable list is. I just cannot figure out the way to track changes in this list for the entire life of the page without breaking Rx logic. I have options to subscribe to:

but I suppose I have to subscribe for IObservable<Model> because I need a way to track individual changes. And I need a way to modify this collection from other methods like Add, Delete or Edit. So should I create IObservable via Observable.Create (or other method) and store IObserver somewhere inside Manager to call OnNext or OnError in other methods? But it doesn't looks like an Rx-way-to-do. Do you have any suggestions about my problem? Any advise appreciated. Thanks.

PS: you may say Rx isn't the best way to solve my problem with tracking modifiable list because it's not endless stream of events and I have to push modifications by myself but Rx has very convenient way of filtering data and handling errors so I'm really looking forward to implement it in the application.

Upvotes: 2

Views: 664

Answers (2)

Gluck
Gluck

Reputation: 2962

ReactiveList works, although it doesn't have any single Rx stream exposed which provides all modification events.

See this related question, which answer is to use an IObservable<IObservable<Model>>, with each inner observable representing one item from your list, along with its modifications (and deletion - when it completes).

Upvotes: 1

jamespconnor
jamespconnor

Reputation: 1412

Look at ReactiveList<T> - it's part of ReactiveUI framework which we've used to great effect with MVVM / WPF UIs.

Upvotes: 1

Related Questions