Information in this document, including URL and other Internet Web
site
references, is subject to change without notice. Unless otherwise
noted, the
example companies, organizations, products, domain names, e-mail
addresses,
logos, people, places and events depicted herein are fictitious, and
no association
with any real company, organization, product, domain name, e-mail
address, logo,
person, place or event is intended or should be inferred. Complying
with all
applicable copyright laws is the responsibility of the user. Without
limiting the
rights under copyright, no part of this document may be reproduced,
stored in or
introduced into a retrieval system, or transmitted in any form or by
any means
(electronic, mechanical, photocopying, recording, or otherwise), or for
any
purpose, without the express written permission of Microsoft
Corporation.
Microsoft may have patents, patent applications, trademarks,
copyrights, or other
intellectual property rights covering subject matter in this document.
Except as
expressly provided in any written license agreement from Microsoft,
the furnishing
of this document does not give you any license to these patents,
trademarks,
Using Design Patterns in the Presentation Layer 11
In This Chapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . 11
Benefits of Using Design Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . 12
Using Design Patterns for the Presentation Layer . . . . . . . . . . . . . . . .
. . . . . . . . . . . . 12
Choosing Design Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . 13
Frequently Used Presentation Layer Patterns . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . 14
Implementing Design Patterns by Using the User Interface
Process Application Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . 25
Design of the User Interface Process Application Block. . . . . . . . . . . .
. . . . . . . . . . 26
Benefits of Using the User Interface Process Application Block . . . . .
. . . . . . . . . . . 28
Building Applications with the User Interface Process Application
Block . . . . . . . . . . 32
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . 44
Chapter 3
Building Maintainable Web Interfaces with ASP.NET 45
In This Chapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . 45
Creating New Web Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . 45
Creating and Using Web User Controls . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . 46
Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . 76
Paging Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . 77
Supporting Data Updates from the Presentation Layer . . . . . . . . . . . .
. . . . . . . . . . . . 77
Batching Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . 78
Using Optimistic Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . 78
Designing Data Maintenance Forms to Support Create, Read,
Update,
and Delete Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . 78
Implementing Separate Forms for the List and Entity Display . . . . . . .
. . . . . . . . . . 81
Validating Data in the Presentation Layer . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . 90
Why Validate? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . 90
Choosing a Validation Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . 91
Using Validation Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . 91
Handing Validation Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . 92
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . 92
Chapter 5
Managing State in Web Applications 93
Contents v
Chapter 6
Multithreading and Asynchronous Programming
in Web Applications 115
In This Chapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . 115
Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . 116
Using the Thread Pool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . 117
Synchronizing Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . 119
Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . 122
Using Asynchronous Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . 123
Using the .NET Framework Asynchronous Execution Pattern . . . . . . .
. . . . . . . . . . 123
Using Built-In Asynchronous I/O Support . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . 130
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . 131
Chapter 7
Globalization and Localization 133
In This Chapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . 133
Understanding Globalization and Localization Issues . . . . . . . . . . . . .
. . . . . . . . . . . 133
Additional Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . 135
Performing Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . 154
Using Code Access Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . 155
Implementing Security Across Tiers . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . 158
Auditing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . 159
vi Contents
Performing Operational Management . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . 161
Managing Exceptions in the Presentation Layer . . . . . . . . . . . . . . . . .
. . . . . . . . . 161
Monitoring in the Presentation Layer . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . 162
Managing Metadata and Configuration Information . . . . . . . . . . . . . . .
. . . . . . . . . 162
Defining the Location of Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . 164
Deploying Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . 164
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . 164
Appendix B
How To Samples 165
In This Appendix:. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . 165
How To: Define a Formatter for Business Entity Objects . . . . . . . . . . .
. . . . . . . . . . . 166
Defining the ReflectionFormattable Class . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . 217
Defining the ThreadResults Class . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . 218
Defining the Result Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . 219
How To: Use the Trusted Subsystem Model . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . 221
How To: Use Impersonation/Delegation with Kerberos Authentication
. . . . . . . . . . . . 222
How To: Use Impersonation/Delegation with Basic or Forms
Authentication . . . . . . . . 223
How To: Localize Windows Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . 224
How To: Define a Catch-All Exception Handler in Windows Forms-
based
Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . 226
Additional Resources 227
1
The Presentation Layer
Introduction
Most, if not all, applications require some level of user interaction. In
today’s
distributed applications, the code that manages this user interaction is in the
presentation layer.
Most simple presentation layers contain user interface components, such as
Microsoft® Windows Forms or ASP.NET Web Forms. These components
typically
contain code to perform functions such as configuring the visual appearance
of
How To Use This Guide
This guide addresses specific goals of presentation layer component design.
This
guide provides prescriptive recommendations and code samples that enable
you to
use design patterns and Microsoft .NET Framework programming idioms
effectively
in the presentation layer of your applications. The intent of this guide is to
help you
increase the portability, maintainability, scalability, security, and overall
design
quality of your presentation layer code; it does not discuss aesthetic user
interface
design issues.
The information in this guide is organized into the following chapters:
● This chapter introduces the topics in this guide and provides guidance on
basic
terminology. It also notes the decisions the authors of this guide assume you
have
already made.
Additionally, this chapter summarizes the key messages that are documented
in
detail elsewhere in this guide and describes how this guide fits into the
Microsoft
patterns & practices family of documentation.
● Chapter 2, “Using Design Patterns in the Presentation Layer,” describes
how to
separate the responsibilities of components in the presentation layers by
using
common design patterns.
controls in the presentation layer.
● Chapter 4, “Managing Data,” describes the correct way for components in
the
user interface (UI) to access, present, update, and validate data input, and
how
the UI participates in maintaining data integrity.
Chapter 1: The Presentation Layer 3
The first of these topics, “Accessing and Representing Data,” compares and
contrasts various techniques for accessing data in the presentation layer. This
topic describes the best way to represent data in disconnected and streaming
applications and how best to use transactions in the presentation layer. This
topic
also describes the importance of a layered approach to data access and how
to
use message-driven techniques, data access logic components, and business
objects to access data in specific scenarios.
The second topic in the chapter, “Presenting Data Using Formatters, Data
Binding,
and Paging,” describes how to use .NET Framework formatter classes, data
binding techniques, and paging to display data to the user.
The third topic in the chapter, “Supporting Data Updates from the
Presentation
Layer,” describes how the presentation layer can participate in updates to
data
in a back-end data store such as a relational database. This topic describes
how to
create data maintenance forms that let users create, read, update, and delete
data
entities either individually or as part of a list. It also describes how to
increase
● Chapter 6, “Multithreading and Asynchronous Programming in Web
Applications,”
describes how to increase performance and responsiveness of the code in
the presentation layer by using multithreading and asynchronous
programming.
This chapter describes how to use .NET Framework thread pools to simplify
multithreaded code in your application. In situations where the thread pool
is inappropriate, the chapter describes how to manually create, manage, and
synchronize threads in your code.
4 Design and Implementation Guidelines for Web Clients
This chapter also describes how and when to use asynchronous method
invocation,
by using delegates. Delegates represent method calls on objects; with
delegates,
you can start methods asynchronously by using the BeginInvoke and
EndInvoke delegate methods.
● Chapter 7, “Globalization and Localization,” describes how globalization
and
localization requirements affect the development of your presentation layers.
This chapter addresses how the .NET Framework uses cultures to define
language-
specific and country-specific issues, such as number formats and currency
symbols; it also describes how and when it might be useful to define
alternative
cultures programmatically.
Additionally, this chapter describes how to format various .NET Framework
data
types appropriately, according to the current culture. As part of this
discussion, it
describes how to create custom resource files to hold language-specific
applications, see Application Architecture for .NET: Designing Applications
and Services
on MSDN (http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/dnbda/html/
distapp.asp). It defines the application architecture illustrated in Figure 1.1.
Chapter 1: The Presentation Layer 5
Data Access Logic
Components
Service Agents
UI Components
UI Process Components
Service Interfaces
Business
Workflows
Business
Components
Business