ASP.NET Dynamic Data Unleashed
Oleg Sych
800 East 96th Street, Indianapolis, Indiana 46240 USA
ASP.NET Dynamic Data Unleashed
Copyright 2012 by Pearson Education, Inc.
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein.
ISBN-13: 978-0-6723-3565-5
ISBN-10: 0-672-33565-4
Library of Congress Cataloging-in-Publication Data is on file.
Printed in the United States of America
First Printing: May 2012
Editor-in-Chief
Greg Wiegand
Executive Editor
Neil Rowe
Development Editor
Mark Renfrow
Managing Editor
Kristy Hart
Project Editor
Anne Goebel
Copy Editor
Christal White, Language Logistics, LLC
Indexer
Christine Karpeles
Proofreader
Sarah Kearns
Technical Editors
Scott Hunter
David Ebbo
Publishing Coordinator
Cindy Teeters
Cover Designer
Gary Adair
Senior Compositor
Gloria Schurick
Trademarks
All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Pearson Education, Inc. cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark.
Warning and Disclaimer
Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an as is basis. The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book.
Bulk Sales
Pearson offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. For more information, please contact:
U.S. Corporate and Government Sales
1-800-382-3419
For sales outside of the U.S., please contact:
International Sales
+1-317-581-3793
About the Author
Oleg Sych has been working as a professional software engineer since 1993, building distributed enterprise applications since 2000, and leading development teams since 2002. Between 2009 and 2011, Oleg received several MVP awards from Microsoft in C# and Visual Studio ALM categories. His professional interests include agile software architecture and development process. In his spare time, Oleg is trying to learn how to ski, play tennis, and raise teenage boys.
Dedication
To Tanyusha, Gleb, and Nickita. I am nothing without you.
Acknowledgments
This book would not be possible without help from Brook Farling, Scott Hunter, David Ebbo, Neil Rowe, and Sven Aelterman. Thank you!
We Want to Hear from You!
As the reader of this book, you are our most important critic and commentator. We value your opinion and want to know what were doing right, what we could do better, what areas youd like to see us publish in, and any other words of wisdom youre willing to pass our way.
You can email or write me directly to let me know what you did or didnt like about this bookas well as what we can do to make our books stronger.
Please note that I cannot help you with technical problems related to the topic of this book, and that due to the high volume of mail I receive, I might not be able to reply to every message.
When you write, please be sure to include this books title and author as well as your name and phone or email address. I will carefully review your comments and share them with the author and editors who worked on the book.
Email:
Mail: Neil Rowe
Executive Editor
Sams Publishing
800 East 96th Street
Indianapolis, IN 46240 USA
Reader Services
Visit our website and register this book at informit.com/register for convenient access to any updates, downloads, or errata that might be available for this book.
Preface
Most of the web applications that exist today work with data. From a simple guest book page on a personal website, to a large product catalog of an online retailer, to customer relationship management (CRM) and enterprise resource planning (ERP) systemsapplications help people work with data. Working with data means dealing with CRUD. Every database application must allow users to Create, Read (or Retrieve), Update, and Delete records. A product catalog will not be functional unless users can add new products to the catalog, search for and retrieve existing products, update product information, and delete discontinued products from the catalog. Of course, a product catalog rarely exists by itself; it is usually a part of a larger e-commerce system, which has to support all CRUD operations not only for products, but also for customers, suppliers, shipping companies, orders, and more.
Implementing each type of CRUD operation requires some form of presentation, business, and data access logic. Implementing them in your applications usually requires a lot of repetition. Data access logic for products is similar to data access logic for customers, as well as suppliers and shipping companies. You write SQL or .NET code (or both) to insert, select, update, and delete database records. Presentation logic for products is also similar to presentation logic for customers, suppliers, and shipping companies. You create ASP.NET pages with text boxes, drop-down lists, validation controls, links, and buttons. What truly distinguishes the different types of entities is their purposethe business logic they represent. The Product entity has attributes such as description, price, picture, and rules that define how to validate product names, what to do when a new product is added to the catalog, or when an existing product is discontinued. The Customer entity has different attributesfirst name, last name, credit card numbersand rules that define what to do when a customer changes his billing address or requests to close his account.
Understanding the business logic, a developer can usually figure out how to implement the required data access and presentation logic. The resulting application might not look very pretty, but it will perform its business function. The ASP.NET framework provided all building blocks required for implementing database applications, and although it made web developers a lot more productive, it offered little assistance with the repetitive coding of CRUD operations and made it easy for them to fall into the trap of code reuse by copy and paste. It is unfortunate that a typical ASP.NET application has numerous web pages full of validators and code-behind files where data access code is intermixed with business logic.
There are many techniques for reducing code duplication. In ASP.NET, user controls are an excellent way to extract common presentation logic and reuse it in multiple pages. Patterns, such as Domain Model and Active Record (Fowler, 2002), can be used to encapsulate business and data access logic in application code. Some developers also choose to use stored procedures and implement this logic in database code instead. Separation of the business and data access code from the presentation code allows you to reuse it in multiple web pages without duplication. However, implementing business and data access layers requires more forethought, expertise, and commitment from developers. To be effective, these techniques must be introduced in the application architecture early. Retrofitting them to an existing application requires a lot of refactoring (Fowler, et al., 1999) and can be very costly. It is not unusual for such a refactoring effort to be longer than the initial application implementation itself.