6315_fm_final.qxd 4/7/06 5:24 PM Page i Expert VB 2005 Business Objects Second Edition Rockford Lhotka 6315_fm_final.qxd 4/7/06 5:24 PM Page ii Expert VB 2005 Business Objects,Second Edition Copyright © 2006 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-59059-631-9 ISBN-10 (pbk): 1-59059-631-5 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. Lead Editor: Jonathan Hassell Technical Reviewer: Petar Kozul Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick, Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser, Keir Thomas, Matt Wade Project Manager: Kylie Johnston Copy Edit Manager: Nicole LeClerc CopyEditor:Damon Larson Assistant Production Director: Kari Brooks-Copony Production Editor: Laura Cheu Compositor: Linda Weidemann, Wolf Creek Press Proofreader: April Eddy Indexer: John Collin Artist: Kinetic Publishing Services,LLC 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 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected],or visit http://www.apress.com. 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.comin the Source Code section. 6315_fm_final.qxd 4/7/06 5:24 PM Page iii In memory of my Grandmother,Evylyn, atrue angel on earth,who now rests in heaven. 6315_fm_final.qxd 4/7/06 5:24 PM Page iv 6315_fm_final.qxd 4/7/06 5:24 PM Page v Contents at a Glance About the Author ......................................................................xv About the Technical Reviewer ...........................................................xvi Acknowledgments ....................................................................xvii Introduction ..........................................................................xix nCHAPTER 1 Distributed Architecture.............................................1 nCHAPTER 2 Framework Design.................................................35 nCHAPTER 3 Business Framework Implementation...............................93 nCHAPTER 4 Data Access and Security .........................................163 nCHAPTER 5 Completing the Framework........................................239 nCHAPTER 6 Object-Oriented Application Design ................................325 nCHAPTER 7 Using the CSLA .NET Base Classes.................................365 nCHAPTER 8 Business Object Implementation...................................407 nCHAPTER 9 Windows Forms UI................................................465 nCHAPTER 10 Web Forms UI ....................................................515 nCHAPTER 11 Web Services Interface............................................567 nCHAPTER 12 Implementing Remote Data Portal Hosts............................607 nINDEX ...........................................................................627 v 6315_fm_final.qxd 4/7/06 5:24 PM Page vi 6315_fm_final.qxd 4/7/06 5:24 PM Page vii Contents About the Author ......................................................................xv About the Technical Reviewer ...........................................................xvi Acknowledgments ....................................................................xvii Introduction ..........................................................................xix nCHAPTER 1 Distributed Architecture........................................1 Logical and Physical Architecture .......................................1 Complexity .....................................................3 Relationship Between Logical and Physical Models ...................4 A5-Layer Logical Architecture.....................................8 Applying the Logical Architecture .................................13 The Way Ahead ................................................18 Managing Business Logic.............................................18 Potential Business Logic Locations................................18 Business Objects...............................................22 Mobile Objects.................................................25 Architectures and Frameworks.........................................33 Conclusion .........................................................33 nCHAPTER 2 Framework Design.............................................35 Basic Design Goals ..................................................36 N-Level Undo Capability.........................................37 Tracking Broken Business Rules..................................40 Tracking Whether the Object Has Changed .........................41 Strongly Typed Collections of Child Objects.........................41 Simple and Abstract Model for the UI Developer.....................43 Supporting Data Binding.........................................47 Object Persistence and Object-Relational Mapping...................50 Custom Authentication ..........................................57 Integrated Authorization .........................................58 vii 6315_fm_final.qxd 4/7/06 5:24 PM Page viii viii nCONTENTS Framework Design...................................................58 Business Object Creation ........................................59 N-Level Undo Functionality.......................................64 Data Binding Support ...........................................67 Validation Rules................................................68 Data Portal ....................................................71 Custom Authentication ..........................................84 Integrated Authorization .........................................85 Helper Types and Classes........................................86 Namespace Organization .............................................89 Conclusion .........................................................91 nCHAPTER 3 Business Framework Implementation........................93 Setting Up the CSLA .NET Project.......................................94 Creating the Directory Structure...................................95 Supporting Localization..........................................95 Csla.Core Namespace................................................96 IBusinessObject Interface........................................97 IUndoableObject Interface........................................97 IEditableCollection Interface......................................98 IReadOnlyObject Interface........................................99 IReadOnlyCollection Interface.....................................99 ICommandObject Interface.......................................99 ObjectCloner Class..............................................99 BindableBase Class............................................100 NotUndoableAttribute Class .....................................104 UndoableBase Class ...........................................104 BusinessBase Class............................................112 ReadOnlyBindingList Class......................................130 Csla.Validation Namespace...........................................131 RuleHandler Delegate..........................................132 RuleArgs Class................................................132 RuleMethod Class .............................................133 ValidationRules Class ..........................................134 BrokenRule Class..............................................137 BrokenRulesCollection Class ....................................137 ValidationException............................................138 Csla.Security Namespace............................................139 RolesForProperty Class.........................................139 AccessType Enum.............................................140 AuthorizationRules Class........................................140 6315_fm_final.qxd 4/7/06 5:24 PM Page ix nCONTENTS ix Csla Namespace ...................................................143 BusinessBase Class............................................143 BusinessListBase Class.........................................146 ReadOnlyBase Class...........................................159 ReadOnlyListBase Class........................................160 Conclusion ........................................................161 nCHAPTER 4 Data Access and Security ....................................163 Data Portal Design..................................................164 Channel Adapter and Message Router Patterns.....................165 Distributed Transaction Support..................................168 Context and Location Transparency ..............................170 Enhancing the Base Classes..........................................173 FactoryMethods and Criteria....................................175 Save Methods.................................................176 Data Portal Methods ...........................................178 Csla.MethodCaller Class........................................181 Csla.Server.CallMethodException ................................187 Csla.RunLocalAttribute Class....................................188 Csla.DataPortalEventArgs Class..................................188 Csla.DataPortal Class ..........................................189 Csla.Server.IDataPortalServer ...................................197 Csla.DataPortalClient.IDataPortalProxy............................198 Csla.DataPortalClient.LocalProxy.................................198 Csla.DataPortalClient.RemotingProxy .............................200 Csla.Server.Hosts.RemotingPortal................................202 Csla.DataPortalClient.EnterpriseServicesProxy......................204 Csla.Server.Hosts.EnterpriseServicesPortal ........................206 Csla.DataPortalClient.WebServicesProxy ..........................210 Csla.Server.Hosts.WebServicePortal..............................213 Distributed Transaction Support.......................................215 Csla.TransactionalTypes........................................215 Csla.TransactionalAttribute......................................215 Csla.Server.DataPortal .........................................216 Csla.Server.ServicedDataPortal..................................220 Csla.Server.TransactionalDataPortal..............................221 Message Router....................................................222 Csla.CriteriaBase..............................................223 Csla.Server.SimpleDataPortal....................................223