CYAN YELLOW MAGENTA BLACK PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS® THE EXPERT’S VOICE® IN .NET Companion eBook Available Pro ASP.NET 4 CMS Pro Dear Reader, Visual Studio 2010 and version 4 of the .NET Framework are upon us. There’s a lot A of new ground to cover, but also many benefits to enjoy; as .NET developers, we gain the Managed Extensibility Framework, the Dynamic Language Runtime, P/ LINQ, and more, all of which I cover in this book. There are also a bundle of new S Pro language enhancements this time around, including much better support for Alan Harris, Author of functional and dynamic programming. P I wrote this book to introduce these topics, as well as show them in use in a real- Pro IronPython world application. This ensures that you’ll be introduced to realistic examples that . demonstrate the full functionality of the .NET 4 Framework for ASP.NET, and that N ASP.NET 4 CMS you'll be able to apply your new skills to any web development situation. Building a content management system that is both flexible and powerful is not an easy task, but with the technology available in .NET 4, it is actually a lot of fun. E The CMS demonstrated throughout this book is a complete application, serving pages to millions of users at this very moment. It was built in a fairly short amount T of time to solve the problems of a business that operates on an international scale, and it has been successful in that capacity. It was successful primarily because of the powerful tools provided by .NET 4. Advanced Techniques for C# Developers Knowledge and experience are not zero-sum games; personally, I enjoy sharing 4 with and learning from other developers of all experience levels. Programming is a passion and something to be enjoyed. My hope is that somewhere between the Using the .NET 4 Framework code in these pages and the new technology Microsoft has offered, there’s at least C one thing that makes you pause for a moment and say, “Now that is cool.” Alan Harris M MCTS .NET Framework 3.5, ASP .NET Applications THE APRESS ROADMAP S Pro Learn the latest features of .NET 4 to build Companion eBook Pro C# 2010 ASP.NET 4 CMS Introducing and the powerful ASP.NET 4 web applications .NET 4.0 .NET 4 Platform Pro Silverlight 4 in C# Pro ASP.NET 4 See last page for details AcCc#e l2e0ra1te0d in C# 2010, Pro on $10 eBook version Fourth Edition Dynamic .NET 4 Applications Alan Harris SOURCE CODE ONLINE H www.apress.com ISBN 978-1-4302-2712-0 a 54299 r r i US $42.99 s Shelve in: Software Engineering/System Analysis 9 781430 227120 User level: Intermediate–Advanced this print for content only—size & color not accurate 7.5 x 9.25 spine = 0.71875" 320 page count Pro ASP.NET 4 CMS Advanced Techniques for C# Developers Using the .NET 4 Framework ■ ■ ■ Alan Harris i Pro ASP.NET 4 CMS: Advanced Techniques for C# Developers Using the .NET 4 Framework Copyright © 2010 by Alan Harris 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-2712-0 ISBN-13 (electronic): 978-1-4302-2713-7 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: Ewan Buckingham, Matthew Moodie Technical Reviewer: Jeff Sanders Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Anne Collett Copy Editor: Kim Wimpsett Compositor: MacPS, LLC Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders- [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit 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 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 www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code. ii “What you leave behind is not what is engraved in stone monuments, but what is woven into the lives of others.” —Pericles iii Contents at a Glance ■Contents at a Glance............................................................................................iv(cid:1) ■Contents................................................................................................................v(cid:1) ■About the Author.................................................................................................xii(cid:1) ■About the Technical Reviewer............................................................................xiii(cid:1) ■Acknowledgments..............................................................................................xiv(cid:1) ■Introduction ........................................................................................................xv ■Chapter 1: Visual Studio 2010 and .NET 4.............................................................1(cid:1) ■Chapter 2: CMS Architecture and Development....................................................29(cid:1) ■Chapter 3: Parallelization....................................................................................47(cid:1) ■Chapter 4: Managed Extensibility Framework and the Dynamic Language Runtime.........................................................103(cid:1) ■Chapter 5: jQuery and Ajax in the Presentation Tier.........................................135(cid:1) ■Chapter 6: Distributed Caching via Memcached...............................................165(cid:1) ■Chapter 7: Scripting via IronPython..................................................................197(cid:1) ■Chapter 8: Performance Tuning, Configuration, and Debugging..........................229(cid:1) ■Chapter 9: Search Engine Optimization and Accessibility................................257(cid:1) ■Index.................................................................................................................285(cid:1) iv Contents ■Contents at a Glance............................................................................................iv(cid:1) ■Contents................................................................................................................v(cid:1) ■About the Author.................................................................................................xii(cid:1) ■About the Technical Reviewer............................................................................xiii(cid:1) ■Acknowledgments..............................................................................................xiv(cid:1) ■Introduction.........................................................................................................xv ■Chapter 1: Visual Studio 2010 and .NET 4.............................................................1(cid:1) Who This Book Is For......................................................................................................1(cid:1) Who This Book Is Not For (or “Buy Me Now, Read Me Later”)........................................2(cid:1) What’s New in .NET 4......................................................................................................2(cid:1) C# Optional and Named Parameters......................................................................................................3(cid:1) C#’s dynamic Keyword...........................................................................................................................5(cid:1) Dynamic and Functional Language Support........................................................................................10(cid:1) Parallel Processing..............................................................................................................................10(cid:1) Managed Extensibility Framework (MEF).............................................................................................13(cid:1) Distributed Caching with Velocity........................................................................................................13(cid:1) ASP.NET MVC.......................................................................................................................................16(cid:1) A Tour of Visual Studio 2010.........................................................................................18(cid:1) Windows Presentation Foundation......................................................................................................18(cid:1) Historical Debugging............................................................................................................................19(cid:1) Improved JavaScript IntelliSense.........................................................................................................21(cid:1) jQuery Support.....................................................................................................................................22 v ■ CONTENTS Building a CMS..............................................................................................................24(cid:1) CMS Functional Requirements.............................................................................................................24(cid:1) Creating the Application Framework...................................................................................................25(cid:1) Summary.......................................................................................................................28(cid:1) ■Chapter 2: CMS Architecture and Development....................................................29(cid:1) Motivations for Building a CMS.....................................................................................29(cid:1) Motivations for Using .NET............................................................................................30(cid:1) Application Architecture................................................................................................30(cid:1) The CMS Application Tiers...................................................................................................................32(cid:1) CommonLibrary: The Data Transfer Objects........................................................................................33(cid:1) GlobalModule: The HttpModule............................................................................................................35(cid:1) Components of a CMS Page..........................................................................................37(cid:1) Buckets................................................................................................................................................37(cid:1) Embeddable Objects............................................................................................................................39(cid:1) Embeddable Permissions.....................................................................................................................41(cid:1) Handling CMS Content..................................................................................................43(cid:1) The Content Table................................................................................................................................43(cid:1) The ContentVersion Table....................................................................................................................44(cid:1) Assembling Content on Demand..........................................................................................................44(cid:1) How Embeddable Objects Handle Versions.........................................................................................46(cid:1) Summary.......................................................................................................................46(cid:1) ■Chapter 3: Parallelization....................................................................................47(cid:1) What Is Parallelization?.................................................................................................47(cid:1) Good Parallelization Candidates...........................................................................................................47(cid:1) Differences from Multithreading..........................................................................................................48(cid:1) Parallel Pitfalls..............................................................................................................48(cid:1) Deadlocks............................................................................................................................................48(cid:1) Race Conditions...................................................................................................................................51(cid:1) Thread Starvation.................................................................................................................................54(cid:1) vi ■ CONTENTS Amdahl’s Law......................................................................................................................................55(cid:1) .NET 4 Parallelization Concepts....................................................................................56(cid:1) Task vs. Data Parallelism.....................................................................................................................56(cid:1) Task Parallel Library.....................................................................................................56(cid:1) Task.Wait()...........................................................................................................................................57(cid:1) Parallel.For() and Parallel.ForEach().....................................................................................................59(cid:1) Parallel LINQ (aka PLINQ)..............................................................................................59(cid:1) .AsParallel()..........................................................................................................................................60(cid:1) CMS Parallelization Opportunities.................................................................................61(cid:1) Creating a Data Mining Embeddable....................................................................................................62(cid:1) Expanding the Data Mining Tasks........................................................................................................66(cid:1) Tagging.........................................................................................................................70(cid:1) Tagging on the Client...........................................................................................................................73(cid:1) Fleshing Out the Tagging Embeddable................................................................................................75(cid:1) What’s in a Name?...............................................................................................................................76(cid:1) Handling Tag Input...............................................................................................................................79(cid:1) Tag Processing in the Business Tier....................................................................................................82(cid:1) POST Problems....................................................................................................................................87(cid:1) Finalizing Tag Storage.........................................................................................................................89(cid:1) Inserting Tags......................................................................................................................................92(cid:1) Content Tags........................................................................................................................................96(cid:1) Summary.....................................................................................................................102(cid:1) ■Chapter 4: Managed Extensibility Framework and the Dynamic Language Runtime.........................................................103(cid:1) Managed Extensibility Framework..............................................................................103(cid:1) The Manual Way.................................................................................................................................103(cid:1) The MEF Way.....................................................................................................................................105(cid:1) Working from Usage to Implementation............................................................................................106(cid:1) Exposing Libraries via MEF................................................................................................................106(cid:1) A Simple Plug-in Contract..................................................................................................................107(cid:1) vii ■ CONTENTS Implementing the Plug-In...................................................................................................................107(cid:1) Using the Plug-In................................................................................................................................108(cid:1) Catalogs and Containers....................................................................................................................112(cid:1) Supporting Multiple Parts..................................................................................................................113(cid:1) Dynamic Language Runtime.......................................................................................117(cid:1) The dynamic Keyword........................................................................................................................118(cid:1) Benefits of the dynamic Keyword......................................................................................................119(cid:1) CMS Plug-Ins..............................................................................................................120(cid:1) IEmbeddable......................................................................................................................................121(cid:1) Server Controls as Embeddables.......................................................................................................122(cid:1) Displaying Embeddables....................................................................................................................124(cid:1) PageAssembler..................................................................................................................................125(cid:1) Additional Methodology Benefits................................................................................128(cid:1) Missing DLLs......................................................................................................................................128(cid:1) Exceptions in Embeddables...............................................................................................................129(cid:1) A More Complex Emeddable.......................................................................................130(cid:1) Breadcrumbs......................................................................................................................................130(cid:1) Navigating the CMS Tree...................................................................................................................131(cid:1) Summary.....................................................................................................................133(cid:1) ■Chapter 5: jQuery and Ajax in the Presentation Tier.........................................135(cid:1) An Introduction to jQuery............................................................................................135(cid:1) The $() Factory...................................................................................................................................136(cid:1) Implicit Iteration.................................................................................................................................138(cid:1) Ajax via jQuery............................................................................................................139(cid:1) Caching and Ajax...............................................................................................................................140(cid:1) Avoiding Caching on GET Requests...................................................................................................141(cid:1) Lightweight Content Delivery with Ajax and HTTP Handlers..............................................................142(cid:1) Handling Asynchronous Errors...........................................................................................................147(cid:1) Handling DOM Modifications..............................................................................................................150(cid:1) Improving the CMS Admin with jQuery.......................................................................154(cid:1) viii
Description: