Can't pass filtered collection as param from VM to VM?

Aug 17, 2013 at 3:27 AM
Edited Aug 17, 2013 at 3:27 AM
Ok, I know we can't pass complex objects in navigation because they can't be serialized in the app life cycle.

I have a page that allows the user to sort by many fields, filter on one field, and filter on empty top level categories.

The filtering and sorting is done in a bunch of lines of code (I am sure you experts could help me out on that, but that is for another thread).

I can't pass just an ID in navigation, and I don't want to duplicate the complex code, so what is the pattern for this?

The page I am navigating to show the collection of child entities of the top level category. Those children are filtered and sorted as well, so I can't just pass an ID of the top level category.

The only thing I can think of is creating properties on the App class or some other globally scoped object so the next vm can do the work.
Aug 20, 2013 at 7:57 PM

As a possible approach I think that instead of passing a collection of already ordered / sorted entities, you could pass a string containing the settings to sort / order the collection (like a query in a URI.) Then, the view model showing the collection would execute the logic to obtain and order the entities based on the settings passed. In order to avoid duplicating code, you could extract the logic of sorting and ordering the collection into a helper class that could be consumed by any view model when needed.

Another approach could be the one you described about saving the collection in a globally available resource from where the view model can retrieve it. This could be performed using for example a Service or a similar implementation.

I hope this helps,

Damian Cherubini
Aug 20, 2013 at 10:11 PM
Damian, thanks for the reply. I think I will use a helper class so I don't duplicate the code to create the filtered collection.