The Dashboard uses just one MVVM Class
Communicate between a View and ViewModel using commanding
Create a ViewModelBase class to handle INotifyPropertyChanged notifications
Create a ViewModel class that derives from ViewModelBase and handles data interactions
Bind a ViewModel class to a View in XAML
Bind Silverlight controls to ViewModel properties
The View in MVVM represents the Silverlight screens that you build. This includes the XAML files and the code-beside files that are responsible for showing data to end users. The View’s responsibilities include displaying data and collecting data from end users. A given View isn’t responsible for retrieving data, performing any business rules or validating data.
In this case the View is the one XAML file I called the Dashboard
The ViewModel acts as the middle-man between the View and the Model. It’s responsible for aggregating and storing data that will be bound to a View. For example, a ViewModel may contain a List<State> property and a List<Person> property that may be bound to two ComboBox controls in a View. The ViewModel will retrieve the values held by these two properties from the Model. By using a ViewModel the View doesn’t have to worry about retrieving data and knows nothing about where data comes from.
When developers first start building Silverlight applications they typically add all of the code for the application into the XAML’s code-beside file (MainPage.xaml.cs for example). Although this approach certainly works, by following the MVVM pattern you can take advantage of several inherent benefits including better code re-use, simplified maintenance, more modular code and enhanced testing support. I’ll focus on the overall benefits achieved by building applications that are based on the MVVM pattern throughout the rest of this article.
The Extended Model
Notice how I use an Extended Model class for each Table, to add additional validation capabilities but most important, to create the Calculated Fields
Once the Model and my Extended Model are ready to go the View and ViewModel classes can be created. As mentioned earlier, a View represents a Silverlight screen that end users interact with which includes the XAML file and the associated code-beside file. Rather than adding all of the code to call the Model and retrieve data directly into the View’s code-beside file, the View will rely on a ViewModel class to retrieve data and then bind to the properties of the ViewModel.
The Model-View-ViewModel (MVVM) pattern provides a flexible way to build Silverlight applications that promotes code re-use, simplifies maintenance and supports testing. The pattern consists of three different components including:
The Model (entities and code on the server)
The View (the Silverlight screens)
The ViewModel (the glue between the Model and the View).
An example of the how the Model, View and ViewModel components relate to each other is shown on the next Dashboard:
the Patient is Master of Consultations
Consultations is master of Diagnosis
Diagnosis is master of Prescriptions
Prescriptions of Exams
I created the following video to illustrate the pattern
There is no voice on the video but written notes will guide you through.
Here is the Sample Application
I am just another Silverlight MVVM Developer.
I hope you enjoy it as much as I have !!