I appreciate this is a bit of a general question, but I thought someone might be able to direct me.
I've written a sample application to show my students an example of MVVM . One of the motivations for MVVM is loose coupling and hence easier unit testing.
For my ViewModel, everything is working out so far except for one small detail - Commands.
In my view model constructor, I have the following lines which are dependent on Xamarin.Forms:
In the constructor of the ViewModel
FetchNextSayingCommand = new Command(execute: async () => await DoFetchNextMessageCommand(), canExecute: () => NetworkIsIdle);
where DoFetchNextMessageCommand()
simply asks the model to fetch a new string from Azure and NetworkIsIdle
is a bool
property that is false
during a network transaction.
When listening for an event from the Model to say the network status has changed:
(Command)FetchNextSayingCommand).ChangeCanExecute()
I think my question reduces down to two fundamentals:
- Should a ViewModel aspire to have no (tightly coupled) dependencies on Forms or am I simply being too purist?
(i.e. such that I can comment outusing Xamarin.Forms;
from the head of my source) - Is there a considered best-practice for unit testing Commands?
(I have my own ideas, but I suspect someone else has already solved this)
Many thanks,
Nick