Microsoft* Building Enterprise Applications with Windows* Presentation Foundation and the Model View ViewModel Pattern Raffaele Garofalo
Contents at a Glance 1 Introduction to Model View ViewModel and Line of Business Applications 1 2 Design Patterns 25 3 The Domain Model 61 4 The Data Access Layer 91 5 The Business Layer 123 6 The Ul Layer with MVVM > 149 7 MVVM Frameworlcs and Toolkits 179
Table of Contents Introduction Conventions and Features in This Book Acknowledgments xi xv xviii Errata and Book Support xix 1 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 orwpf? 4 Microsoft's Ul-Building Tools. 5 Composition of a LOB User Interface 9 The Menubar 11 The Toolbar 12 The Tooltip (and Its Abuse) 12 Notifications and Alerts 13 The Ribbon Bar 15 General Style and Control Considerations 16 Separation of Concerns 17 Layers, Tiers, and Services 19 Summary 23 2 Design Patterns 25 An Overview of Design Patterns.. 25 Classifying Design Patterns 26 Ul 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
viii Table of Contents 3 The Domain Model 61 Introduction to Domain-Driven Design 61 DDD Terminology 62 Analyzing the CRM Domain 63 Domain Entity and Data Transfer Object 164 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 4 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
Table of Contents ix Sample Code: The CRM Data Access Layer 112 A Flexible lilnitofwork 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 5 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 Workflow with WF 4.0 129 Different Ways of Running a Workflow 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 5.0.. 139 A Generic Workflow Engine 141 Service for Business Transactions 143 Summary : 147 6 The Ul 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 IDataErrorlnfo Interface 164
x Table of Contents DataTemplate in WPF and Silverlight 167 DataTemplate and MVVM 168 WeakEvents and Messages.. - 168 The WeakEvent Pattern 168 The EventAggregator Pattern 169 Dialogs and Modal Pop-Ups 171 Modal View in MVVM 172 Inversion of Control with MVVM 174 Sample Code 175 The Microsoft Office Ribbon and MVVM 176 Summary 177 7 MVVM Frameworks and Toolkits 179 MVVM Toolkits.: 179 MVVM Light Toolkit, by Laurent Bugnion 180 MEFedMVVM 181 Cinch, by Sacha Barber 182 MVVM and XAML Facilities 183 Karl Shifflett's Tools 184 Radical, by Mauro Servienti 185 Composite Ul Frameworks 186 Microsoft Prism 186 Caliburn 189 What do you think of this book? We want to hear from you! Microsoft is interested in hearing your feedback so we can continually improve our books and learning resources for you. To participate in a brief online survey, please visit: www.microsoft.com/learning/booksurvey/