In a too simplistic answer, your ViewModel should contain LOGIC to control the display of your view, as well as how it is allowed to interact with the model or data.
Events, such as receiving data, saving and deleting, are intercepted through the command mechanism and placed in the ViewModel, where they can be tested. Handling dirty events is also a ViewModel responsibility. As for who calls the ViewModel, you trust the Calling to Binding features available in WPF and Silverlight.
In the ViewModel, he is still looking for best practices and ensuring that you have a DataAccess layer abstracting your data source and possibly using a repository template to abstract it.
The ViewModel life cycle can be as simple as the following:
- Designer called by view
- GetData Method Called by ViewModel Ctor
- Data Received and Transferred to an Existing Binding Data View ObservableCollection Property
However, since you are likely to have many moving parts inside the Ctor VM, including the Data Repositories interface, you probably want to work with IoC. This will make the ViewModel life cycle closer to ...
- View / ViewModel (depends if you are viewing or viewing the Model first) pulled from IoC
- IoC Handles View-ViewModel (Based on Convention)
- Data repository embedded in ViewModel
- GetData Method Called by ViewModel Ctor
- Data Received and Transferred to an Existing Binding Data View ObservableCollection Property
This may seem like more steps, however with the IoC container you really just call one method, such as IoC.Get (), and the rest of the steps are automatically connected based on the conventions applied.
source share