Hi Paul
I've been thinking about this a bit more, and rather struggling. I'm not sure I like 'new'ing this SearchResultsViewModel.I don't mind 'new'ing ViewModels which are simple derived representations of my models, but not the ViewModels behind my pages.
To summarise the situation:
- the SearchViewModel is the ViewModel for the SearchPage, which simply collects the parameters for the search
- the SearchResultsViewModel is the ViewModel for the SearchResultsPage. This executes the Search over the ISearchApi. It can be navigated to from either the SearchPage or the Search charm.
The SearchResultsViewModel obviously needs the ISearchApi. I generally like to inject this into the constructor this via IOC rather than fetching it from the IOC container. In my experience, passing around the IOC container, or using a CommonServiceLocator causes a loss of visibility of what a class' dependencies are - the IOC container becomes a "bag of holding", and you can't see what's the in the bag.
So, if I were to 'new' the SearchResultsViewModel, then I have to explicitly pass in the ISearchApi into the constructor. But then my SearchViewModel would then have to the ISearchApi injected simply to be able to pass it on. From my perspective, the SearchViewModel shouldn't have to know about all the SearchResultViewModel's dependencies.
What I've found to be unique about ReactiveUI's routing, is that the Navigate.Execute() takes a class instance. A quick look at how other frameworks are doing routing shows that the Navigate() methods often take a Type. I think the following is what I need:
From SearchViewModel:
screen.Router.Navigate.Execute(ISearchResultsViewModel, searchParameters); // Execute method signature is Execute(Type, object parameters)
Then in my SearchResultsViewModel:
this.WhenNavigatedTo().Subscribe(x => {}); // x is my searchParameters
In this case, WhenNavigatedTo would be something like:
IObservable<object> WhenNavigatedTo<TViewModel>(this TViewModel viewModel) where TViewModel : class, IRoutableViewModel
I'm not sure if my ideas are completely down the wrong path, that's why I'm posting here to get your (and hopefully the opinion of others out there!). If my ideas are not wrong, I have two options:to implement something like I need myself (I'd be injecting my own router to my ViewModels), or I could bring in another MVVM framework to help with this (which I'm trying to avoid).
Thanks for reading, and I appreciate any input on my ideas.
Cheers
Wayne