02 May 2010

Experience 3 tier architecture

There are lot internet information’s on 3 tier architecture. I don’t want to add one more on top of it to spam a Google query! This is only to drow my experience on 3 tier architechure.
Dot NET framework and Visual Studio present many choices for choosing the right architecture from placing the data access code directly in the UI through datasets as well as separating all these code in a reusable component nothing called layers. Like any other developer I also come across to select 3 tiers in my application so want to share my experiences in this. Here I need to come up with an application where the presentation layer needs to extract information from a backend database, the presentation would utilize a series of layers to retrieve the data, rather than having the database calls embedded directly within itself. So that I can use be reused this components as well as will end up an easy maintaince.There may be also chances was there but some of the functionality may later be moved to a smart client application. Portions of an application may be split between a web site and a web or windows service that runs on a server.Likewice so many reasons was there to select three tiers as application architecture for our development. Rather than explain the reason I would like to take you how we went ahead and implemented in our application.
As one of the greatest advantages we have split the layers between developers across the location and started development parallel among 12 member team. As each layer complete with their development we have integrated the code and shaped to complete application! Simple approach right? The below diagram will give more clear picture.

As every other 3 tier application we too have ,Tier 1 or GUI tier with a form will be called FrmCompanyGUI, tier 2 or business logic will be called BO Company short for Business Object Company and finally the tier 3 or the data tier will be called CompanyDAL short for Data Access Company. I have compiled all the 3 tiers in the same project for ease of work.
User Interface tier :
User Interface layer is nothing but UI. Please see the below image, just a proto type.Dont want to give real UI image or code here as I want to keep the confidentiality of the real time application as well as client.

This is a chunk of code from the user interface. I am only including the functions that are used to call the middle tier or the business logic layer. I am keeping a reference to business logic layer as CompanyBO.
protected void Page_Load(object sender, EventArgs e)
{

DataSet ds = new DataSet();

CompanyBO objStudentBO = new CompanyBO ();

ds = CompanyBO.GetCompanyDetails();

grdStudentDetails.DataSource = ds.StudentDetails;

grdCompanyDetails.DataBind();

}

Business Logic layer :
Here, I am including all the code for this tier. Basically it has properties that are needed to define the compny object. But as I mentioned it is just a dummy company and many other properties can be added if required. It also has all the methods including Add, update, find that are required to maintain customer details.
This is the middle tier and acts between the GUI and the Data access layer. It keeps a reference to the data access tier as companyDA = new companyDA (). It also has a reference to System.Data namespace as sometimes it returns details in the form of DataSet to the GUI tier.
We have a defined properties of the business objects ,May be some time we can keep it on property layer if required.
public class Companypublic CompanyDS GetCompanyDetails(long searchID)

{

private string CompanyName { get; set; }

private string CompanyID { get; set; }

private string CompanyAddress1 { get; set; }

public Company()

{

}

public string CompanyName

{

get{return CompanyName;}

set{CompanyName = value;}

}

public string CompanyID

{

get { return CompanyID; }

set { CompanyID = value; }

}

}
operations:
{

CompanyDS companyDS = new CompanyDS();

try

{

CompanySummaryDM objComSumDAL = new CompanySummaryDM();

companyDS = objComSumDAL.GetCompanyDetails(searchID, isCoSeqId);

return companyDS.CompanyDetails;

}

catch (DatabaseException ex)

{

throw ex;

}

catch (Exception ex)

{

throw new BusinessException(ex);

}

}
Data Access Layer :
The data tier has details to manipulate an SQL database. However, all these details are transparent and do not affect the business logic layer. This module has a reference to the business logic layer as ComapnyBO objCompanyBO. To make the usage easier and be supported by any other database I have created some constants including the database name and field name that need to be changed as per the customer table details. This is an usable module for any database after making these changes.
public CompanyDS GetCompanyDetails()

{

DataSet ds = new DataSet();

CompanyDS companyDS = new CompanyDS();

IDbConnection conn = DataFactory.CreateConnection();

conn.ConnectionString = this.ConnectionString;

IDbDataParameter[] commandParameters = new IDbDataParameter[1];

commandParameters[0]= DataHelper.CreateParameter("@SearchID", DbType.Int64, ParameterDirection.Input, CompanyID);
try

{

ds=DataHelper.ExecuteDataset(conn, CommandType.StoredProcedure,"CompanyDetails", commandParameters);

ds.Tables[0].TableName = "CompanyDetails";

companyDS.Merge(ds);

return companyDS;

}

catch (Exception ex)

{

throw new DatabaseException(ex);

}

finally

{

conn.Close();

}

}

Deployment:
Yes we have completed all the three layers. Now the time for deployment. Yes it’s too simple. Using a separation of layers can aid in development of distributed applications. Because the code is broken up into layers, a layer that facilitates the use of web services can be added to the project, with a minimal amount of work.
In 3 tier architecture there are basically two types of assemblies .One is for Business Layer and other for Data access layer. In our particular example we have CompanyUI.dll ,CompanyBO.dll and ComapnyDA.dll
If we have to deploy these on three different server than We need to do something like rather we did same.
Server 1: Database server: (SQL Server / Oracle)
Server 2: Business Server: Contain Assemblies and
Communicate to Presentation layer using web service or WCF.
Server 3: Presentation Server: Contain the UI part to display data fetched from Business layer consuming the Web service or WCF.

No comments: