Thứ Bảy, 8 tháng 3, 2014

Microsoft ASP NET and AJAX

v
Contents at a Glance
Part I
The (Much Needed) Facelift for the Old Web
1 Under the Umbrella of AJAX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 The Easy Way to AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3 AJAX Architectures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Part II
Power to the Client
4 A Better and Richer JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
5 JavaScript Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
6 AJAX Design Patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
7 Client-Side Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
8 Rich Internet Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
www.zshareall.com
www.zshareall.com
vii
Table of Contents
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xi
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xiii
Part I
The (Much Needed) Facelift for the Old Web
1 Under the Umbrella of AJAX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
What Web Do We Want? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
It’s All About User Experience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Origins of the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Paradox of the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
The Biggest Benefi t of AJAX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
What’s AJAX, Exactly? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
The Paradigm Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
AJAX and New Web Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Adding AJAX Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Architecture Is the Concern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
The Case for Rich Internet Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2 The Easy Way to AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
The ASP.NET AJAX Infrastructure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
The Page’s Script Manager. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
The Microsoft JavaScript Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Partial Rendering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
The UpdatePanel Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Programming Updatable Panels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Minimizing Data Transfer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Shades of Partial Rendering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
AJAX and JavaScript Injections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Remote Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Widgets and Effects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Microsoft is interested in hearing your feedback so we can continually improve our books and learning
resources for you. To participate in a brief online survey, please visit:
www.microsoft.com/learning/booksurvey/
What do you think of this book? We want to hear from you!
www.zshareall.com
viii
Table of Contents
3 AJAX Architectures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
The AJAX Service Layer Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Architectural Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Inside the HTTP Façade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
The AJAX Presentation Layer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Security Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
The AJAX Server Pages Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Architectural Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
The Classic Postback Model Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Libraries in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Part II
Power to the Client
4 A Better and Richer JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
JavaScript Today . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
The Language and the Browser. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Pillars of the Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
JavaScript (If Any) of the Future . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
The Microsoft AJAX Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Overview of the Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
JavaScript Language Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Object-Oriented Extensions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Framework Facilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
5 JavaScript Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
From Server Controls to JavaScript Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
The ASP.NET Factor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
The Widget Factor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
The jQuery Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
The Library at a Glance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
The Core Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
jQuery Selectors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Working on Wrapped Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
jQuery Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
www.zshareall.com
Table of Contents
ix
6 AJAX Design Patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Design Patterns and Code Development. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Generalities About Design Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Patterns in AJAX Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Patterns for JavaScript Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
The Singleton Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
The Model-View-Controller Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
The On-Demand JavaScript Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
The Predictive Fetch Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Generalities of the Predictive Fetch Pattern . . . . . . . . . . . . . . . . . . . . . . . 178
Creating a Reference Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
The Timeout Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Generalities of the Timeout Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
A Timeout Pattern Reference Implementation. . . . . . . . . . . . . . . . . . . . . 188
Related Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
The Progress Indicator Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Generalities of the Progress Indicator Pattern . . . . . . . . . . . . . . . . . . . . . 194
A Progress Indicator Reference Implementation . . . . . . . . . . . . . . . . . . . 196
Canceling an Ongoing Remote Task. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Other Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
The Micro-Link Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
The Cross-Domain Proxy Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
The Submission Throttling Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
7 Client-Side Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
An Architectural Tour of ASP.NET Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . 224
Defi ning the HTML Template. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Defi ning the Data Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Data Binding at the Time of AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
The Browser-Side Template Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Generalities of the BST Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Creating a BST Reference Implementation . . . . . . . . . . . . . . . . . . . . . . . . 238
The HTML Message Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Generalities of the HM Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Developing an HM Reference Implementation . . . . . . . . . . . . . . . . . . . . 253
www.zshareall.com
x
Table of Contents
A Look at ASP.NET AJAX 4.0. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
ASP.NET AJAX Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
ASP.NET Library for ADO.NET Data Services. . . . . . . . . . . . . . . . . . . . . . . 266
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
8 Rich Internet Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Looking for a Richer Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
The Dream of Binary Code Running over the Web . . . . . . . . . . . . . . . . . 270
Browser Plug-ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Microsoft Silverlight at a Glance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Elements of the Silverlight Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Graphics and Multimedia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Building Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
The Programming Model of Microsoft Silverlight . . . . . . . . . . . . . . . . . . . . . . . 282
WPF-Based User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
The .NET Base Class Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Isolated Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Microsoft Silverlight and Code Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
The Security Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Security Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Secure by Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Microsoft is interested in hearing your feedback so we can continually improve our books and learning
resources for you. To participate in a brief online survey, please visit:
www.microsoft.com/learning/booksurvey/
What do you think of this book? We want to hear from you!
www.zshareall.com
xi
Acknowledgments
A team of people helped me to assemble this book.
Ben Ryan was sneakily convinced to support the project on a colorful Las Vegas night,
during an ethnic dinner during which we watched waiters coming up from and going down
to the wine cellar in transparent elevators.
Lynn Finnel just didn’t want to let Dino walk alone in this key project after brilliantly
coordinating at least fi ve book projects in the past.
Kenn Scribner is now Dino’s offi cial book alter ego. Kenn started working with Dino on
books back in 1998 in the age of COM and the Active Template Library. How is it possible
that a book with Dino’s name on the cover isn’t reviewed and inspired (and fi xed) by Kenn’s
unique and broad perspective on the world of software? The extent to which Kenn can be
helpful is just beyond human imagination.
Roger LeBlanc joined the team to make sure that all these geeks sitting together at the
same virtual desktop could still communicate using true English syntax and semantics.
I owe you all the (non-rhetorically) monumental “Thank you” for being so kind, patient,
and accurate.
—Dino
www.zshareall.com
www.zshareall.com
xiii
Introduction
This book is the Web counterpart to another recently released book I co-authored with
Andrea Saltarello: Microsoft .NET: Architecting Applications for the Enterprise (Microsoft
Press, 2008). I wrote it, in part, in response to the many architectural questions—both small
questions and big ones—that I was asked repeatedly while teaching ASP.NET, AJAX, and
Silverlight classes.
Everybody in the industry is committed to AJAX. Everybody understands the impact of it.
Everybody recognizes the enormous power that can be derived from its employment in
real-world solutions.
Very few, though, know exactly how to make it happen. There are so many variations to AJAX
and so many implementations that even after you have found one that suits your needs, you
are left wondering whether that is the best possible option.
The fact is that AJAX triggered a chain reaction in the world of the Web. AJAX represents a
change of paradigm for Web applications. And, as the history of science proves, a paradigm
shift has always had a deep impact, especially in scenarios that were previously stable and
consolidated.
I estimate that it will take about fi ve years to absorb the word AJAX (and all of its background)
into the new defi nition of the Web. And the clock started ticking about four years ago. The
time at which we say “the Web” without feeling the need to specify whether it contains AJAX
or not . . . well, that time is getting closer and closer. But it is not that time yet.
Tools and programming paradigms for AJAX, which were very blurry just a few years ago,
are getting sharper every day. Whether we are talking about JavaScript libraries or suites
of server controls, I feel that pragmatic architectures can be identifi ed. You fi nd them
thoroughly discussed in Chapter 3, “AJAX Architectures.”
Architecting a Web application today is mostly about deciding whether to prefer the richness
of the solution over the reach of the solution. Silverlight and ASP.NET AJAX are the two
platforms to choose from as long as you remain in the Microsoft ecosystem. But the rich vs.
reach dilemma is a general one and transcends platforms and vendors. A neat answer to that
dilemma puts you on the right track to developing your next-generation Web solution.
Who This Book Is For
I believe that this book is ideal reading for any professionals involved with the ASP.NET
platform and who are willing or needing to fi nd a solution that delivers a modern and rich
user experience.
www.zshareall.com
xiv
Introduction
Companion Content
Examples of techniques and patterns discussed in the book can be found at the following
site: http://www.microsoft.com/learning/en/us/books/12926.aspx.
Hardware and Software Requirements
You’ll need the following hardware and software to work with the companion content
included with this book:

Nearly any version of Microsoft Windows, including Vista (Home Premium Edition,
Business Edition, or Ultimate Edition), Windows Server 2003 and 2008, and
Windows XP Pro.

Microsoft Visual Studio 2008 Standard Edition, Visual Studio 2008 Enterprise Edition,
or Microsoft Visual C# 2008 Express Edition, and Microsoft Visual Web Developer 2008
Express Edition.

Microsoft SQL Server 2005 Express Edition, Service Pack 2 or Microsoft SQL Server 2005,
Service Pack 3, or Microsoft SQL Server 2008.

The Northwind database of Microsoft SQL Server 2000 is used to demonstrate data-access
techniques. You can obtain the Northwind database from the Microsoft Download Center
(http://www.microsoft.com/downloads/details.aspx?FamilyID=06616212-0356-46A0-8DA2-
EEBC53A68034&displaylang=en).

1.6 GHz Pentium III+ processor, or faster.

1 GB of available, physical RAM.

Video (800 by 600 or higher resolution) monitor with at least 256 colors.

CD-ROM or DVD-ROM drive.

Microsoft mouse or compatible pointing device.
Find Additional Content Online
As new or updated material becomes available that complements this book, it will be posted
online on the Microsoft Press Online Developer Tools Web site. The type of material you
might fi nd includes updates to book content, articles, links to companion content, errata,
sample chapters, and more. This Web site is available at http://www.microsoft.com/learning/
books/online/developer and is updated periodically.
www.zshareall.com

Xem chi tiết: Microsoft ASP NET and AJAX


Không có nhận xét nào:

Đăng nhận xét