380 likes | 476 Vues
Dive into the world of Model-View-ViewModel (MVVM) architecture to understand its significance in software development. Learn how to respond to user actions, design with data, utilize data services, and test efficiently. Explore the benefits of MVVM through ViewModel implementation, separation of services, and RIA platforms integration. Discover how services, messaging, and blendability can enhance your application's functionality and design. Stay ahead with automated testing and resources for further learning and reference.
E N D
MVVMmmmmmmmmmmm Ward Bell VP Technology, IdeaBlade Twitter: @wardbell Blog: http://neverindoubtnet.blogspot.com/
Agenda • MVVM: What and Why • Respond to User Actions • Abstraction • Design with Data • Data Services • Testing
Bookshelf in Action demo
MVVM View Model
MVVM • Model • Domain object • Properties • Model Validation View
MVVM • View • The screen or page or control • User friendly presentation of information • Themes and Styles • User interactions Model
MVVM Model View ViewModel
MVVM • ViewModel • Glues the View to the Model • Databound to the View • Properties • Commands • Methods • Contains the Model • Talks to services and load/save the Model Model View
MVVM • View • The screen or page or control • User friendly presentation of information • Themes and Styles • User interactions • Bindings • Events • Behaviors ViewModel Model
MVVM Presentation logic Binding ViewModel Tell View when data changes Design w/ XAML Model (e.g. PeopleModel/Person) View (e.g. Person Master/Details) XAML Code Not reusable
Binding a View to a ViewModel demo ViewModelLocator INotifyPropertyChanged
+ Services MVVM Reusable Services VM V M • ViewModel • Presentation logic • Handle user interaction • Reference Models • Services • Perform operations • Reusable tasks Model View
Separating the Services • Services provide related tasks to the caller • BookDataService, SecurityService, BookEditor • ViewModel uses a Service • Passed through the constructor (Dependency Injection) • ViewModel can focus on bindings and logic • Easily refactored and testable • Services implement an interface
Adding Value with Services demo BookViewModel talks to services
RIA Platforms • Data access and Server communications • Data modeling as Entities (not DTOs) • Binding directly to Entities • INPC and INDEI • You get EntityManager/DomainContext • Your gateway to the server • Change tracking • Abstraction through Services
DevForce and MVVM demo Behind the BookDataService
+ Messaging MVVM VM VM Direct Message? V V M M
+ Messaging MVVM EventAggregator Pattern VM VM V V M M Subscribe Messaging Service Publish Subscribe Confirmation Dialog
Confirmation Dialog Example • Define a message type (ConfirmationDialogMessage) • Send method knows MessengerService • Message defines its own payload • Publish message (BookSaver:SaveSuccessful) • Subscribe to message (MainPage:DialogMessageReceived) • Subscriber acquires MessengerService • Subscriber registers action for message
Messaging demo
Design Time Data What am I looking at? Ah! Are my bindings right? How’s my layout? Isn’t this easier/more productive? Notice I’ve tested my Submit Button Logic
Swapping Services • Services won’t run in the Blend and Visual Studio designers • Need design time calls • Service Providers • If DesignTime • Create Design Time Services • Else • Create Regular Services
Blendability: Data in the Designer demo Service Provider and Design Time Services Using cached sample data - “Self-initializing Fake”
Automated Testing demo
Summary • MVVM: What and Why • Respond to User Actions • Abstraction • Design with Data • Data Services • Testing
Resources Web: IdeaBlade: www.ideablade.com Bookshelf in DevForce: links.ideablade.com/drc-bookshelf SL Firestarter 2010: links.ideablade.com/firestarter10-mvvm MIX 2010 MVVM: links.ideablade.com/mix10-laurent-mvvm MVVM Light: links.ideablade.com/mvvmlight SL Test Framework: links.ideablade.com/sl-utf Print: Silverlight 4 Unleashed, Laurent Bugnion