Introduction:
3-Tier architecture is a very well know buzz word in the world of
software development whether it web based or desktop based. In this
article I am going to show how to design a web application based on
3-tier architecture.
Presentation Layer (UI)
Presentation layer cotains pages like .aspx or windows form where data is presented to the user or input is taken from the user.
Business Access Layer (BAL) or Business Logic Layer
BAL contains business logic, validations or calculations related
with the data, if needed. I will call it Business Access Layer in my
demo.
Data Access Layer (DAL)
DAL contains methods that helps business layer to connect the data
and perform required action, might be returning data or manipulating
data (insert, update, delete etc). For this demo application, I have
taken a very simple example. I am assuming that I have to play with
record of persons (FirstName, LastName, Age) and I will refer only these
data through out this article.
Data Access Layer (DAL):
Connection:
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; ////// Summary description for Createconnection /// public class Createconnection { public Createconnection() { // // TODO: Add constructor logic here // } public static SqlConnection open_connection() { SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString); cn.Open(); return cn; } public static void close_connection() { SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString); cn.Close(); cn.Dispose(); } }
Command:
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; ////// Summary description for Createcommand /// public class Createcommand { public Createcommand() { // // TODO: Add constructor logic here // } public static void exenonquery(SqlCommand cmd) { try { SqlConnection cn = Createconnection.open_connection(); cmd.Connection = cn; cmd.CommandType = CommandType.StoredProcedure; cmd.ExecuteNonQuery(); } finally { Createconnection.close_connection(); } } public static DataTable execquery(SqlCommand cmd) { SqlConnection cn = Createconnection.open_connection(); cmd.Connection = cn; cmd.CommandType = CommandType.StoredProcedure; DataTable dt = new DataTable(); SqlDataAdapter dap = new SqlDataAdapter(cmd); dap.Fill(dt); return dt; } }
Parameter:
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; ////// Summary description for Createparameter /// public class Createparameter { public Createparameter() { // // TODO: Add constructor logic here // } public SqlParameter intparameter(string pname, string pvalue) { SqlParameter para = new SqlParameter(); para.DbType = DbType.Int32; para.Direction = ParameterDirection.Input; para.ParameterName = pname; para.Value = pvalue; return para; } public SqlParameter stringparameter(string pname, string pvalue) { SqlParameter para = new SqlParameter(); para.DbType = DbType.String; para.Direction = ParameterDirection.Input; para.ParameterName = pname; para.Value = pvalue; return para; } }
Business Access Layer (BAL):
InsertLogic:
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; ////// Summary description for insertlogic /// public class insertlogic { public insertlogic() { // // TODO: Add constructor logic here // } public static void insert_user_tb(user_property up) { SqlCommand cmd = new SqlCommand(); cmd.CommandText = "insert_user_tb_sp"; Createparameter para = new Createparameter(); cmd.Parameters.Add(para.stringparameter("@name", up.name)); cmd.Parameters.Add(para.stringparameter("@mobile", up.mobile)); cmd.Parameters.Add(para.stringparameter("@city", up.city)); Createcommand.exenonquery(cmd); } }
UpdateLogic:
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; ////// Summary description for updatelogic /// public class updatelogic { public updatelogic() { // // TODO: Add constructor logic here // } public static void update_user(user_property up) { SqlCommand cmd = new SqlCommand(); cmd.CommandText = "update_user_tb_sp"; Createparameter para = new Createparameter(); cmd.Parameters.Add(para.intparameter("@id", up.id)); cmd.Parameters.Add(para. stringparameter("@city", up.city)); cmd.Parameters.Add(para.stringparameter("@mobile", up.mobile)); cmd.Parameters.Add(para.stringparameter("@name", up.name)); Createcommand.exenonquery(cmd); } }
DeleteLogic:
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; ////// Summary description for deletelogic /// public class deletelogic { public deletelogic() { // // TODO: Add constructor logic here // } public static void deletedata(string sp, string id) { SqlCommand cmd = new SqlCommand(); cmd.CommandText = sp; Createparameter para = new Createparameter(); cmd.Parameters.Add(para.intparameter("@id", id.ToString())); Createcommand.exenonquery(cmd); } }
OtherLogic:
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; ////// Summary description for otherlogic /// public class otherlogic { public otherlogic() { // // TODO: Add constructor logic here // } public static DataTable bindgried(string sp) { SqlCommand cmd = new SqlCommand(); cmd.CommandText = sp; DataTable dt = new DataTable(); dt=Createcommand.execquery(cmd); return dt; } public static DataTable updategried(string sp, string id) { SqlCommand cmd = new SqlCommand(); cmd.CommandText = sp; DataTable dt=new DataTable(); Createparameter para = new Createparameter(); cmd.Parameters.Add(para.intparameter("@id", id)); dt = Createcommand.execquery(cmd); return dt; } }
Presentation Layer (UI) :
ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>CS:Untitled Page
using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Globalization; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Session["id"].ToString() != "") { updategried(); } } } protected void sv_btn_Click(object sender, EventArgs e) { if (Session["id"].ToString() == "") { user_property up = new user_property(); up.city = ct_txt.Text; up.mobile = mo_txt.Text; up.name = text.ToString(); insertlogic.insert_user_tb(up); Response.Redirect("data.aspx"); } else { user_property up = new user_property(); up.city = ct_txt.Text; up.mobile = mo_txt.Text; up.name = nm_txt.Text; up.id = Session["id"].ToString(); updatelogic.update_user(up); Session["id"] = ""; Response.Redirect("data.aspx"); } } public void updategried() { DataTable dt = new DataTable(); dt = otherlogic.updategried("selectedit_user_tb_sp", Session["id"].ToString()); if (dt.Rows.Count > 0) { nm_txt.Text = dt.Rows[0]["name"].ToString(); ct_txt.Text = dt.Rows[0]["city"].ToString(); mo_txt.Text = dt.Rows[0]["mobile"].ToString(); } } }
Display Record in Griedview:
ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="data.aspx.cs" Inherits="data" %>Untitled Page
CS:
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class data : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { bindgried(); } public void bindgried() { DataTable dt = new DataTable(); string sp = "select_user_tb_sp"; dt = otherlogic.bindgried(sp); data_grd.DataSource = dt; data_grd.DataBind(); } protected void data_grd_RowDeleting(object sender, GridViewDeleteEventArgs e) { int id = Convert.ToInt32(data_grd.DataKeys[e.RowIndex].Values[0].ToString()); deletelogic.deletedata("delete_user_tb_sp", id.ToString()); bindgried(); } protected void data_grd_RowEditing(object sender, GridViewEditEventArgs e) { int id = Convert.ToInt32(data_grd.DataKeys[e.NewEditIndex].Values[0].ToString()); Session["id"] = id.ToString(); Response.Redirect("Default.aspx"); } }
0 comments :
Post a Comment