BETA - Published for Community Feedback. This page is a wiki.  Please provide your feedback in the comments below.

Three-Tier RIA Application Scenario

J.D. Meier, Alex Homer, David Hill, Jason Taylor, Prashant Bansode, Lonnie Wall, Rob Boucher Jr, Akshay Bogawat.

Applies To

  • Microsoft .NET 3.5
  • Silverlight 2
  • Windows Server 2003, 2008

Scenario

In this scenario, you are using an existing database schema in the development of a Rich Internet Application (RIA). The application will be accessible from the Internet. However, the application does not handle personal information or sensitive data. As a result, this scenario can be implemented using a three-tier design with the client workstation representing one tier, a web server representing the second tier, and a database server representing the third and final tier.

Scenario.PNG

Key Characteristics

  • The presentation layer is hosted in a rich user interface container.
  • Service, business, and data access layers are located on the same physical machine.
  • The presentation layer interacts with the business layer through a service.
  • The application uses an existing database schema.

Pattern Solution

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

PatternsSolution (2).PNG

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.
  • Transaction Script objects are used to handle business request processing.
  • A Repository pattern is used to access domain entities.
  • A Domain Entity pattern is used to define business entities that contain data only.
  • A Data Mapper pattern is used to map domain entities to the database schema.

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 Mapper Implement a mapping layer between objects and the database structure in order to move data from one structure to another while keeping them independent.
Data Transfer Object Used to combine multiple data structures into a unified view that can be passed across physical and logical boundaries.
Domain Entity A set of business objects modeled after a domain that represents the relationship between entities in the domain but does not contain behavior or rules related to the entities.
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.
Repository An in-memory representation of a data source that works with domain entities.
Service Interface A programmatic interface that systems used to interact with other systems.
Transaction Script Encapsulate operations that occur as part of a transaction into a single operation that executes the individual operations one at a time.

Pattern Solution Details

The pattern solution identifies primary patterns used by this scenario. The solution summary provides information on 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.
  • Silverlight controls will be used to create a rich composite view.
  • Use a message-based protocol for communication between the presentation and business layers.
  • Implement a Domain Entity pattern to represent business entities.
  • Interact with the database using Repository and Data Mapper patterns.
Client Workstation – Presentation Layer
The presentation layer is responsible for accepting user input and rendering the user interface. In this scenario a front controller pattern is used to handle user interaction. Interaction with the business tier is done through a service, and data structures returned from the service are bound to controls on the page.

Pattern Info Example
Front Controller
The front controller is composed of a handler and command objects. The handler accepts all requests, instantiates the proper command object, and passes control to the command object. The combination of handler and command objects represent a front controller. This design works well with rich user interfaces where you may have multiple actions that can execute the same command.
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, and user interface controls are used to create a Composite View. Most development environments use controls and designers to create the user interface.
Controls are used to generate markup code that is rendered within the rich user interface (UI) container. Controls used to define the user interface are responsible for generating markup code. For example, when a Silverlight page is rendered the page and controls on the page will generate XAML, which is then used to render the user interface in a Silverlight plug-in.
Bound Data Control
Data objects returned from the service are either bound to or used to initialize Composite View objects. In this design, message objects, which represent Data Transfer Objects (DTO), contain data objects that are returned from the service. In most cases these data objects can be bound to controls in the view.
Proxy
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.
Web 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. Business entities from the business layer are translated to and from service data structures within the service layer.

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.
Web 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. The transaction script pattern is used to define business process objects that handle requests and support the management of transactions and context data as required.

Pattern Info Example
Façade
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 that 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.
Can also be used to manage context information. This pattern can be used to implement a single point of entry for each request where context can be initialized and used throughout the request processing.
Web 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. In this scenario, a domain entity pattern is used to define business entities that are mapped to a database schema using a data mapper pattern.

Pattern Info Example
Repository
A repository provides an in-memory representation of domain entities. As a general rule objects are normally not stored in memory with a web application or service. However, the repository hides all database interaction and provides operations that operation domain entities as if they were in memory.
Criteria objects can be used by the repository to generate database queries. In cases where you have multiple complex queries to retrieve an entity, a criteria object can be used to define selection criteria that is passed into a single get operation.
Data Mapper
Impedance mismatch between objects and relational data require mapping layer There are several factors that cause a mismatch between object and relational database structures. As a result, a mapping layer can be used to map object structures to database schemas. This allows developers to perform operations against the objects without having to know the database schema.
Domain Entity
Business entities are defined that represent entities within the application domain. In this scenario you have full control of the database schema and can define business entities based on entities in the domain and the relationships those entities have with each other.

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.

TechnicalSolution.PNG

The following is a summary of the technologies, interfaces or objects shown above:
  • The Model-View-Controller pattern is implemented using objects in the presentation layer.
  • Silverlight is used to define and render the user interface in a client browser.
  • 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.
  • Repository objects are used to provide a data access interface.
  • Business entities are defined using the ADO.NET Entity Framework’s Entity Data Model (EDM).
  • The ADO.NET Entity Framework is responsible for mapping business entities to the database schema.
  • 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 objects to implement a Front Controller pattern (MVC).
  • Use Silverlight to render a rich user interface within a browser.
  • Use WCF services to provide an interface between the presentation and business layers.
  • Host the WCF service using Microsoft Internet Information Services (IIS).
  • Implement a message-based interface for interaction between the presentation and business layers.
  • Use the ADO.NET Entity Framework to define business entities based on an existing database schema.
  • Access tables, views, or stored procedures from a SQL Server database.
Client Workstation – Presentation Layer
Check & More Info Example
MVC Objects
Objects are used in the presentation tier to implement a Front Controller pattern (MVC) The controller intercepts all inbound requests, interacts with the business layer to initialize a model (data), and chooses the appropriate view (page) to display.
A handler object and abstract command interface are defined to support the Front Controller pattern (MVC) Control events can be wired to a single handler, which then instantiates the appropriate command object, and interacts with the command object using the command interface.
Concrete command objects that implement the command interface are defined. Each command that can be executed in the interface is represented by a command object. Regardless of what action the user takes to invoke a command, such as a menu link or toolbar button, there would only be one command object associated with that command.
Silverlight
Silverlight is used to host and render the rich internet application within a browser. Silverlight is a browser plug-in that uses XAML to define and render rich user interfaces within a browser. The plug-in has to be installed on the client in order to access Silverlight content.
Silverlight controls are used to define the user interface. A designer, such as Expression Blend, uses Silverlight controls to create the user interface. Controls are available with Expression Blend and product companies are also developing Silverlight controls.
Silverlight supports both JavaScript and .NET code running on the client. JavaScript can be used to improve user experience and the .NET code provides an environment for implementing object oriented patterns, such as the front controller.
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.
Web 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
Web 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
Web Server – Data Access Layer
Check & More Info Example
Identify parent objects in your entity data model and define a separate repository object for each one. Parent objects represent the top level object in a in a group of objects that are related. If you have a one-to-one mapping between objects and tables or views in the database you will normally create a repository object for each table or view object.
Define operations that use Entity Framework data entities, or business entities, as parameters and return values. Entities.PNG
For objects that can be retrieved using multiple complex queries consider using a criteria object to define the queries instead of defining a separate operation for each query. A typical approach is to define a common criteria interface that all criteria objects implement. InterfaceCriteria.PNG
Business Entities
Use entities from the Conceptual Model of the ADO.NET Entity Framework’s Entity Data Model to represent business entities. Business entities are defined as part of the Entity Data Model (EDM) used by the ADP.NET Entity Framework. When first creating an EDM the recommended procedure is to use the EDM Wizard. You can also use EdmGen.exe to generate an EDM from an existing database schema. The ADO.NET Entity Framework also provides an EDM visual designer that you can use to manage the EDM.
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.
ADO.NET Entity Framework
The ADO.NET Entity Framework is responsible for managing the database schema and providing a mapping layer between the database and data entities. In this scenario a database schema does not exist and we can take advantage of the schema generation and management functionality provided by the ADO.NET Entity Framework. This is accomplished by using the Entity Data Model (EDM) visual designer to create the EDM.
The Entity Data Model provides a mapping layer between the conceptual model and storage model. The Entity Data Model (EDM) is actually composed of three separate language types. The store schema definition language (SSDL) is used to define the storage, or database, schema. The conceptual storage definition language (CSDL) is used to define the entity types, complex types, associations, entity containers, entity sets, and association sets in the application domain. Finally, the mapping specification language (MSL) is used to describe the mapping between the conceptual model and target database.
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.

Additional Resources

Last edited Jan 24, 2009 at 1:09 AM by rboucher, version 6

Comments

lorenpaulsen Mar 16, 2009 at 12:16 AM 
@maheshkshirsagar

I agree, that would be helpful to see here. The "Service Gateway" pattern article gives some guidance regarding translating business entities and data transfer objects (http://msdn.microsoft.com/en-us/library/ms998552.aspx)

jhollida Mar 2, 2009 at 2:38 PM 
I'm far from an expert but it seems to me that you are short changing the "Rich" part of RIA by placing all of the business layer on the server side. I have been thinking that a subset of the business layer would reside on the client side to allow for some local inteligence in the application. If not, doesn't this just reduce a RIA application to a "prettier than HTML" application? You've got all the power of the CLR with Silverlight, why wouldn't you want to call some of the business logic within the client? A friend just pointed me to the Three-Tier Mobile Application Scenario (http://apparch.codeplex.com/Wiki/View.aspx?title=Three-Tier%20Mobile%20Application%20Scenario&referringTitle=App%20Patterns) which looks a lot more like what I would have expected in a RIA.

maheshkshirsagar Jan 22, 2009 at 1:31 PM 
What would be a typical structure of a class/service to translate between business entities and WCF data contracts? Can you provide with a sample?