Implementing MVVM in Real World ArcGIS Server Silverlight Applications Brandon Copeland LJA Engineering, Inc. 1
Agenda / Focused Topics Application Demo Model-View-ViewModel (MVVM) What is MVVM? Why is it important? MVVM in an ESRI ArcGIS Server application context Further abstraction through a service layer MVVM Next Level - Help from PRISM Modularity, Regions, and UI Composition Decoupled Communication 2
Application Demo Core functionality Basic architecture Key technologies 3
Why MVVM (or any other design pattern) Separation of Concerns - UI independent of presentation logic Single Responsibility Maintainability Scalability Maintain consistent development patterns Promote testable code XAML data binding baked in to Silverlight, WPF, WP7 4
Testing... Who Cares? Testing serves as Documentation Testing promotes better designed, reusable, modular code Refactoring safety net In TDD, test defines scope and feature completion In TDD, test provides instant gratification 5
MVVM View View Model Model Data Domain Logic Validation Class w/ Properties REST Service RIA Services 6
MVVM View Screen / Page/ Control Presentation of Data Bindings Behaviors Animations Themes and Styles View Model Model Data Domain Logic Validation Class w/ Properties REST Service RIA Services 7
MVVM View Screen / Page/ Control Presentation of Data Bindings Behaviors Animations Themes and Styles View Model Presentation Logic Exposes data from model to the view (Data Binding) Properties and Commands View s DataContext Model Data Domain Logic Validation Class w/ Properties REST Service RIA Services 8
ICommand Allow View to bind directly to methods in View Model CanExecute support Delegate to determine Is use of this command supported in the current application state? Button.IsEnabled handled by command Supported out of the box in Silverlight 4 for ButtonBase Framework implementations - DelegateCommand (PRISM) and RelayCommand Simplified further in Silverlight 5 with XAML Custom Markup Extensions 9
MVVM View Model View Model 10
MVVM View Model Receives messages from and interacts with Model INotifyPropertyChanged View Model 11
MVVM View Model XAML data binding to Properties and Commands on View Model Receives messages from and interacts with Model INotifyPropertyChanged View Model 12
Code Demo MVVM in action Communication between layers Commanding 13
Abstraction through Services Service Layer - Reusable interfaces for interaction with Model Load and save data Invoke operations with server (REST, RIA data context, etc...) Dependency Injection in View Model constructor View model does not know about implementation specifics Promotes single responsibility, reuse, and testability Allows implementation changes without dependency concerns 14
Code Demo Service layer in action Dependency Injection 15
Additional PRISM Goodness (or MVVM Light, MVVM Toolkit, Caliburn, choose your framework...) DI container (Unity and MEF in PRISM) Decoupled communication through messenger class (IEventAggregator in PRISM) Regions and UI Composition Modularity 16
Code Demo PRISM Extras 17
Questions??? 18