Page 1 of 8
How to
Introduction
In this article we will learn to implement 3- Tier architecture in C#.NET application. 3-Tier architecture is
very famous and well known buzz word in world of software development. If we analyze any traditional
project then we will find that most of (at least 60-70 %) them has traditional N-Tier, basically 3-Tier
architecture. It does not matter whether it is web or windows application, we can implement 3-Tier
architecture in any type of environment.
Although it is very well known and common architecture but its not very clear to fresher developer or
those who are very new in project development.
http://www.codeproject.com/Tips/662107/Understand-Tier-Architecture-in-Csharp?displa...
7/14/2014
Page 2 of 8
It is one confusing question for beginner. Few think that both are same. But they are not same. Tier
represents different hardware box. Means the components are physically separated to different machines.
But in case of layer all component remains in same system.
Business Layer
This is on top of presentation layer. As the name suggest, most of the business operation performs here.
For example, after collecting form data we want to validate them with our custom business rule. Basically
we define classes and business entities in this layer.
Lets Implement
Before start with example one more question need to be clear. How we will pass data from one layer to
another layer? Means in which form data from pass?
There are many solutions for this problem. For our example we will pass data through function parameter.
In this example we will implement one small windows application to fetch data from database using
3-Tier architecture. In this example we will read data from single "Person" Table.
http://www.codeproject.com/Tips/662107/Understand-Tier-Architecture-in-Csharp?displa...
7/14/2014
Page 3 of 8
Lets start from Data access layer; we will create function to read data from database. Have a look on
below code.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
namespace WindowsFormsApplication1.DAL
{
public class PersonDAL
{
public string ConString =
"Data Source=SOURAV-PC\\SQL_INSTANCE;Initial Catalog=test;Integrated Security=True";
SqlConnection con = new SqlConnection();
DataTable dt = new DataTable();
public DataTable Read()
{
con.ConnectionString = ConString;
if (ConnectionState.Closed == con.State)
con.Open();
SqlCommand cmd = new SqlCommand("select * from Person",con);
try
{
SqlDataReader rd = cmd.ExecuteReader();
dt.Load(rd);
return dt;
}
catch
{
throw;
}
}
public DataTable Read(Int16 Id)
{
con.ConnectionString = ConString;
if (ConnectionState.Closed == con.State)
con.Open();
SqlCommand cmd = new SqlCommand("select * from Person where ID= "+ Id +"", con);
try
{
SqlDataReader rd = cmd.ExecuteReader();
dt.Load(rd);
return dt;
}
catch
{
throw;
}
}
}
}
We have created two overloaded function to read data. One function will not take any argument and
other function will fetch data using ID.
http://www.codeproject.com/Tips/662107/Understand-Tier-Architecture-in-Csharp?displa...
7/14/2014
Page 4 of 8
http://www.codeproject.com/Tips/662107/Understand-Tier-Architecture-in-Csharp?displa...
7/14/2014
Page 5 of 8
The form contains one datagrid one textbox and one button. In load event of form we will pull all data
and it will show in datagrid. There is another operation, where user can able to fetch particular person by
providing person ID. Have a look on below code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApplication1.BLL;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
PersonBLL p = new PersonBLL();
this.dataGridView1.DataSource = p.GetPersons(Convert.ToInt16(this.txtID.Text));
}
catch
{
MessageBox.Show("Error Occurred");
}
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
PersonBLL p = new PersonBLL();
this.dataGridView1.DataSource = p.GetPersons();
}
catch
{
MessageBox.Show("Error Occurred");
}
}
}
}
http://www.codeproject.com/Tips/662107/Understand-Tier-Architecture-in-Csharp?displa...
7/14/2014
Page 6 of 8
http://www.codeproject.com/Tips/662107/Understand-Tier-Architecture-in-Csharp?displa...
7/14/2014
Page 7 of 8
Table Stricture
Here is our table structure. It contains three fields ID, name and surname.
Table Name:-Person
Conclusion
In this article we have discussed how to implement simple 3-tier architecture in C# application. Hope you
have understood the concept.
License
This article, along with any associated source code and files, is licensed under The Code Project Open
License (CPOL)
http://www.codeproject.com/Tips/662107/Understand-Tier-Architecture-in-Csharp?displa...
7/14/2014
Page 8 of 8
I am software developer from INDIA, working in manufacturing domain. Beside my day to day
development work, i like to learn new technology and update myself. I am passionate blogger
and author in various technical community including dotnetfunda.com , c-sharpcorner.com and
codeproject. My area of interest is modern web technology in Microsoft stack.
Follow on
http://www.codeproject.com/Tips/662107/Understand-Tier-Architecture-in-Csharp?displa...
7/14/2014