My reactive extension skills are weak and stretched thin. Here is the situation:
-
In my MVVM I have ObservableCollection which binds to my Xaml listbox. That’s normal stuff.
public ObservableCollection<IExtractedFileInfo> ExtractedFileInfoList -
In my business layer I have a long running process that queries my EntityFramework and is called with a signature something like
IList<IExtractedFileInfo> ExtractedFileInfoListFetch(string location)
My usual solution to passing the results to the listbox would be to get the output from the long running process, create a new ObservableCollection, and set it equal to the property in my MVVM like this
var extractedFileInfoList = _businessFacade.ExtractedFileInfoListFetch(location);
ExtractedFileInfoList = new ObservableCollection<IExtractedFileInfo>(extractedFileInfoList);
Of course this plops the entire list of results into the Xaml listbox at the end of the process. I would like to implement this process so the Xaml listbox fills as the ReactiveExtensions pull in each IExtractedFileInfo object. I just cannot find any examples of how to orchestrate this process. I’m confused by the separation between the layers and how to get them to work together using ReactiveExtension. I think I can write the query in my business layer, but how do I get the results to transfer up to the MVVM and showup on my Display thread? Do I pass the ObservableCollection property from my MVVM down to my business layer and have it populated there? Or, do I pull all my queries up to the MVVM and run the whole process from there? As you can see, I’m going down for the third time.
Your service layer can expose an
IObservable<IExtractedFileInfo>. Your view model would then subscribe to that and push any items it receives into anObservableCollection<IExtractedFileInfo>(to which your view is bound). Of course, it will need to subscribe on the UI thread.Your service layer then has no need to hold onto items as they are received, and your VM is free to use all the goodness of reactive extensions to do things like batch items together or delay their application.