Read my other blogs, C# 6.0 new features, ASP.Net Tutorial and .Net Core Tutorial.
In this blog, we will see 3-tier architecture to develop an application in C#.
In this blog, we will see 3-tier architecture to develop an application in C#.
What is 3-Tier Architecture?
3-Tier architecture is well known in software development for web and windows software.
3-Tier architecture contains UI (Presentation) Layer, Business Logic and Data Access Layer.
In below structure, you can see-
Program.cs is Presentation Layer (i.e. for User Input)
BAL.cs – Business Logic, BO.cs – Business Entity
DAL.cs – Data Access Layer
Lets see code one by one for 3 tier architecture.
Program.cs
namespace Practice
{
class Program
{
static void Main(string[] args)
{
Program objP = new Program();
objP.InsertRecordFunction();
objP.PrintData();
}
private void PrintData()
{
Practice.BAL objBusinessLayer = new BAL();
objBusinessLayer.GetEmployeeRecord();
}
private void InsertRecordFunction()
{
BO objBusinessEntity = new BO();
Console.WriteLine(“Enter Login”);
objBusinessEntity.Login = Console.ReadLine();
Console.WriteLine(“Enter Employee BirthDay”);
objBusinessEntity.DOB = Console.ReadLine();
Console.WriteLine(“Enter Date of Joining”);
objBusinessEntity.DOJ = Console.ReadLine();
Console.WriteLine(“Enter Date of Retirement”);
objBusinessEntity.DOR = Console.ReadLine();
Practice.BAL objBusinessLay = new BAL();
bool result = objBusinessLay.InsertEmployeeRecord(objBusinessEntity);
if (result)
{
Console.WriteLine(“Record Inserted”);
}
else
{
Console.WriteLine(“There is an error. Please try later”);
}
}
}
}
BO.cs
namespace Practice
{
public class BO
{
private string _Login;
private string _DOB;
private string _DOJ;
private string _DOR;
public string Login
{
get
{
return _Login;
}
set
{
_Login = value;
}
}
public string DOB
{
get
{
return _DOB;
}
set
{
_DOB = value;
}
}
public string DOJ
{
get
{
return _DOJ;
}
set
{
_DOJ = value;
}
}
public string DOR
{
get
{
return _DOR;
}
set
{
_DOR = value;
}
}
}
}
BAL.cs
namespace Practice
{
public class BAL
{
Practice.DAL objDataLayer = new DAL();
public bool InsertEmployeeRecord(BO objBO)
{
bool result = objDataLayer.InsertRecord(objBO);
if (result)
{
return true;
}
else
{
return false;
}
}
public void GetEmployeeRecord()
{
objDataLayer.GetRecordFunction();
}
}
}
DAL.cs
namespace Practice
{
public class DAL
{
public bool InsertRecord(BO objBusinessEntity)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[GlobalConstant.DBCONNECTION].ConnectionString);
try
{
string commandQuery = GlobalConstant.INSERT_SP;
SqlCommand cmd = new SqlCommand(commandQuery, con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(“@login”, objBusinessEntity.Login);
cmd.Parameters.AddWithValue(“@dob”, objBusinessEntity.DOB);
cmd.Parameters.AddWithValue(“@doj”, objBusinessEntity.DOJ);
cmd.Parameters.AddWithValue(“@dor”, objBusinessEntity.DOR);
con.Open();
cmd.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
finally
{
con.Close();
}
}
public void GetRecordFunction()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[GlobalConstant.DBCONNECTION].ConnectionString);
try
{
string cmdQry = GlobalConstant.GET_SP;
SqlCommand cmd = new SqlCommand(cmdQry, con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
con.Open();
SqlDataReader sqlreader = cmd.ExecuteReader();
Console.WriteLine(
string.Format(“|{0}| {1}| {2} |{3}| {4} |”,
AlignCentre(“ID”, 12),
AlignCentre(“Login”, 12),
AlignCentre(“DOB”, 12),
AlignCentre(“DOJ”, 12),
AlignCentre(“DOR”, 12)));
while (sqlreader.Read())
{
PrintRow(Convert.ToString(sqlreader[“id”]), Convert.ToString(sqlreader[“login”]), Convert.ToString(sqlreader[“bday”]), Convert.ToString(sqlreader[“doj”]), Convert.ToString(sqlreader[“dor”]));
}
}
catch
{
}
finally
{
con.Close();
}
}
static void PrintRow(string id, string login, string dob, string doj, string dor)
{
Console.WriteLine(
string.Format(“|{0}| {1}| {2} |{3}| {4} |”,
AlignCentre(id, 12),
AlignCentre(login, 12),
AlignCentre(dob, 12),
AlignCentre(doj, 12),
AlignCentre(dor, 12)));
}
static string AlignCentre(string text, int width)
{
if (string.IsNullOrEmpty(text))
{
return new string(‘ ‘, width);
}
else
{
return text.PadRight(width – (width – text.Length) / 2).PadLeft(width);
}
}
}
}
GlobalConstant.cs
namespace Practice
{
public class GlobalConstant
{
public const string DBCONNECTION = “practice”;
public const string INSERT_SP = “insert_record_isp”;
public const string DELETE_SP = “”;
public const string GET_SP = “get_record_ssp”;
}
}
Keep Following – SharePointCafe.net