ER Diagrams, Domain Model, and N-Layer Architecture with ASP.NET 3.5 (part1)

0
436
10 min read

Let us start with a 1-tier ASP.NET application configuration.

Note that the application as a whole including database and client browser is three tier.

ER Diagrams, Domain Model, and N-Layer Architecture with ASP.NET 3.5 (part1)

We can call this 1-tier architecture a 3-tier architecture if we include the browser and database (if used). For the rest of this article we will ignore the database and browser as separate tiers so that we can focus on how to divide the main ASP.NET application layers logically, using the n-layer pattern to its best use.

We will first try to separate the data access and logical code into their own separate layers and see how we can introduce flexibility and re-usability into our solution. We will understand this with a sample project. Before we go ahead into the technical details and code, we will first learn about two important terms: ER Diagram and Domain Model, and how they help us in getting a good understanding of the application we need to develop.

Entity-Relationship Diagram

Entity-Relationship diagrams, or ER diagrams in short, are graphical representations depicting relationships between different entities in a system. We humans understand and remember pictures or images more easily than textual information. When we first start to understand a project we need to see how different entities in the project relate to each other. ER diagrams help us achieve that goal by graphically describing the relationships.

An entity can be thought of as an object in a system that can be identified uniquely. An entity can have attributes; an attribute is simply a property we can associate with an entity. For example, a Car entity can have the following attributes: EngineCapacity, NumberofGears, SeatingCapacity, Mileage, and so on. So attributes are basically fields holding data to indentify an entity. Attributes cannot exist without an entity.

Let us understand ER diagrams in detail with a simple e-commerce example: a very basic Order Management System. We will be building a simple web based system to track customer’s orders, and manage customers and products.

To start with, let us list the basic entities for our simplified Order Management System (OMS):

  • Customer: A person who can place Orders to buy Products.
  • Order: An order placed by a Customer. There can be multiple Products bought by a Customer in one Order.
  • Product: A Product is an object that can be purchased by a Customer.
  • Category: Category of a Product. A Category can have multiple Products, and a Product can belong to many Categories. For example, a mixer-grinder can be under the Electronic Gadgets category as well as in Home Appliances.
  • OrderLineItem: An Order can be for multiple Products. Each individual Product in an order will be encapsulated by an OrderLineItem. So an Order can have multiple OrderLineItems.

Now, let us picture the relationship between the core business entities is defined using an Entity-Relationship diagram. Our ER diagram will show the relational associations between the entities from a database’s perspective. So it is more of a relational model and will not show any of the object-oriented associations (for which we will use the Domain Model in the later sections of this article). In an ER diagram, we show entities using rectangular boxes, the relationships between entities using diamond boxes and attributes using oval boxes, as shown below:

ER Diagrams, Domain Model, and N-Layer Architecture with ASP.NET 3.5 (part1)

The purpose of using such shapes is to make the ER diagram clear and concise, depicting the relational model as closely as possible without using long sentences or text. So the Customer entity with some of the basic attributes can be depicted in an ER diagram as follows:

ER Diagrams, Domain Model, and N-Layer Architecture with ASP.NET 3.5 (part1)

Now, let us create an ER diagram for our Order Management System. For the sake of simplicity, we will not list the attributes of the entities involved.

Here is how the ER diagram looks:

ER Diagrams, Domain Model, and N-Layer Architecture with ASP.NET 3.5 (part1)

The above ER diagram depicts the relationships between the OMS entities but is still incomplete as the relationships do not show how the entities are quantitatively related to each other. We will now look at how to quantify relationships using degree and cardinality.

Degree and Cardinality of a Relationship

The relationships in an ER diagram can also have a degree. A degree specifies the multiplicity of a relationship. In simpler terms, it refers to the number of entities involved in a relationship. All relationships in an OMS ER diagram have a degree of two, also called binary relationships. For example, in Customer-Order relationships only two entities are involved—Customer and Order; so it’s a two degree relationship. Most relationships you come across would be binary.

Another term associated with a relationship is cardinality. The cardinality of a relationship identifies the number of instances of entities involved in that particular relationship. For example, an Order can have multiple OrderLineItems, which means the cardinality of the relationship between Order and OrderLineItem is one-to-many. The three commonly-used cardinalities of a relationship are:

  • One-to-one: Depicted as 1:1
    Example: One OrderLineItem can have only one Product; so the OrderLineItem and Product entities share a one-to-one relationship
  • One-to-many: Depicted as 1:n
    Example: One customer can place multiple orders, so the Customer and Order entities share a one-to-many relationship
  • Many-to-many: Depicted as n:m
    Example: One Product can be included in multiple Categories and one Category can contain multiple Products; therefore the Product and Category entities share a many-to-many relationship

After adding the cardinality of the relationships to our ER diagram, here is how it will look:

ER Diagrams, Domain Model, and N-Layer Architecture with ASP.NET 3.5 (part1)

This basic ER diagrams tells us a lot about how the different entities in the system are related to each other, and can help new programmers to quickly understand the logic and the relationships of the system they are working on. Each entity will be a unique table in the database.

OMS Project using 2-Layer

We know that the default coding style in ASP.NET 2.0 already supports the 1-tier 1-layer style, with two sub-layers in the main UI layer as follows:

  • Designer code files: ASPX markup files
  • Code behind files: Files containing C# or VB.NET code

Because both of these layers contain the UI code, we can include them as a part of the UI layer. These two layers help us to separate the markup and the code from each other. However, it is still not advisable to have logical code, such as data access or business logic, directly in these code-behind files.

Now, one way to create an ASP.NET web application for our Order Management System (OMS) in just one layer is by using a DataSet (or DataReader) to fill the front-end UI elements directly in the code-behind classes. This will involve writing data access code in the UI layer (code-behind), and will tightly bind this UI layer with the data access logic, making the application rigid (inflexible), harder to maintain, and less scalable.

In order to have greater flexibility, and to keep the UI layer completely independent of the data access and business logic code, we need to put these elements in separate files. So we will now try and introduce some loose-coupling by following a 2-layer approach this time. What we will do is, write all data access code in separate class files instead of using the code-behind files of the UI layer. This will make the UI layer independent of the data-access code.

We are assuming that we do not have any specific business logic code at this point, or else we would have put that under another layer with its own namespace, making it a 3-layered architecture. We will examine this in the upcoming sections of this article.

Sample Project

Let us see how we can move from this 1-tier 1-layer style to a 1-tier 2-layer style. Using the ER diagram above as reference, we can create a 2-Layer architecture for our OMS with these layers:

  • UI-layer with ASPX and code-behind classes
  • Data access classes under a different namespace but in the same project

So let’s start with a new VS 2008 project. We will create a new ASP.NET Web Project in C#, and add a new web form, ProductList.aspx, which will simply display a list of all the products using a Repeater control. The purpose of this project is to show how we can logically break up the UI layer further by separating the data access code into another class file.

The following is the ASPX markup of the ProductList page (unnecessary elements and tags have been removed to keep things simple):

<asp:Repeater ID="prodRepeater" runat="server">
<ItemTemplate>
Product Code: <%# Eval("Code")%>
<br>
Name: <%# Eval("Name")%>
<br>
Unit Price: $<%# Eval("UnitPrice")%>
<br>
</ItemTemplate>
</asp:Repeater>

In this ASPX file, we only have a Repeater control, which we will bind with the data in the code-behind file.

Here is the code in the ProductList.aspx.cs code-behind file:

namespace OMS
{
public partial class _Default : System.Web.UI.Page
{
/// <summary>
/// Page Load method
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = DAL.GetAllProducts();
prodRepeater.DataSource = dt;
prodRepeater.DataBind();
}
}//end class
}//end namespace

Note that we don’t have any data access code in the code-behind sample above. We are just calling the GetAllProducts() method, which has all of data access code wrapped in a different class named DAL. We can logically separate out the code, by using different namespaces to achieve code re-use and greater architectural flexibility. So we created a new class named DAL under a different namespace from the UI layer code files. Here is the DAL code:

namespace OMS.Code
{
public class DAL
{
/// <summary>
/// Load all comments from the Access DB
/// </summary>
public static DataTable GetAllProducts()
{
string sCon = ConfigurationManager.ConnectionStrings[0].ConnectionString;
using (SqlConnection cn = new SqlConnection(sCon))
{
string sQuery = @"SELECT * FROM OMS_Product";
SqlCommand cmd = new SqlCommand(sQuery, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
cn.Open();
da.Fill(ds);
return ds.Tables[0];
}
}
}//end class
}//end namespace

So we have separated the data access code in a new logical layer, using a separate namespace, OMS.Code, and using a new class. Now, if we want to, we can re-use the same code in the other pages as well. Furthermore, methods to add and edit a product can be defined in this class and then used in the UI layer. This allows multiple developers to work on the DAL and UI layers simultaneously.

Even though we have a logical separation of the code in this 2-layer sample architecture, we are still not using real Object Oriented Programming (OOP). All of the Object-Oriented Programming we have used so far has been the default structure the .NET framework has provided, such as the Page class, and so on.

When a project grows big in size as well as complexity, using the 2-layer model discussed above can become cumbersome and cause scalability and flexibility issues. If the project grows in complexity, then we will be putting all of the business logic code in either the DAL or the UI layer. This business logic code includes business rules. For example, if the customer orders a certain number of products in one order, he gets a certain level of discount. If we code such business rules in the UI layer, then if the rules change we need to change the UI as well, which is not ideal, especially in cases where we can have multiple UIs for the same code, for example one normal web browser UI and another mobile-based UI.

We also cannot put business logic code in the DAL layer because the DAL layer should only contain data access code which should not be mixed with any kind of business processing logic. In fact the DAL layer should be quite “dumb”–there should be no “logic” inside it because it is mostly a utility layer which only needs to put data in and pull data out from a data store.

To make our applications more scalable and to reap the benefit of OOP, we need to create objects, and wrap business behavior in their methods. This is where the Domain Model comes into the picture.


Subscribe to the weekly Packt Hub newsletter

* indicates required

LEAVE A REPLY

Please enter your comment!
Please enter your name here