Monday, July 28, 2008

Creating a Business Logic Layer

The Data Access Layer (DAL) created in the first tutorial cleanly separates the data access logic from the presentation logic. However, while the DAL cleanly separates the data access details from the presentation layer, it does not enforce any business rules that may apply


In this tutorial we'll see how to centralize these business rules into a Business Logic Layer (BLL) that serves as an intermediary for data exchange between the presentation layer and the DAL. In a real-world application, the BLL should be implemented as a separate Class Library project; however, for these tutorials we'll implement the BLL as a series of classes in our App_Code folder in order to simplify the project structure. Figure 1 illustrates the architectural relationships among the presentation layer, BLL, and DAL.

Partial code for Business Logic Layer



using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

///
/// Summary description for blayer
///

public class blayer
{
public blayer()
{
//
// TODO: Add constructor logic here
//
}

///
/// insert records into database
///

public int insert(string title, string description)
{

person p1 = new person();
try
{
return p1.insert(title, description);
}
catch (Exception ex)
{
throw;
}
finally
{
p1 = null;
}
}

///
/// update records into database
///

public int update(int id, string title, string description)
{
person p1 = new person();

try
{
return p1.update(id, title, description);
}

catch
{
throw;
}

finally
{
p1 = null;
}
}
///
/// Shows records from database
///

public DataTable show()
{
person p1 = new person();

try
{
return p1.show();
}
catch
{
throw;
}
finally
{
p1 = null;

}

}
public DataTable show1(int id)
{
person p1 = new person();

try
{
return p1.show1(id);
}
catch
{
throw;
}
finally
{
p1 = null;
}
}
///
/// Delete record from database
///

public int delete(int id)
{
person p1 = new person();

try
{
return p1.delete(id);
}

catch
{
throw;

}
finally
{
p1 = null;

}
}
}

Creating a Data Access Layer

We'll start with creating a software architecture composed of a Data Access Layer (DAL) using Typed DataSets, a Business Logic Layer (BLL) that enforces custom business rules, and a presentation layer composed of ASP.NET pages that share a common page layout. Once this backend groundwork has been laid, we'll move into reporting, showing how to display, summarize, collect, and validate data from a web application

In this tutorial we'll start from the very beginning and create the Data Access Layer (DAL), followed by creating the Business Logic Layer (BLL) in the second tutorial, and working on page layout and navigation in the third. The tutorials after the third one will build upon the foundation laid in the first three

This layer is also a class which we use to get or set the data to the database back and forth. This layer only interacts with the database. We write the database queries or use stored procedures to access the data from the database or to perform any operation to the database.
Summary

  • Application layer is the form where we design using the controls like textbox, labels, command buttons etc.
  • Business layer is the class where we write the functions which get the data from the application layer and passes through the data access layer.
  • Data layer is also the class which gets the data from the business layer and sends it to the database or gets the data from the database and sends it to the business layer.
  • Property layer is the sub layer of the business layer in which we make the properties to sent or get the values from the application layer. These properties help to sustain the value in a object so that we can get these values till the object destroy
Partial Code for Data Access Layer



using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public class person
{
SqlConnection myconn = new SqlConnection("server=isa;database=CuteChat4;uid=sa;password=sa");

public person()
{
//
// TODO: Add constructor logic here
//
}
///


/// Used to insert records into database
///

public int insert(string title,string description)
{
myconn.Open();
SqlCommand cmd = new SqlCommand("insertnews", myconn);
cmd.CommandType = CommandType.StoredProcedure;

try
{
cmd.Parameters.AddWithValue("@title", title);
cmd.Parameters.AddWithValue("@description", description);
return cmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
cmd.Dispose();
myconn.Close();
myconn.Dispose();

}

}
///
/// Update record into database
///

public int update(int id, string title, string description)
{

myconn.Open();
SqlCommand cmd = new SqlCommand("updatenews", myconn);
cmd.CommandType = CommandType.StoredProcedure;
try
{
cmd.Parameters.AddWithValue("@id", id);
cmd.Parameters.AddWithValue("@title", title);
cmd.Parameters.AddWithValue("@description", description);
return cmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
cmd.Dispose();
myconn.Close();
myconn.Dispose();
}

}
///
/// Shows all records from database
///

public DataTable show()
{

SqlDataAdapter da = new SqlDataAdapter("selectnews", myconn);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();

try
{
da.Fill(ds, "news");
return ds.Tables["news"];
}
catch
{
throw;
}

finally
{
ds.Dispose();
da.Dispose();
myconn.Close();
myconn.Dispose();
}
}

public DataTable show1(int id)
{

SqlDataAdapter da = new SqlDataAdapter("select* from news where id=" + id, myconn);
da.SelectCommand.CommandType = CommandType.Text;
DataSet ds = new DataSet();

try
{
da.Fill(ds, "news");
return ds.Tables["news"];
}
catch
{
throw;
}

finally
{
ds.Dispose();
da.Dispose();
myconn.Close();
myconn.Dispose();
}

}

///
/// Delete record from database
///

public int delete(int id)
{
myconn.Open();
SqlCommand cmd = new SqlCommand("deletenews", myconn);
cmd.CommandType = CommandType.StoredProcedure;

try
{
cmd.Parameters.AddWithValue("@id", id);
return cmd.ExecuteNonQuery();
}

catch
{
throw;
}

finally
{
cmd.Dispose();
myconn.Close();
myconn.Dispose();

}

}

}