Training on C# Programming
3- Tier Architecture
3-Tier architecture is very famous and well known word in software development industry. 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.
Why 3-Tier Architecture?
If we separate our codes by layer then changes in one layer will not affect much to another one. Tomorrow if
business demands, we can change UI very quickly by using existing code.
Difference between Tier and Layer
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.
What are the Layers?
Theoretically it is N-Tier architecture. So, we can create as much layer as possible but basically people classify code
in three different categories and put them in three different layers.
1.
Presentation Layer/ UI Layer
This is the top most layer of application where user performs their activity. Lets take example of any
application where user needs to fill up one form. This form is nothing nut presentation layer. In windows
application windows form is presentation layer and in web application web form belongs to presentation
layer.
2.
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.
3.
Data Access Layer
It is on top of Business Logic Layer. It contains methods that helps business layer to connect with database
and perform CRUD operation. In data access layer generally all database related code and stuff belongs
to. Sometimes people use platform independent data access layer to fetch data from different database
vendor.
Training on C# Programming
Database Connection
We always use Connection String in forms that we design but when you design a big software you should set the
connection string for once and use it in many forms. In this method you use the name of the Connection String
instead of the connection string text.
Every time you want to change the connection string just change the main connection string in the App.Config file.
By this method you don't need to change all of the forms in your project, just change the Connection String in the
App.Config.
1.
set the connection string in the App.Config file. Example,
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name=" DBConn"
connectionString="Data Source=.;Initial Catalog= DBConn;Integrated
Security=True"/>
</connectionStrings>
</configuration>
2.
Create Public Class for Database Connections
using
using
using
using
System;
System.Configuration;
System.Data;
System.Data.SqlClient;
namespace Data
{
public class DbConnections
{
public SqlCommand Command;
public SqlConnection Connection;
// Get Connection Function
private SqlConnection GetConnection ()
{
string connect =
ConfigurationManager.ConnectionStrings["DBConn"].ToString();
Connection = new SqlConnection(connect);
return Connection;
}
//Get Dataset Function
public DataSet GetDataset (string strSQL)
Training on C# Programming
{
DataSet functuonReturnValue;
var ds = new DataSet();
try
{
Connection = GetConnection();
Command = Connection.CreateCommand();
Command.CommandText = strSQL;
Command.CommandType = CommandType.Text;
var da = new SqlDataAdapter(Command);
if ( Connection.State == ConnectionState.Closed ) Connection.Open();
da.Fill(ds);
functuonReturnValue = ds;
}
catch ( Exception ex )
{
throw new Exception(ex.Message);
}
return functuonReturnValue;
}
// Get Datatable Function
public DataTable GetDataTable (string strSQL)
{
using ( DataTable dt = GetDataset(strSQL).Tables[0] )
{
return dt;
}
}
// Get Single Data Row Function
public DataRow GetSingleDataRow (string strSQL)
{
DataRow dr = GetDataTable(strSQL).Rows[0];
return dr;
}
// Get Single Field Function
public string GetSingleField (string strSQL)
{
string str;
try
{
str = GetSingleDataRow(strSQL)[0].ToString();
}
catch ( Exception )
{
str = "";
}
return str;
}
// Execute Query Function
public void ExecuteQuery (string strSQL)
{
try
{
Connection = GetConnection();
Training on C# Programming
Command = Connection.CreateCommand();
Command.CommandText = strSQL;
Command.CommandType = CommandType.Text;
if ( Connection.State == ConnectionState.Closed )
Connection.Open();
Command.ExecuteNonQuery();
}
catch ( Exception ex )
{
throw new Exception(ex.Message);
}
finally
{
if ( Connection != null )
Connection.Close();
}
}
// Command Parameters
public void CommandParameter (string strSQL)
{
try
{
Connection = GetConnection();
Command = Connection.CreateCommand();
Command.CommandText = strSQL;
}
catch ( Exception ex )
{
throw new Exception(ex.Message);
}
}
// Execute Reader Function
public SqlDataReader ExecutiveReaderQuery (string strSQL)
{
try
{
Connection = GetConnection();
Command = Connection.CreateCommand();
Command.CommandText = strSQL;
Command.CommandText = strSQL;
Command.CommandType = CommandType.Text;
if ( Connection.State == ConnectionState.Closed )
Connection.Open();
SqlDataReader reader = Command.ExecuteReader();
return reader;
}
catch ( Exception ex )
{
throw new Exception(ex.Message);
}
}
// Execute Scaler Function
Training on C# Programming
public object ExecutiveScaler (string strSQL)
{
try
{
Connection = GetConnection();
Command = Connection.CreateCommand();
Command.CommandText = strSQL;
Command.CommandText = strSQL;
Command.CommandType = CommandType.Text;
if ( Connection.State == ConnectionState.Closed )
Connection.Open();
object reader = Command.ExecuteScalar();
return reader;
}
catch ( Exception ex )
{
throw new Exception(ex.Message);
}
}
}
}