2 Tier Vs 3 Tier Architecture

Abstract

This article i will describe you to migrate your application from 2-Tier architecture to the most advance Data Access methodology available in .Net 4.0.Please keep watching all migration articles one by one. If it really helps you out then please don’t forget to put your feedback and comments.

2 Tier Architecture

If you are writing all the data access logic inside your code behind, I guess your code looks like this.

  • While Inserting Data into Database:
    SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[“EmployeeConString”].ConnectionString);

SqlCommand sqlCommand = new SqlCommand(string.Format(“INSERT INTO Department (DepartmentId, Name, Budget, StartDate) VALUES ({0}, ‘{1}’, {2}, ‘{3}’)”, txtDeptId.Text, txtDeptName.Text, txtBudget.Text, txtStartDate.Text), sqlConnection);

sqlConnection.Open();
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();

  • While Binding Data to ComboBox /DropDownList:SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[“EmployeeConString”].ConnectionString);
    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(“SELECT DepartmentId, Name FROM Department”, sqlConnection);DataTable deptList = new DataTable(“Department”);
    sqlDataAdapter.Fill(deptList);

cmbDepartment.DataSource = deptList;
cmbDepartment.DisplayMember = “Name”;
cmbDepartment.ValueMember = “DepartmentId”;

OR

  • While Binding Data to DataGridView:SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[“EmployeeConString”].ConnectionString);
    SqlCommand sqlCommand = new SqlCommand(“SELECT DepartmentId, Name FROM Department”, sqlConnection);sqlConnection.Open();
    SqlDataReader dataReader = sqlCommand.ExecuteReader();

while (dataReader.Read())
cmbDepartment.Items.Add(dataReader[“Name”].ToString());

dataReader.Close();

  • While Binding Data to ListView:SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[“EmployeeConString”].ConnectionString);
    SqlCommand sqlCommand = new SqlCommand(“SELECT DepartmentId, Name, Budget, StartDate FROM Department”, sqlConnection);sqlConnection.Open();
    SqlDataReader dataReader = sqlCommand.ExecuteReader();

while (dataReader.Read())
{
ListViewItem item = new ListViewItem(dataReader[“DepartmentId”].ToString());
item.SubItems.Add(dataReader[“Name”].ToString());
item.SubItems.Add(dataReader[“Budget”].ToString());
item.SubItems.Add(dataReader[“StartDate”].ToString());

lvwDepartment.Items.Add(item);
}

dataReader.Close();

So, all your data access code is being meshed up within the code behind and these practices were very old compared to the new advanced design patterns.

Let’s do this application in a 3-Tier approach. Although the 3-Tier approach is obsolete, still it is the base for any advanced design pattern. So it’s very much essential to learn and know the pattern. Let’s see the fundamental diagram of a 3-Tier architecture.
Basics of 3-tier Architecture:

Basic 3-Tier architecture

ado.net1.gif

Figure 1:Basic 3-Tier architecture

The above describes a very simple architecture of a 3-tier model.

  • DAL (Data Access Layer) interacts with Database directly, so all the SQL operation are being done within DAL only.
  • BLL (Business Logic Layer) works like a mediator between DAL and the Presentation Tier.
  • No direct communication is allowed between DAL and Presentation Layer.
  • Although there is no physical presence of the Entity Layer, but Entity encapsulates all the information/data and passes it from one layer to the other.
  • So all the Database object name and the Database Schema is being restricted inside the DAL which gives an extra security layer to the application.
  • As Business rules/logics are being defined inside BLL, any update to business logic will not impact the DAL and the presentation layer.ado.net2.gif
    Figure 2: Complex 3-Tier Architecture

This diagram describes an actual implementation of a 3-tier model.

  • Data Access Service and the Database Server can be hosted in single Server.
  • Mostly SQL Server 2000/2005/2008 or Oracle can be hosted on Windows 2000/2003 Server.
  • Business Server exposes all the operation through Web Service /Remoting/WCF.
  • A highly configured server with Windows 2000/2003 can be used to host the Business Service, or else Microsoft BizTalk Server also can be used for this.
  • Presentation Tier or the Client Consumes the Service exposed at Business Server by using the Proxy through http:// pipeline.
  • Client can be any standalone machine with the application is being installed in case of Desktop Application (Win-Form or Console Application), or having a Browser to run Web Application.
  • Data/Information are being encapsulated by entity and transferred from one location to another over network followed by all the network protocol.

  You might be thinking “During this new era of Data Access Technology what is the need of learning a Basic 3-Tier Application development process?” Yes you are correct, but before implementing any new .Net Design Patterns like LINQ, Entity Model, MVC, MVP or MVVM we need to understand the basics of 3-Tier Application.

Basic Rules and Concept of 3-Tier Application

  1. There will be clear separation of layers. such as Presentation Layer, Business Logic Layer, Data Access Layer and Entity Layer.
  2. This kind of separation of layers will give the facility to host Business Logic in Business Server and Data Access in Database Server.
  3. Data Encapsulation done by Entity Layer which can serialized across the layer, from Presentation to Business and Business to Data Access and vice versa.
  4. Business Logic will encapsulated in Business Logic layer.
  5. Data Access process and Security will be encapsulated in Data Access Layer.
  6. Finally the Presentation Layer will access only the Business Logic Layer to Add a new record, Modify an existing record, to View records by list or a particular Record.
  7. Presentation Layer will never communicate directly with Database Object or Data Access Layer.

This Article will also guide you the Path way to develop a 3-Tier Application with some simple Steps.

Step – 1

Add 4 Projects to an empty Visual Studio Solution. Of of these, 3 of the projects should be a Class Library (For Class Library the output will be a dll) and the other one can be your application (Windows Form Application or Web Application or Web site). You should follow a better naming convention for the projects. For example you can have your projects in your Solution like this.

Projects Application Type of Project
Entity
ApplicationName.Entity
Class Library
DAL
ApplicationName.DAL Class Library
BLL
ApplicationName. Class Library
Application
Application Win/Web Application/Site


So now the Solution Explorer will looks like this.

solution_explorer.png

Step – 2
Now add Entity classes to the Entity Project. A simple Entity Class will look like this.
entity.png

Step – 3

Add a reference of Entity to the DAL Project, so we can access the Entity Classes within the DAL. Inside a DAL class you can write methods to access and to manipulate data from a Database. A simple DAL class can have all the following methods:

dal_members.png

In the Attached Sample code you can find the code for DAL, but for understanding purposes see the Upsert method bellow, which insert/Update a record in Database. (To know more about Upsert Method Read my previous Article How to: Implement UPSERT () Method in C#).
dal_upsert.png

And the Select Method will look like this.
dal_select.png

Step – 4

Now add Classes to BLL for each Entity and you can have the methods list like this. Before that do not forget to add reference from Entity and DAL.

bll_members.png

Step – 5
So you are done with the Entity Layer, Data Access Layer and Business Logic Layer. Now you can add a very good look and feel and user friendly UI as the Presentation Layer. To access the Entity and Business Logic layer add references from both the projects.

  • In your Save button Click event you can create an object of BLL and can call the Add() method of it.

add_new.png

  • To modify an existing record you can call the Edit() Method of BLL.

modify.png

  • And to bind data to a GridView you can call GetList() Method.

bind_grid.png

So cheer up… now your old 2-Tier Application is ready with a new Flavor of 3-Tier Architecture.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s