Wednesday, October 27, 2010

App Pattern: Four-Tier Web Application Scenario (Table Module) CodePlex

Applies To

  • ASP.NET 2.0, 3.5
  • Windows Server 2003, 2008


In this scenario, you have an existing database with tables or views that provide data used by your applications. In addition, security requirements dictate that the Web server should be deployed to a perimeter network that is isolated from the organization’s internal network. As a result, the solution is divided across four separate tiers, or physical machines. The client workstation represents a tier that uses a browser application to interact with the Web server. The Web server represents a second tier that contains the presentation layer, which is responsible for handling requests from the user and interacting with the business layer to implement the request. The application server represents the third tier with service, business, and data access layers. The fourth tier in this design is the database server.


Key Characteristics

  • Stand-alone ASP.NET Web application that supports basic CRUD operations.
  • Presentation and Business logic are distributed across physical boundaries.
  • Browser interaction with the Web Server uses standard HTTP GET and POST requests.
  • The presentation layer uses a message-based protocol to interact with the business layer.
  • The application uses data from an existing database schema.
  • Tables and views in the database define data structures used by the application.

Pattern Solution

The following diagram displays the major patterns used by this scenario and the layers where those patterns are implemented.


The following is a summary of the patterns used by this scenario:
  • User interface processing is handled by a Model-View-Controller pattern.
  • The user interface is composed of multiple controls, with some that can be bound to data elements.
  • A proxy is used to communicate between the presentation layer and service layer
  • The Data Transfer Object (DTO) pattern is used to package multiple data structures into one.
  • The service layer provides translation between internal and external data structures.
  • The business layer uses a façade pattern to support coarse-grained message-based operations.
  • Transactions are managed by objects in the business layer.
  • Business entities are defined using the Table Module pattern.
  • The Table or Row Data Gateway pattern is used to provide a data access interface.
  • The Query Object pattern is used to support the generation of SQL queries.

Pattern Description
Bound Data Control Control that can be bound to a data structure, which is used to provide data displayed by the control.
Composite View Combine individual views into a composite representation.
Data Transfer Object Used to combine multiple data structures into a unified view that can be passed across physical and logical boundaries.
Entity Translator Implement an object that transforms message data types to business types for requests and reverses the transformation for responses.
Façade Implement a unified interface to a set of operations to reduce coupling between systems.
Front Controller Implementation of MVC where the controller is composed of a handler and abstract command objects. The handler accepts requests for multiple views and instantiates an appropriate concrete command object to process the request.
Page Controller Implementation of MVC with a one-to-one relationship between the view and controller.
Proxy Provides a local implementation of a remote interface. The proxy is responsible for interaction with the remote interface and the serialization of data sent to and from the remote interface.
Query Object An object that represents a database query.
Row Data Gateway Create an object that acts as a gateway to a single record in a data source.
Service Interface A programmatic interface that systems use to interact with other systems.
Table Data Gateway Create an object that acts as a gateway to a table in a data source.
Table Module Business entity that represents a table or view within a database and provides operations to interact with the table or view.
Transaction Script Encapsulate operations that occur as part of a transaction into a single procedure, and execute the individual operations one at a time.

Pattern Solution Details

The Pattern Solution section above identified the primary patterns used in this scenario. The Pattern Solution Details section will provide additional information about each pattern, and how the patterns are used within the design. Each table below represents a logical layer in the design and contains the patterns associated with that layer.

In this solution you will:
  • Use a Model-View-Controller (MVC) pattern to handle user interaction.
  • Use ASP.NET page, user, and server controls to create a composite view.
  • Use a message-based protocol for communication between the presentation and business layers.
  • Implement a Table Module pattern to represent business entities.
  • Interact with the database using a Query Object pattern to generate queries.
Web Server
The web server is deployed to a perimeter zone as required by security policies. The presentation layer is responsible for accepting user input and rendering the user interface that is returned from the web server.

Pattern Info Example
Front Controller
A controller handles HTTP requests from the browser. Common implementations use an HTTP handler or ISAPI filter to intercept inbound requests to IIS.
The controller interacts with a proxy to retrieve model data. Based on request information the controller creates an instance of the proxy to interact with the application tier, which returns data to the presentation layer.
The controller chooses an appropriate view. Once the model has been initialized request information is used to identify the appropriate page to display, which is initialized with data from the model.
Composite View
Page, server and user controls represent a Composite View. Most development environments use controls and designers to create the user interface.
The controls are used to render HTML that is returned to the browser. Controls used to define the user interface are responsible for generating HTML. A common technique is to loop through all controls on a page when generating the page HTML.
Bound Data Control
Data Transfer Objects are either bound to or used to initialize Composite View objects. In this design, data structures returned from service represent Data Transfer Objects (DTO) that contain data. In most cases these DTOs can be bound to controls in the view.
Provides a local interface used to interact with services on the application tier. Adding a service reference will generate classes used to interact with the service. From a coding perspective the proxy provides a local interface that hides details related to interaction with the service.

Application Server – Service Layer
Service layer components provide access to business logic in the application. The presentation layer interacts with the service layer by passing messages to and from it over a communication channel.

Pattern Info Example
Service Interface
Provides a contract that defines operations and data structures supported by the service. SOAP service and data contracts can be used to define a service interface.
Data Transfer Object (DTO)
Used to combine multiple data structures into a single unit that can be transmitted across physical and logical boundaries. Demographic information contains data from multiple tables or views. Rather than passing the data from each table or view across service boundaries one at a time all of the data related to demographic information is combined into a single structure.
The service message structure represents a DTO that contains one or more data structures. In this scenario business entities are translated into WCF data contracts and the data contracts are included in WCF message contracts, which represent data transfer objects.
Entity Translator
Used to translate between business entities and data structures exposed by the service. Data structures exposed by the service represent an external contract while business entities are internal to the service. As a result, translators are required to move data from one format to another.

Application Server – Business Layer
Business layer components implement the core functionality of the system, and encapsulate the relevant business logic. In this scenario a façade pattern is used to support a message-based, or coarse-grained, interface into the business layer. Request messages from the service are translated into message calls into the business layer through a common interface, which is the façade.

Pattern Info Example
Provides a coarse grained interface into the business layer. Rather than defining chatty object based operations a façade combines multiple operations into a single interface.
The service implementation interacts with the business tier through the façade. The service implementation is responsible for translating between external contracts and internal entities and then passing the request on to the business layer façade.
An interface type is used to define the façade interface. In order to provide a common interface, a typical approach is to define the façade using an Interface type with a single operation used to handle all requests.
Transaction Script
The façade uses transaction script objects to execute business operations. Within the façade an appropriate transaction script object is initialized based on the operation being executed, and control is passed on to a transaction script operation.
Used for business operations those need to be executed as a single unit. Within the transaction script operation a transaction is started, one or more business operations are performed, and the transaction is committed or rolled-back based on the outcome of the business operations.
Table Module
Business entities are defined that represent tables or views in the database. Each table or view from a database that contains information required by your application is defined as a class that represents a business entity.

Application Server – Data Access Layer
Data layer components provide access to data that is hosted within the boundaries of the system, and data exposed by other back-end systems.

Pattern Info Example
Table/Row Data Gateway
Data access interface component that provides a gateway to tables or rows in the database. Each business entity in the design has an associated gateway object that provides operations used to perform Create, Read, Update, and Delete (CRUD) operations against the business entity.
Returns result sets that are used to initialize business entities in the business layer. A common approach with .NET is to return a DataReader from the Gateway and then use the DataReader to initialize one or more business entities.
Query Object
Data helper object that builds queries used to interact with the database. The query object allows you to encapsulate the generation of SQL code within operations that generate specific types of commands using criteria information.
Table or Row data gateway objects use query objects to build queries. While it is possible to generate SQL directly within the gateway object the use of a query object provides a centralized and common approach that can be used by multiple gateway objects.

Technical Solution

The following diagram represents the technical solution by replacing patterns shown in the Pattern Solution with technologies, interfaces, or objects that are used to implement the pattern.


The following is a summary of the technologies, interfaces or objects shown above:
  • Internet explorer is the target browser for this application scenario.
  • The ASP.NET MVC framework is used to implement the Model-View-Control pattern.
  • ASP.NET Page, Server, and User controls are used to define the user interface.
  • A WCF service reference is used to generate a proxy to the service layer.
  • WCF service contracts are used to define the service interface.
  • WCF message contracts are used to define Data Transfer Objects (DTO).
  • Objects are used to provide translation between external and internal data structures.
  • A common interface is defined for the façade into the business layer.
  • Business process objects are used to implement the transaction script pattern.
  • Business entities have a one-to-one relationship with tables or views in the database.
  • Gateway objects are used to provide an interface into the data access layer.
  • The ADO.NET object model is used to implement query objects.
  • The database used by this application scenario is SQL Server.

Technical Solution Details

The technical solution provides information about technologies used to implement patterns that were identified in the Pattern Solution. Each table below represents a logical layer within the design and provides information and examples related to implementing patterns in the solution. In addition to the technical solution for patterns, these tables also provide technical considerations that should be checked for this scenario.

In this solution you will:
  • Use the ASP.NET MVC framework to implement a Front Controller pattern.
  • Use ASP.NET page, user, and server controls to create a composite view.
  • Host the web application using Microsoft Internet Information Services (IIS).
  • Use WCF services to provide an interface between the presentation and business layers.
  • Use ADO.NET to generate queries against the database.
  • Access tables or views from a SQL Server database.
Client Workstation

Check & More Info Example
Target browser is Internet Explorer 6.x and higher. Check the Browser agent for appropriate identification information, such as “MSIE 6.0”.

Web Server

Check & More Info Example
ASP.NET MVC framework is used to implement a Front Controller. The controller intercepts all inbound requests, interacts with the business layer to initialize a model (data), and chooses the appropriate view (page) to display.
Your design does not depend on ASP.NET viewstate. When using ASP.NET MVC, as the standard ASP.NET postback processing is not executed, viewstate is not available when the controller intercepts calls.
Standard ASP.NET page processing is used to handle control events. You can mix standard ASP.NET postback processing with ASP.NET MVC to handle control events.
ASP.NET Page Control
ASP.NET Page controls are used to define each page of the web application. The ASP.NET page control is used to render an HTTP page that will be sent back to clients.
ASP.NET User and Server Controls
ASP.NET User controls are used to provide common menus Common links used to represent a menu are grouped into an ASP.NET user control so they can be reused on multiple pages.
ASP.NET User and Server controls are used to provide the interface ASP.NET user and server controls are used to generate HTML interface elements, such as INPUT, that are used to provide a user interface into the application.
WCF Service Reference
A WCF service reference is added to the presentation layer for interaction with the business layer Within Visual Studio use “Add Service Reference”, which will generate a proxy in the presentation layer that is used to access the service in the business layer. You can also use svcutil.exe to generate a proxy from the command line.

Application Server – Service Layer (WCF)

Check & More Info Example
WCF Service Contract
You are using WCF to define business operations called by the presentation layer. The service layer is defined using WCF service, data, message, and fault contracts.
The service contracts are coarse grained. Operations exposed by the service are application scoped. For example, instead of providing multiple operations to return demographic information you would provide one operation that returns all data related to demographic information.
A configuration file with WCF binding information is defined for the assembly that implements the service. This makes the WCF service discoverable within a Visual Studio solution.
WCF Message Contract
Message contracts are used to define Data Transfer Objects. MessageContract.PNG
You have defined explicit message contracts instead of relying on implicit message contracts. ExplicitMessageContract.PNG
Translator Objects
Classes are defined that provide operations to translate between business entities and WCF data contracts. TranslatorObjects.PNG

Application Server – Business Layer

Check & More Info Example
Façade Interface
The business layer implements a façade with coarse grained operations. Define a generic operation that takes the command and array of data objects as parameters and returns an Errors collection, which would only contain errors if they occurred during processing.
An Interface type is used to define the façade interface. FacadeInterface.PNG
The Façade acts as a boundary that catches all unhandled exceptions that may occur when processing business operations. Exception.PNG
Business Process Objects
You are using business process objects to handle requests made to the business layer. Business process objects allow you to implement business rules and provide transaction support if required.
Business operations that need to be included in a transaction are combined in a single operation exposed by a business process object that implements the transaction script pattern. TransactionScript.PNG
Business Entities
Classes are defined that represent tables or views from the database. DatabaseView.PNG
Business rules are not implemented within the business entities. With this design the business entities are just data containers. Business rules are handled by business processing components.

Application Server – Data Access Layer

Check & More Info Example
Gateway Objects
A gateway is defined for each business entity in the design. gateway.PNG
The gateway encapsulates all logic related to interacting with the associated table or view. Code related to interacting with a database and generating database commands can only be executed as part of a gateway operation.
Use ADO.NET object model to build queries instead of string concatenation. Queries.PNG
Separate classes are used to define query objects. Instead of implementing the generation of queries within a gateway object you can use objects that are dedicated to building queries and can be reused by multiple gateway objects.

Database Server

Check & More Info Example
Tables and views are accessible to the data access layer. Security in the database is configured to allow access to tables and views from the application tier.
Trusted sub-system is used to access the database. Define a common business identity and then use that identity when accessing tables and views in the database.

No comments:

Post a Comment