cyan yelloW MaGenTa Black panTone 123 c Books for professionals By professionals® The eXperT’s Voice® in .neT Companion eBook Available Expert C# 2008 Business Objects Expert Dear Reader, This book is about application architecture, design, and development using Expert object-oriented concepts in .NET. Specifically, I’ll show how to build business- C focused objects called business objects and how to use them to build applica- tions with Windows Presentation Foundation (WPF), Web Forms, Windows Communication Foundation (WCF), Windows Forms, Windows Workflow Author of Foundation (WF), and other types of application. While this book is about the # C# 2008 creation of the CSLA .NET development framework, that framework is the codi- Expert C# 2005 fication of an underlying architecture, which helps provide order to the chaos Business Objects and enables productive application of the architectural concepts. You may agree or disagree with my views, and that is fine. In the end, having an architecture that withstands the tests of time and technological change is 2 the key. At a minimum, the concepts, ideas, and techniques in this book will help inform your understanding of architecture and will enable you to put all the new and changing technologies into perspective. The CSLA .NET framework itself is one of the most widely used development 0 frameworks on the .NET platform. In reading this book, you will see the design Business Objects decisions that enable the framework to support the many technologies listed previously—all based on the same set of business objects. You’ll also learn how the framework abstracts the concept of data access, enabling an application to 0 use nearly any data access technology available on the .NET platform. Over the past eight years, CSLA .NET has evolved, and it continues to evolve to support new capabilities of the .NET platform. The underlying architecture of using object-oriented design in a distributed computing environment has 8 remained consistent all this time, and it continues to be critical looking forward to Silverlight, Windows Azure, and beyond. I hope you enjoy this book and find it challenging and educational. Code Architect, design, and develop highly scalable and well, have fun! maintainable object-oriented business applications OB Rockford Lhotka Companion eBook bu THE APRESS ROADMAP js e Beginning Pro C# 2008 and the i C# 2008 Objects .NET 3.5 Platform, 4e See last page for details cn on $10 eBook version Pro ASP.NET 3.5 Expert C# 2008 Beginning C# 2008, 2e in C# 2008, 3e Business Objects te s Illustrated C# 2008 Pro WPF in C# 2008, 2e s s Rockford Lhotka SOURCE CODE ONLINE www.apress.com ISBN 978-1-4302-1019-1 55999 Lhotka US $59.99 Shelve in Programming Languages/C# User level: 9 781430 210191 Advanced this print for content only—size & color not accurate spine = 1.5015" 800 page count Lhotka_1019-1FRONT.fm Page i Thursday, November 20, 2008 5:04 PM Expert C# 2008 Business Objects ■ ■ ■ Rockford Lhotka Lhotka_1019-1FRONT.fm Page ii Thursday, November 20, 2008 5:04 PM Expert C# 2008 Business Objects Copyright © 2009 by Rockford Lhotka All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-1019-1 ISBN-13 (electronic): 978-1-4302-1020-7 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Editor: Matthew Moodie Technical Reviewers: James Miller, Andrés Villanueva, Joe Fallon Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Richard Dal Porto Copy Editors: Nicole Abramowitz, Jennifer Whipple Associate Production Director: Kari Brooks-Copony Production Editor: Laura Esterman Compositor: Susan Glinert Proofreader: Nancy Bell Indexer: John Collin Artist: April Milne Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http:// www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com. Lhotka_1019-1FRONT.fm Page iii Thursday, November 20, 2008 5:04 PM To my Mom and Dad. Thank you for all you’ve taught me through the years! Lhotka_1019-1FRONT.fm Page iv Thursday, November 20, 2008 5:04 PM Lhotka_1019-1FRONT.fm Page v Thursday, November 20, 2008 5:04 PM Contents at a Glance About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxv ■ CHAPTER 1 Distributed Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 ■ CHAPTER 2 Framework Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 ■ CHAPTER 3 Object-Oriented Application Design . . . . . . . . . . . . . . . . . . . . . . . . . . 101 ■ CHAPTER 4 CSLA .NET Object Stereotypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 ■ CHAPTER 5 CSLA .NET Object Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 ■ CHAPTER 6 Business Framework Implementation . . . . . . . . . . . . . . . . . . . . . . . . 207 ■ CHAPTER 7 Property Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 ■ CHAPTER 8 Object Status Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 ■ CHAPTER 9 Parent-Child Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 ■ CHAPTER 10 Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 ■ CHAPTER 11 Business and Validation Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 ■ CHAPTER 12 Authentication and Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353 ■ CHAPTER 13 N-Level Undo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369 ■ CHAPTER 14 LINQ to CSLA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399 ■ CHAPTER 15 Persistence and the Data Portal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419 ■ CHAPTER 16 Other Framework Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487 ■ CHAPTER 17 Business Object Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517 ■ CHAPTER 18 Example Data Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547 ■ CHAPTER 19 Windows Presentation Foundation UI . . . . . . . . . . . . . . . . . . . . . . . . 583 ■ CHAPTER 20 Web Forms UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619 ■ CHAPTER 21 WCF Service Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675 ■ INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .719 v Lhotka_1019-1FRONT.fm Page vi Thursday, November 20, 2008 5:04 PM Lhotka_1019-1FRONT.fm Page vii Thursday, November 20, 2008 5:04 PM Contents About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxv ■ CHAPTER 1 Distributed Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Logical and Physical Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 N-Tier and SOA. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Complexity. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Relationship Between Logical and Physical Models. . . . . . . . . . . . . . 4 A 5-Layer Logical Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Applying the Logical Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 The Way Ahead. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Managing Business Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Potential Business Logic Locations . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Business Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Mobile Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Architectures and Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 ■ CHAPTER 2 Framework Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Basic Design Goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Validation and Business Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Tracking Whether the Object Has Changed. . . . . . . . . . . . . . . . . . . . 41 Integrated Authorization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Strongly Typed Collections of Child Objects . . . . . . . . . . . . . . . . . . . 42 N-Level Undo Capability. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Simple and Abstract Model for the UI Developer . . . . . . . . . . . . . . . 45 Supporting Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Object Persistence and Object-Relational Mapping . . . . . . . . . . . . . 54 Custom Authentication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 vii Lhotka_1019-1FRONT.fm Page viii Thursday, November 20, 2008 5:04 PM viii ■ CONTENTS Designing the Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Business Object Creation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 N-Level Undo Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Data Binding Support. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Business and Validation Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Data Portal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Custom Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Integrated Authorization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Helper Types and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Namespace Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 ■ CHAPTER 3 Object-Oriented Application Design . . . . . . . . . . . . . . . . . . . . . 101 Responsibility-Driven Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Use-Case or Story-Based Analysis. . . . . . . . . . . . . . . . . . . . . . . . . . 102 Objects with Responsibilities. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Objects Exist for Use Cases. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Normalization of Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Application Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Use Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Object Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Initial Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Revising the Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Custom Authentication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Using CSLA .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Database Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Creating the Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 PTracker Database. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Security Database. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Description: