This document provides recommendations for best practices when developing Silverlight 3 applications using the Model-View-ViewModel (MVVM) pattern. It discusses using the MVVM pattern to separate application logic, data, and user interface. It also recommends specific practices like using an ObservableCollection for data binding, implementing INotifyPropertyChanged, and attaching view models to views to associate data and behavior. The document provides tips for styles and resources, data annotations, and performance optimizations in Silverlight development.
5. Model View Controller/Presenter Pattern Problem: Decoupling data display logic from the business logic that maintains and updates it Solution: MVC Pattern separates the modeling of the domain, presentation, and actions based on user input into three separate classes: Model: Manages behavior/data of the application domain responds to requests for information about its state (usually from View) responds to instructions to change its state (usually from Controller). View: Manages the display information Controller: Informs the Model to change its state by interpreting inputs from keyboard, mouse and external instructions.
6. Developed by Martin Fowler, and is an extension of MVP Again, the View is separated from its behavior and state A Presentation Model is introduced, which acts as an abstraction of the view – so the view is merely a rendering of the Presentation Model View has a reference to Presentation Model, but Presentation Model has no reference to view Presentation Model
7. MVP/MVC nice for web and thick client Good for ASP.NET,WinForms, WPF (as a start) Good for Silverlight as a start What you’re used to
8. Identical to Presentation Model, but engineered by John Gossman to utilize WPF Dependency Properties and Commands to bind UI to abstraction Ideal as it allows abstraction, called the ViewModel, to be tested Also allows for any UI to bind to ViewModel fields to show and modify the data XAML controls / Elements bind to properties in the ViewModel class Use Model-View-ViewModel Pattern
9. Model POCO – no Dependency Properties / Routed Events No UI knowledge - pure data Design Patterns: MVVM
10. View Model No knowledge of the view Exposes the model(s) / custom properties used in the view Use ObservableCollection<T> for collections of data Implement INotifyPropertyChanged for custom properties Should be able to act as a command line interface to the view No Dependency Properties Design Patterns: MVVM
11. View Binds to the ViewModel (DataContext) Use converters to bind visual elements to ViewModel / Model properties ie: Model’s Temperature Colour Brush Events are hooked up through either code-behind or commanding Commanding better, but no OOB support in SL Silverlight Extensions project on CodePlex Custom Behaviors Composite Application Guidance/Library (Prism) Design Patterns: MVVM
12. View and ViewModel Binding ViewModel.cs App.xaml.cs ModelData md; public ViewModel() { md=new ModelData(); } public string Data { get{ return md.Data;} set{ md.Data = value;} } public App() { View view = new View(); view.DataContext= new ViewModel(); //… } View.xaml ... <TextBox Content=“{Binding Data}” ... /> ...
13. Can use “regular” property procedures in ViewModel If binding a collection to a DataGrid, etc., prefer the ObservableCollection<> (or non-generic equivalent) to sync updates back to the Model automatically MVVM Data Binding
14. MVVM Collection Item Binding The View should never bind to the data directly, but bind to a ViewModel If a collection of data is to be displayed in the View, the main ViewModel should expose an ObservableCollection of an additional ViewModel class that exposes properties, and thus, data Property ... Property ... Property ... Property ... Property ... Property ... public ObservableCollectionMyOC ...{ return new ObservableCollection<ViewModel2> ... } Property ... Property ... Property ... <DataGridItemsSource=“ {Binding MyOC}” .../> Property ... Property ... Property ... Property ... Property ... Property ... ViewModel2 The View ViewModel1
15. Issue: changing property values in ViewModel does not always update bound control in View Solution: have ViewModel implement INotifyPropertyChanged interface, and raise PropertyChanged event in each property Or better yet, create a base ViewModel class and have each ViewModel subclass it Property Binding
16. Attached Properties in XAML can also be used in lieu of missing Command Create Command, expose as property in ViewModel Create static class with Attached Property Add namespace to XAML screen Place Attached Property in desired control Commands and Attached Properties
19. The View knows about the ViewModel, but the ViewModel does not know about the View ViewModel is assigned to the DataContext of the View (Silverlight XAML) In xaml.cs codebehind for page In App.xaml.cs In XAML via Resource Other Associating ViewModel with View
20. Create ViewModel base class (implement INotifyPropertyChanged) Have all ViewModels subclass base Expose NO models to View directly Use Attached Properties as ‘workaround’ to lack of Commands in Silverlight controls Possibly Behaviors … Good practices …
22. Easy to get sloppy – name all controls/elements UI elements should have the same naming convention as your private class variables (that’s what they become) ie: <Button x:Name=“_submitButton” /> Prefix every resource with a “namespace” “Storyboards.FlashUserImage” “Brushes.ButtonBackground” “Templates.GiftsListBox” Development
38. Use Visibility rather than Opacity to hide objects Use transparent control backgrounds sparingly Avoid long-running JavaScript tasks and more Performance Tips …
39. Never make the user wait and wonder Progress, progress, progress Minimize XAP size (this initial download) Make services prime, but remember Progress, progress, progress User interaction
40. Resizing Blending Drawing resultant pixels onscreen ALL can be done with GPU acceleration enabled In <object> tag <parm name=“enableGPUAcceleration” value=“true” /> Hardware optimization
Introduced in .NET 3.5 SP1, further support in .NET 4.0 Allows you to annotate your classes with what it means for an instance to be valid Place it on properties When value is set, annotations get evaluated and an exception is thrown if they fail Silverlight 3 controls automatically react to exceptions and display an error state (a visual state)