Building Enterprise Applications With Windows Presentation .

3y ago
31 Views
6 Downloads
7.42 MB
225 Pages
Last View : 5m ago
Last Download : 3m ago
Upload by : Roy Essex
Transcription

www.it-ebooks.info

Building EnterpriseApplications with WindowsPresentation Foundation andthe Model View ViewModelPattern Raffaele Garofalowww.it-ebooks.info

Published with the authorization of Microsoft Corporation by:O’Reilly Media, Inc.1005 Gravenstein Highway NorthSebastopol, California 95472Copyright 2011 Raffaele Garofalo.Complying with all applicable copyright laws is the responsibility of the user. All rights reserved. Without limiting therights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, ortransmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for anypurpose, 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 1Microsoft Press titles may be purchased for educational, business or sales promotional use. Online editions are alsoavailable for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutionalsales department: (800) 998-9938 or corporate@oreilly.com. Visit our website at microsoftpress.oreilly.com. Sendcomments 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/orother 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 fictitious, and no association with any real company, organization, product, 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 anyexpress, statutory, or implied warranties. Neither the author, O’Reilly Media, Inc., Microsoft Corporation, nor theirrespective resellers or distributors, will be held liable for any damages caused or alleged to be caused either directlyor indirectly by such information.Acquisitions and Development Editor: Russell JonesProduction Editor: Kristen BorgProduction Services: Octal Publishing, Inc.Technical Reviewer: David HillIndexing: Fred BrownCover: Karen MontgomeryIllustrator: Robert Romano978-0-735-65092-3www.it-ebooks.info

To my wife Deborah. Thank you for everything!www.it-ebooks.info

www.it-ebooks.info

Contents at a Glance1234567Introduction to Model View ViewModel and Line of BusinessApplications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Design Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25The Domain Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61The Data Access Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91The Business Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123The UI Layer with MVVM . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149MVVM Frameworks and Toolkits . . . . . . . . . . . . . . . . . . . . . . 179vwww.it-ebooks.info

www.it-ebooks.info

Table of ContentsIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiConventions and Features in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvAcknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviiiErrata and Book Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix1Introduction to Model View ViewModel and Line of BusinessApplications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1The Model View ViewModel Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Line of Business Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2Choosing the Right Technology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3Silverlight or WPF? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4Microsoft’s UI-Building Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5Composition of a LOB User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9The Menubar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11The Toolbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12The Tooltip (and Its Abuse) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Notifications and Alerts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13The Ribbon Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15General Style and Control Considerations . . . . . . . . . . . . . . . . . . . . . . . . . 16Separation of Concerns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Layers, Tiers, and Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232Design Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25An Overview of Design Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25Classifying Design Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26UI Design Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29The MVC Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30The MVP Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34The PM Pattern and MVVM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39Advanced Design Patterns and Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43The Inversion of Control Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44DSLs: Writing Fluent Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53Introduction to TDD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60viiwww.it-ebooks.info

viiiTable of Contents3The Domain Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61Introduction to Domain-Driven Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61DDD Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62Analyzing the CRM Domain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63Domain Entity and Data Transfer Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64The POCO Object and the O/RM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66Development Approaches of a Domain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67Transaction Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68Database-Driven Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69Domain-Driven Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70How To Create an Object In DDD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71Factory Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72Domain Entities Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75Classic Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75Validation Using Attributes and Data Annotations . . . . . . . . . . . . . . . . . . 77Available Validation Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79Unit Test the Domain Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80Sample Code: The CRM Domain Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81The Person Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81The Order Domain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 894The Data Access Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91The Database and Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92Choosing an O/RM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93Microsoft Entity Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95NHibernate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98Other O/RMs for .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100The Unit of Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101Lifecycle of a UoW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102Identify a Business Transaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102The Repository Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104Test-Driven Development: The Data Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106Building a Distributed Data Layer with RIA and WCF . . . . . . . . . . . . . . . . . . . . 108www.it-ebooks.info

Table of ContentsSample Code: The CRM Data Access Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112A Flexible IUnitOfWork Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112Mapping the Domain Model Using Entity Framework . . . . . . . . . . . . . 113Mapping the Domain Using NHibernate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117Getting the Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118The UnitOfWork and the ISession . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1215The Business Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123A Business Rule Is Not a Validation Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124Business Rules by Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127The Facade Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128Business Rules by Workflow with WF 4.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129Different Ways of Running a Workflow . . . . . . . . . . . . . . . . . . . . . . . . . . 130Third-Party Toolkits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133Technologies for the Data Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134Rule Engine and Business Rule Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . 136Business Layer Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137When Do I Need to Create a Business Layer? . . . . . . . . . . . . . . . . . . . . . 137Bad BLL Habits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138Sample Code: The Business Service Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139Data Validation with the Enterprise Library 5.0 . . . . . . . . . . . . . . . . . . . . 139A Generic Workflow Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141Service for Business Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1476The UI Layer with MVVM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149Introduction to the MVVM Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150The View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151Blendability: A Dummy ViewModel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152The Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155The Command in WPF and Silverlight . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157A Workaround: An MVVM Command . . . . . . . . . . . . . . . . . . . . . . . . . . . 158Re-evaluate ICommand Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160The ViewModel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161The INotifyPropertyChanged Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . 162The IDataErrorInfo Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .www.it-ebooks.infoix

xTable of ContentsDataTemplate in WPF and Silverlight . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167DataTemplate and MVVM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168WeakEvents and Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168The WeakEvent Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168The EventAggregator Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169Dialogs and Modal Pop-Ups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171Modal View in MVVM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172Inversion of Control with MVVM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174Sample Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175The Microsoft Office Ribbon and MVVM . . . . . . . . . . . . . . . . . . . . . . . . . 176Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1777MVVM Frameworks and Toolkits . . . . .

The Windows Presentation Framework (WPF), Silverlight, and Windows Phone 7 are the latest tech- nologies for building flexible user interfaces (UI) for applications built with Microsoft technology . All three rely on the XAML markup language to describe UI elements and layout, and you can pro-

Related Documents:

The Windows The Windows Universe Universe Windows 3.1 Windows for Workgroups Windows 95 Windows 98 Windows 2000 1990 Today Business Consumer Windows Me Windows NT 3.51 Windows NT 4 Windows XP Pro/Home. 8 Windows XP Flavors Windows XP Professional Windows XP Home Windows 2003 Server

AutoCAD 2000 HDI 1.x.x Windows 95, 98, Me Windows NT4 Windows 2000 AutoCAD 2000i HDI 2.x.x Windows 95, 98, Me Windows NT4 Windows 2000 AutoCAD 2002 HDI 3.x.x Windows 98, Me Windows NT4 Windows 2000 Windows XP (with Autodesk update) AutoCAD 2004 HDI 4.x.x Windows NT4 Windows 2000 Windows XP AutoCAD 2005 HDI 5.x.x Windows 2000 Windows XP

A computer with at least a 450MHz Pentium CPU with 128 MB of RAM, running Windows 2000, Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7, Windows 8/8.1, Windows 10, Windows Server 2012, Windows Server 2016 or Windows Server 2019 platforms. Instal

Windows 8.1 *6 Windows Server 2003 *7 Windows Server 2008 *8 Windows Server 2012 *9 Mac OS X *10: Supported *1 Printer drivers support both 32-bit and 64-bit Windows. *2 Microsoft Windows XP Professional Edition/Microsoft Windows XP Home Edition *3 Microsoft Windows Vista Ultimate/Microsoft Windows Vista Enterprise/Microsoft Windows Vista Business/

Windows 7 Professional / Enterprise / Ultimate x64 Edition SP1 Microsoft Windows 8 Professional / Enterprise x86 Edition, Microsoft Windows 8 Professional / Enterprise x64 Edition, Microsoft Windows 8.1 Enterprise x86 Edition, Microsoft Windows 8.1 Enterprise x64 Edition

Machine Edition Product Windows 7SP1 Windows 8 and 8.1 Windows 10 QP View Developer - QP Logic Developer – PC - o Windows 7 Ultimate, Windows 7 Enterprise, Windows 7 Professional and Windows 10. Notes The above versions of Windows are supported in both 32-bit and 64-bit. Windows regional settings must be set to English.

Windows 10 IoT Enterprise. 1. A Universal Windows app is a Windows experience that is built upon the Universal Windows Platform (UWP), which was first introduced in Windows 8 as the Windows Runtime. Universal Windows apps are most often distributed via the Windows Store (but can also be side-loaded), and are most often packaged and distributed .

Microsoft Windows 7, 32-bit and 64-bit Microsoft Windows 8 & 8.1, 32-bit and 64-bit Microsoft Windows 10, 32-bit and 64-bit Microsoft Windows Server 2008 R2 Microsoft Windows Server 2012, 64-bit only RAM: Minimum 2 GB for the 32-bit versions of Microsoft Windows 7, Windows 8, Windows 8.1, and Windows 10.