Creating and working with layered Architecture.


In the companies normally the project uses the architectural format for coding. It simplifies the code.In this article i am going to explain the normally used architecture format.Here we will see how we can create an architectural format and work with it.

A very common architectural format is n- tier architecture.Normally we divide it in 3 layers.

User interface -> contains the asp.net controls and no code related to data access.
Business Logic -> contains business logic and the validations.
Data access layer -> interacts with the database

In the Business logic layer we have below code.we have methods for select, insert,update and delete category:


public class Addcategory
{
private string strQuery, strNewcategoryname, strCategoryname, strCategorydesc;
private int strCategoryID;

public int Newcategoryid
{
get { return strCategoryID; }
set { strCategoryID = value; }
}
public string Newcategoryname
{
get { return strNewcategoryname; }
set { strNewcategoryname = value; }
}

public string Categoryname
{
get { return strCategoryname; }
set { strCategoryname = value; }
}
public string Categorydesc
{
get { return strCategorydesc; }
set { strCategorydesc = value; }
}

public DataSet Category()
{
strQuery = "Select CName,CategoryID,DateAdded,CDesc from Category where IsActive= '1'";
return DBFunctions.getDataSet(strQuery);
}

public int CategoryInsert()
{
strQuery = "IF NOT EXISTS(SELECT * FROM Category WHERE CName = '" + strNewcategoryname + "') INSERT INTO Category (CName,CDesc) VALUES ('" + strNewcategoryname + "','" + strCategorydesc + "')";
int i = DBFunctions.ExecuteSQL(strQuery);
return i;
}

public int CategoryUpdate()
{
strQuery = "IF NOT EXISTS(SELECT * FROM Category WHERE CName = '" + strNewcategoryname + "' and CategoryID != " + strCategoryID + ")"
+ "update Category set CName = '" + strNewcategoryname + "',CDesc = '" + strCategorydesc + "' where CategoryID = " + strCategoryID + "";
int i = DBFunctions.ExecuteSQL(strQuery);
return i;
}
public int DeleteCategory(string name)
{
strQuery = "update Category set IsActive = '0' where CName='" + name + "'";
return DBFunctions.ExecuteSQL(strQuery);
}



and then in dataaaccess layer we can have below code to execute the query and insert, update and delete
the values from database:

abstract public class DBFunctions
{
static SqlConnection sqlcon;
static DBFunctions()
{
try
{
sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
sqlcon.Open();
}
catch (Exception ex)
{
throw ex;
}
}

static void ConnectionOpen()
{
if (sqlcon.State == ConnectionState.Closed)
{
sqlcon.Open();
}
}

static void ConnectionClose()
{
if (sqlcon.State == ConnectionState.Open)
{
sqlcon.Close();
}
}
//used in register
public static int ExecuteSQL(string strQuery)
{
int intNoRecords;
try
{
ConnectionOpen();
SqlCommand sqlcmd = new SqlCommand(strQuery, sqlcon);
intNoRecords= Convert.ToInt32(sqlcmd.ExecuteNonQuery());
ConnectionClose();
return intNoRecords;
}
catch (Exception ex)
{
throw ex;
}
}

public static DataSet getDataSet(string strQuery)
{
try
{
ConnectionOpen();
SqlDataAdapter sqladap = new SqlDataAdapter(strQuery, sqlcon);
DataSet ds = new DataSet();
sqladap.Fill(ds);
ConnectionClose();
return ds;
}
catch (Exception ex)
{
throw ex;
}
}
}


Now in the user interface layer we will have asp.net controls. and then we can acess above layers
for inserting, updating and deleting the records.


protected void Page_Load(object sender, EventArgs e)
{
if (Session["str"] == null)
Server.Transfer("admin.aspx");
if (!IsPostBack)
{
PopulateCategory();
}
}
// Insert a new category into database.
protected void btnadd_Click(object sender, ImageClickEventArgs e)
{
try
{
Addcategory c = new Addcategory();
c.Newcategoryname = txtname.Text;
c.Categorydesc = txtdesc.Text;
int i = c.CategoryInsert();
if (i == 0)
{
lblerr.Text = "Error";
}
else if (i == -1)
{
lblerr.Text = "Category Already Exists";
}
else
{
lblerr.Text = "Category Added";
PopulateCategory();
txtname.Focus();

}
}
catch (Exception ex)
{
lblerr.Text = ex.Message;
}
}
// Get the category list from database.
private void PopulateCategory()
{
try
{
ddl_categories.Items.Clear();
Addcategory c = new Addcategory();
DataSet ds = c.Category();
ddl_categories.DataValueField = "CategoryID";
ddl_categories.DataTextField = "CName";
ddl_categories.DataSource = ds;
ddl_categories.DataBind();
ddl_categories.Items.Insert(0, new ListItem("Select", string.Empty));
}
catch (Exception ex)
{
lblerr.Text = ex.Message.ToString();
}
}
// Update the category in table.
protected void btnupdate_Click(object sender, ImageClickEventArgs e)
{
try
{
Addcategory c = new Addcategory();
c.Newcategoryname = txtname.Text;
c.Newcategoryid = Convert.ToInt32(ddl_categories.SelectedValue);
c.Categorydesc = txtdesc.Text;
int i = c.CategoryUpdate();
if (i == 0)
{
lblerr.Text = "Error";
}
else if (i == -1)
{
lblerr.Text = "Category Already Exists";
}
else
{
lblerr.Text = "Category Updated";
txtname.Focus();
PopulateCategory();
}
}
catch (Exception ex)
{
lblerr.Text = ex.Message;
}
}

//Delete the category
protected void btndelete_Click(object sender, ImageClickEventArgs e)
{
try
{
Addcategory c = new Addcategory();
int i = c.DeleteCategory(ddl_categories.SelectedItem.ToString());
if (i != 0)
{
lblerr.Text = "Deleted Sucessfully";
PopulateCategory();
}
else
{
lblerr.Text = "Invalid Category";
}

}
catch (Exception ex)
{
lblerr.Text = ex.Message.ToString();
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: