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

App Pattern: Three-Tier Mobile Application Scenario

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

Applies To

  • Windows Mobile 6.1
  • Windows Server 2003, 2008

Scenario

In this scenario, you have full control over the creation and management of the database schema. The application being created is a rich client that will be deployed to mobile handheld devices. Data for the application is located in an external database deployed within the organizations local area network (LAN). The application also uses local storage on the mobile device to support disconnected operations. As a result, this scenario can be implemented using a three-tier design with the mobile device, application server, and database server.

Scenario.PNG

Key Characteristics

  • Presentation, business, and data access layers are implemented on the mobile device.
  • Local storage on the mobile device is used for disconnected scenarios.
  • The mobile device interacts with an application server using WCF services.
  • Service, business, and data access layers are implemented on the application server.
  • Some business rules are duplicated on the mobile device and application server.
  • The service layer uses a message-based protocol to interact with the business layer.
  • The application has full autonomy over the database schema.

Pattern Solution

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

PatternsSolution.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.
  • The presentation layer on the mobile device interacts with the business layer using transaction script objects.
  • A Repository pattern is used on the mobile device to access presentation entities and data contracts returned from the service.
  • Presentation entities are used on the mobile device to represent data that is maintained on the device.
  • The business layer on the application server uses a Façade pattern to implement a message-based interface between the service and business layer.
  • Transaction Script objects are used to handle business request processing on the application server.
  • A Repository pattern is used to access domain entities on the application server.
  • 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 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.
Presentation Entity A set of objects used to maintain data that is unique to the presentation layer of the application.
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.
  • Use a transaction script pattern to implement business rules on the mobile device.
  • Use presentation entities on the mobile device to manage data that is unique to the device.
  • Use a proxy on the mobile device to interact with the application server.
  • Use an entity translator to convert between domain entities and data contracts.
  • Use a message-based protocol for communication between the service and business layers on the application server.
  • Implement a Domain Entity pattern to represent business entities.
  • Interact with the database using Repository and Data Mapper patterns.
Mobile Device – 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 direct calls to transaction script components from the controller implemented in the presentation layer. Data structures returned from the transaction script interface are used by controls on the device when rendering the display.

Pattern Info Example
Front Controller
User Interface processing is divided into three separate roles. The three roles are Model, View, and Controller. The Model represents data, the View represents the user interface, and the Controller is responsible for processing requests.
The controller is composed of two sub-patterns; handler and command. A handler is responsible for capturing user input, such as control events, and passing control to a command object. Command objects perform specific tasks and then passes control to a view, which uses a model to render the display.
Objects that contain data, the model in MVC, can be passive or active. When model objects are passive the view can only be updated by user interaction. An Observer pattern can be used to implement active model objects that notify the view and/or the controller when changes occur
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 code that is rendered within the mobile device. Controls used to define the user interface are responsible for generating code that will display the control when rendered on the mobile device.
Mobile Device – Business Layer
Business layer components on the mobile device are used to implement some of the business rules from the application server on the mobile device for better performance. The main purpose for implementing rules on the mobile device is to provide immediate feedback to the user instead of waiting for interaction with the service to complete. However, any business rules implemented on the mobile device should also be implemented on the application server.

Pattern Info Example
Transaction Script
Command objects from the front controller use transaction script objects to interact with the business layer. Within the command object 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 can be started, one or more business operations are performed, and the transaction is committed or rolled-back based on the outcome of the business operations.
Mobile Device – 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
Repository
A repository provides an in-memory representation of entities used by the mobile device. As a general rule objects are normally not stored in memory with a mobile application. However, the repository hides all database interaction and provides operations that operate on 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.
Presentation Entity
Presentation entities are defined to represent data that will only be used on the mobile device. Similar to a business or domain entity the presentation entity contains data and defines relationships between other entities that will only be used on the mobile device.
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.

Application Server – Service Layer
Service layer components provide access to business logic in the application. Service consumers interact 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.

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.
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
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.
  • A WCF service reference is used to generate a proxy to the service layer.
  • Presentation entities are used to manage data that is unique to the mobile device.
  • 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 on the application server.
  • Business process objects are used to implement the transaction script pattern on both the mobile device and application server.
  • Repository objects are used to provide a data access interface on the mobile device and application server.
  • 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 mobile device controls to create a composite view.
  • Access data from a local data store on the mobile device.
  • Use WCF services to provide an interface between the mobile device and application server.
  • 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.
Mobile Device – Presentation Layer
In this scenario a Front Controller pattern is used to handle user interaction and process commands. Command objects used by the front controller pattern interact directly with business process objects in the business layer. Finally, controls associated with the development framework being used to develop the application are used to create and render the user interface.

Check & More Info Example
MVC Objects
Objects are used in the presentation layer 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.
UI Controls
You are using controls associated with the development package being used to create the mobile device application. There are several different frameworks that can be used to build mobile applications. Each of these frameworks provide controls that can be used to define the user interface.

Mobile Device – Business Layer
The business layer on a mobile device is used to bring some business rules closer to the user in order to improve performance. In addition, rules associated with managing data on the mobile device are also implemented in the business layer. Interaction with the business layer is implemented using business process objects that are called directly from command objects in the presentation layer. Business process objects interact with the data access layer using repository objects.

Check & More Info Example
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. Promote.PNG
Mobile Device – Data Access Layer
The primary interface into the data access layer is through repository objects, which provide an illusion that business entities are being maintained in memory. For example, business process objects can perform get, save, and update operations on business entities without having any knowledge of the underlying data store used to persist the objects. In this scenario the mobile data access layer is responsible for interacting with the application server using WCF services and a local data store using data access components provided by the mobile device framework you are using.

Check & More Info Example
Data Access Objects
Identify WCF message contracts and parent objects in your presentation entity data model and define a separate repository object for each one. The mobile device repository in this scenario will be used to access message contracts returned from the application server along with presentation entities on the mobile device. 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.
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. CriteriaInterface.PNG
Presentation Entities
Classes are defined that represent tables or views from the local data store. Preferences.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.
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)
In this scenario the mobile device communicates with an application server using WCF services. As a result, the application server implements a service layer that exposes WCF service and message contracts. In addition, the message contracts represent data transfer objects that are used to move data across boundaries as a single unit. Finally, the service layer is responsible for translating between business entities that are internal to the implementation and data contracts that are exposed through the service interface.

Check & More Info Example
WCF Service Contract
You are using WCF to define business operations called by the mobile device. 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. MessageContract1.PNG
Translator Objects
Classes are defined that provide operations to translate between business entities and WCF data contracts. BizEntity.PNG
Application Server – Business Layer
This scenario uses a message based façade design to provide an interface into the business layer from the service layer. The façade can be defined as a shared abstraction that uses a common class defined as a Message that acts as a container for data being sent to and returned from the business layer. The façade passes control to business process objects that know how to process the request and can manage transactions if necessary. Business process objects are responsible for interacting with the data access layer through repository objects.

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. BusinessFacade.PNG
The Façade acts as a boundary that catches all unhandled exceptions that may occur when processing business operations. EmployeeFacade.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. Promote.PNG

Application Server – Data Access Layer
The primary interface into the data access layer is through repository objects, which provide an illusion that business entities are being maintained in memory. For example, business process objects can perform get, save, and update operations on business entities without having any knowledge of the underlying data store used to persist the objects.

Check & More Info Example
Data Access Objects
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. AccountRepository.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. CriteriaInterface1.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
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 Feb 2, 2009 at 7:21 PM by prashantbansode, version 4

Comments

No comments yet.