www.it-ebooks.info Building Enterprise Applications with Windows ® Presentation Foundation and the Model View ViewModel Pattern Raffaele Garofalo www.it-ebooks.info Published with the authorization of Microsoft Corporation by: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, California 95472 Copyright © 2011 Raffaele Garofalo. Complying with all applicable copyright laws is the responsibility of the user. All rights reserved. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without express written permission of O’Reilly Media, Inc. Printed and bound in the United States of America. 1 2 3 4 5 6 7 8 9 LSI 6 5 4 3 2 1 Microsoft Press titles may be purchased for educational, business or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com. Visit our website at microsoftpress.oreilly.com. Send comments to mspinput@microsoft.com. Microsoft, Microsoft Press, ActiveX, Excel, FrontPage, Internet Explorer, PowerPoint, SharePoint, Webdings, Windows, and Windows 7 are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. Other product and company names mentioned herein may be the trademarks of their respective owners. Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are ctitious, and no association with any real company, organization, prod- uct, domain name, e-mail address, logo, person, place, or event is intended or should be inferred. This book expresses the author’s views and opinions. The information contained in this book is provided without any express, statutory, or implied warranties. Neither the author, O’Reilly Media, Inc., Microsoft Corporation, nor their respective resellers or distributors, will be held liable for any damages caused or alleged to be caused either directly or indirectly by such information. Acquisitions and Development Editor: Russell Jones Production Editor: Kristen Borg Production Services: Octal Publishing, Inc. Technical Reviewer: David Hill Indexing: Fred Brown Cover: Karen Montgomery Illustrator: Robert Romano 978-0-735-65092-3 www.it-ebooks.info To my wife Deborah. Thank you for everything! www.it-ebooks.info www.it-ebooks.info Contents at a Glance www.it-ebooks.info www.it-ebooks.info Table of Contents Introduction xi Conventions and Features in This Book xv Acknowledgments xviii Errata and Book Support xix Introduction to Model View ViewModel and Line of Business Applications 1 The Model View ViewModel Pattern 1 Line of Business Applications 2 Choosing the Right Technology 3 Silverlight or WPF? 4 Microsoft’s UI-Building Tools 5 Composition of a LOB User Interface 9 The Menubar 11 The Toolbar 12 The Tooltip (and Its Abuse) 12 Notications and Alerts 13 The Ribbon Bar 15 General Style and Control Considerations 16 Separation of Concerns 17 Layers, Tiers, and Services 19 Summary 23 Design Patterns 25 An Overview of Design Patterns 25 Classifying Design Patterns 26 UI Design Patterns 29 The MVC Pattern 30 The MVP Pattern 34 The PM Pattern and MVVM 39 Advanced Design Patterns and Techniques 43 The Inversion of Control Pattern 44 DSLs: Writing Fluent Code 53 Introduction to TDD 57 Summary 60 www.it-ebooks.info Table of Contents The Domain Model 61 Introduction to Domain-Driven Design 61 DDD Terminology 62 Analyzing the CRM Domain 63 Domain Entity and Data Transfer Object 64 The POCO Object and the O/RM 66 Development Approaches of a Domain 67 Transaction Script 68 Database-Driven Approach 69 Domain-Driven Approach 70 How To Create an Object In DDD 71 Factory Patterns 72 Domain Entities Validation 75 Classic Validation 75 Validation Using Attributes and Data Annotations 77 Available Validation Frameworks 79 Unit Test the Domain Model 80 Sample Code: The CRM Domain Model 81 The Person Context 81 The Order Domain 87 Summary 89 The Data Access Layer 91 Introduction 91 The Database and Stored Procedures 92 Choosing an O/RM 93 Microsoft Entity Framework 95 NHibernate 98 Other O/RMs for NET 100 The Unit of Work 101 Lifecycle of a UoW 102 Identify a Business Transaction 102 The Repository Pattern 104 Test-Driven Development: The Data Layer 106 Building a Distributed Data Layer with RIA and WCF 108 www.it-ebooks.info Table of Contents Sample Code: The CRM Data Access Layer 112 A Flexible IUnitOfWork Interface 112 Mapping the Domain Model Using Entity Framework 113 Mapping the Domain Using NHibernate 117 Getting the Tools 118 The UnitOfWork and the ISession . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .119 Summary 121 The Business Layer 123 Introduction 123 A Business Rule Is Not a Validation Rule 124 Business Rules by Service 127 The Facade Pattern 128 Business Rules by Workow with WF 40 129 Different Ways of Running a Workow 130 Third-Party Toolkits 133 Technologies for the Data Validation 134 Rule Engine and Business Rule Engine 136 Business Layer Considerations 137 When Do I Need to Create a Business Layer? 137 Bad BLL Habits 138 Sample Code: The Business Service Layer 139 Data Validation with the Enterprise Library 50 139 A Generic Workow Engine 141 Service for Business Transactions 143 Summary 147 The UI Layer with MVVM 149 Introduction to the MVVM Pattern 150 The View 151 Blendability: A Dummy ViewModel 152 The Model 155 The Command in WPF and Silverlight 157 A Workaround: An MVVM Command 158 Re-evaluate ICommand Execution 160 The ViewModel 161 The INotifyPropertyChanged Interface 162 The IDataErrorInfo Interface www.it-ebooks.info [...]... behind everything The ViewModel contains the UI logic, the commands, the events, and a reference to the Model In MVVM, the ViewModel is not in charge of updating the data displayed in the UI—thanks to the powerful www.it-ebooks.info 1 2 Chapter 1 Introduction to Model View ViewModel and Line of Business Applications data-binding engine provided by WPF and Silverlight, the ViewModel doesn’t need to... code that you will be able to use and reuse in the future Building Enterprise Applications with Windows Presentation Foundation and the Model View ViewModel Pattern provides not only a solid analysis of how the MVVM pattern works and how to apply it with WPF and Silverlight, but it also offers an exhaustive guide to building layered applications by using the most common and accepted techniques This book... Model View ViewModel and Line of Business Applications After completing this chapter, you will be able to: n Identify a Line of Business application n Decide which is the right technology for you to develop a Line of Business application The Model View ViewModel Pattern The Model View ViewModel (MVVM) pattern was introduced by John Gossman (Software Architect at Microsoft for Windows Presentation Foundation. .. “revolutionary.” In the past, I have used well-known patterns to build applications, including the Model View Presenter pattern with Windows Forms applications, and the Model View Controller pattern with ASP NET applications But with WPF, these two approaches are now obsolete, because they can’t take advantage of the powerful engine provided by XAML Of course, you can still take advantage of the binding engine... The first chapter, “Introduction to Model View ViewModel Applications, ” is a general introduction to LOB applications, their components, and their structure Chapter 2, “Design Patterns,” shows a complete overview of all the well-known design patterns and architectural patterns used to develop enterprise applications, and more generally, to develop loosely-coupled components Chapter 3, The Domain Model, ”... doesn’t need to do that This is because the View is an observer of the ViewModel, so as soon as the ViewModel changes, the UI updates itself For that to happen, the ViewModel must implement the INotifyPropertyChanged interface and fire the PropertyChanged event Initially, only WPF was powerful enough to satisfy the MVVM pattern s requirements In Silverlight 2, you had the option of implementing MVVM, but... and the ViewModel n The Model is the entity that represents the business concept; it can be anything from a simple customer entity to a complex stock trade entity n The View is the graphical control or set of controls responsible for rendering the Model data on screen A View can be a WPF window, a Silverlight page, or just an XAML data template control n The ViewModel is the magic behind everything The. .. only the image to indicate the function of a Toolbar button, so the clearer and more self-explanatory your images are, the more useful the Toolbar will be Because the Toolbar supports the same (or fewer) commands that are already available in the Menubar, using the MVVM approach, you might wish to use the same ViewModel for both controls or to provide a collection of commands for each ViewModel and. .. of triggers in WPF and Silverlight when you implement the MVVM pattern: they should not be heavily used because they can easily incorporate presentation logic that can’t be tested The logic is not available in the ViewModel but it is exposed in the View with the trigger Silverlight 4 ships with a rich set of controls, styles, and templates, one of which is a nice LOB ASP.NET Model View Contoller (MVC)... UIs and different UI technologies, reducing the coupling between the UI and other code, and allowing UI Designers to work in a more independent manner MVVM is a specialized interpretation of the PM pattern designed to satisfy the requirements of Windows Presentation Foundation (WPF) and Silverlight Structurally, an MVVM application consists primarily of three major components: the Model, the View, and . www.it-ebooks.info Building Enterprise Applications with Windows ® Presentation Foundation and the Model View ViewModel Pattern Raffaele Garofalo www.it-ebooks.info Published. well-known patterns to build applications, including the Model View Pre- senter pattern with Windows Forms applications, and the Model View Controller pattern with