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

App Pattern: Three-Tier Web Application Scenario (Domain Entity)

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

Applies To

  • ASP.NET 3.5
  • Windows Server 2003, 2008

Scenario

In this scenario, you have full control over the creation and management of the database schema. In addition, the application will be deployed and accessed from within a local area network, which means that the web server does not need to be deployed to an isolated perimeter network. 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

  • Stand-alone ASP.NET Web application that supports complex data models.
  • Presentation and Business logic are located on the same physical machine.
  • 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 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 (2).PNG

The following is a summary of the patterns used by this scenario:
  • User Interface processing is handled by a Supervising Controller pattern.
  • The Template View pattern is used to define a common look and feel.
  • Controls are bound to objects that contain data.
  • The business layer uses a Façade pattern to implement a message-based interface between the presentation and business layer.
  • 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.
Dependency Inversion Use a base class or interface to define a shared abstraction that can be used by multiple layers in the design. The principal behind dependency inversion is that high level components should not be dependent on low level components. Instead, both should depend on an external abstraction.
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.
Façade Implement a unified interface to a set of operations to reduce coupling between systems.
Repository An in-memory representation of a data source that works with domain entities.
Supervising Controller Separates the user interface code into three separate units; Model (data), View (interface), and Presenter (processing logic), with a focus on the view.
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 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-Presenter (MVP) pattern to handle user interaction.
  • Use an ASP.NET Master page to define a common template for all pages.
  • 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.
Web Server – Presentation Layer
The presentation layer is responsible for accepting user input and rendering the user interface that is returned from the web server.

Check & More Info Example
Supervising Controller
User Interface processing is divided into three separate roles. The three roles are Model, View, and Presenter. The Model represents data, the View represents the user interface, and the Presenter is responsible for processing requests.
The web page handles requests and passes them off to a controller Requests are sent to the View (web page), which then passes control to a provider that is responsible for initializing the Model, returning control back to the View, or passing control on to a different View.
Template View
An ASP.NET Master page is used to provide a common look and feel. Common elements such as background, page layout, menus, header, and footer are defined in the master page.
ASP.NET pages focus on content that is specific to each page Each page is associated with the master page, which renders the common content. As a result, the page only needs to contain user interface elements that are not common across all pages.
Bound Data Control
ASP.NET Server and User controls are bound to business entities returned from the business layer. Business entities returned from the business layer can be bound to web controls, which will use data from the entity when rendering the display.
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.

Check & More 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 façade provides a message-based interface between the presentation layer and business layer. Operations from a web application or service are normally stateless, which means that the system does not maintain state information between requests. As a result, the best way to handle operations are to treat each one as a separate message. This can be accomplished by using a façade that accepts message requests and returns a message response.
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.

Check & More 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:
  • Internet explorer is the target browser for this application scenario.
  • The Microsoft patterns & practices MVP Bundle is used to implement the Model-View-Presenter pattern.
  • ASP.NET Master pages are used to define a Template View.
  • ASP.NET Page, Server, and User controls are used to define the user interface.
  • A common message-based 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 the Microsoft patterns & practices MVP Bundle is used to implement the Model-View-Presenter pattern.
  • Use and ASP.NET Master page to define a common look and feel for all pages on the site.
  • Use ASP.NET page, user, and server controls to create a composite view.
  • Host the web application 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 and map those entities to the database.
  • Access tables, views, or stored procedures 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 – Presentation Layer
Check & More Info Example
patterns & practices MVP
Use the Model View Presenter (MVP) Bundle that is provided by Microsoft patterns & practices. The MVP Bundle is a sub-set of code, binaries, and documentation from the Composite Web Application Block, which is also provided by Microsoft patterns & practices.
Events in the view, ASP.NET Page, are passed onto the presenter, which is responsible for processing actions from the view. PageLoad.PNG
The presenter class from the MVP Bundle is responsible for interacting with a controller, which then interacts with the business layer. Examples from the MVP Bundle use operations in the presenter that are associated with events from the view to perform different actions. ViewLoad.PNG
ASP.NET Master Page
An ASP.NET Master Page is used to define a common layout. A common use of the Master Page is to define the layout for all pages of the application. This approach provides a consistent look and feel across the web site.
Menus are defined in the Master Page. Instead of using User controls for common menus you can define them directly in the Master Page if navigation is fairly static. For dynamic menus you can add place holders in the Master Page, generate appropriate menus, and then add them to the place holders during page initialization.
ASP.NET Page, User and Server Controls
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 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.
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 message container as parameters and returns the message container.
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 process objects can also be used to initialize a common Entity Framework data context that is used by repository objects when interacting with the data access layer. This approach allows you to maintain context with one object while processing the request. This will ensure that all updates to business entities are saved when the context is saved. There are several ways to manage the data context; you can use either the CallContext or HttpContext to hold an instance of the data context, or you can initialize the data context and pass it to repository operations. IMPORTANT! You must dispose the data context object before returning control back to the façade.
Web Server – Data Access Layer
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. 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, or you can create the model manually. The ADP.NET Entity Framework also provides an EDM visual designer that you can use to manage the EDM.
Consider using domain modeling to define the entity data model. Before creating the Entity Data Model (EDM) you should first analyze the business domain in order to identify domain entities and the relationship between those entities.
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 2:08 AM by rboucher, version 5

Comments

ronaldwidha Apr 10, 2009 at 2:17 PM 
I've got a simple example to show how I implement the architecture mentioned above (Model View Presenter/Supervising Controller) here: http://ronaldwidha.net/askbobo/articles/asp-net-mvc-without-using-mvc-framework/

colinjack Jan 22, 2009 at 10:23 PM 
I'm really surprised that this is being given out of guidance. You could sum it up as Service + procedural code + database and it doesn't seem that different from the advice MS gave out years ago, advice that even then was quite dated compared to what Fowler/Evans and many others were discussing.

One particular thing, your terminology changes from the original diagram to the technical solution

Domain Entities -> Business Entities
Repositories -> Data Access Objects

I'm wondering if one of the diagrams is older than the other?

Also I've left my comments some on http://www.codeplex.com/AppArch/Wiki/View.aspx?title=App%20Pattern%20-%20Two-Tier%20Service%20Application%20Scenario%20(REST)&referringTitle=Application%20Patterns and some apply to this article too.

temebele Jan 22, 2009 at 5:31 PM 
I like how you laid out the different components.

Let us assume that we are using ADO.NET Entity Framework with this approach.

EF divides the Data Access Layer into Business Entities and Data Access Components logically with name spaces.
So Business Entities now live under the data layer. Since we are using those same business entities for data binding in our presentation layer, presentation layer is going to have direct dependency on the data layer. So from what I see, presentation layer is going to have dependency in both the business layer and the data layer. How do you see that from basic layering concepts?


Thanks,
T.