Here I will explain about uses of 3-Tier architecture and how to create or implement 3-tier architecture for our project in asp.net

Description

1.What is the use of 3-tier architecture and why we go for that architecture?

2.First we need to know what 3-Tier architecture is.

3.How to create 3-Tier architecture for our project?

Uses of 3-Tier Architecture

1.To make application more understandable.

2.Easy to maintain, easy to modify application and we can maintain good look of architecture.

3.If we use this 3-Tier application we can maintain our application in consistency manner.

Basically 3-Tier architecture contains 3 layers

1.Application Layer or Presentation Layer

2.Business Access Layer(BAL) or Business Logic Layer(BLL)

3.Data Access Layer(DAL)

Here I will explain each layer with simple example that is User Registration

Application LayerorPresentation Layer

Presentation layer contains UI part of our application i.e., our aspx pages or input is taken from the user. This layer mainly used for design purpose and get or set the data back and forth. Here I have designed my registration aspx page like this

This is Presentation Layer for our project Design your page like this and double click on button save now in code behind we need to write statements to insert data into database this entire process related to Business Logic Layer and Data Access Layer.

Now we will discuss about Business Access Layer or Business Logic Layer

Business Access Layer(BAL)or Business Logic Layer (BLL)

This layer contains our business logic, calculations related with the data like insert data, retrieve data and validating the data. This acts as a interface between Application layer and Data Access Layer

Now I will explain this business logic layer with my sample

I have already finished form design (Application Layer) now I need to insert user details into database if user click on button save. Here user entering details regarding Username, password, Firstname, Lastname, Email, phone no, Location. I need to insert all these 7 parameters to database. Here we are placing all of our database actions into data access layer (DAL) in this case we need to pass all these 7 parameters to data access layers.

In this situation we will write one function and we will pass these 7 parameters to function like this

If we need this functionality in another button click there also we need to declare the parameters like string Username, string Password like this rite. If we place all these parameters into one place and use these parameters to pass values from application layer to data access layer by using single object to whenever we require how much coding will reduce think about it for this reason we will create entity layer or property layer this layer comes under sub of group of our Business Logic layer

Don't get confuse just follow my instructions enough

How we have to create entity layer it is very simple

Right click on your project web application---> select add new item ----> select class file in wizard ---> give name as BEL.CS because here I am using this name click ok

Open the BEL.CS class file declare the parameters like this in entity layer

Don’t worry about code it’s very simple for looking it’s very big nothing is there just parameters declaration that’s all check I have declared whatever the parameters I need to pass to data access layer I have declared those parameters only

BEL.CS

#region Variables

///<summary>

/// User Registration Variables

///</summary>

privatestring _UserName;

privatestring _Password;

privatestring _FirstName;

privatestring _LastName;

privatestring _Email;

privatestring _Phoneno;

privatestring _Location;

privatestring _Created_By;

#endregion

///<summary>

/// Gets or sets the <b>_UserName</b> attribute value.

///</summary>

///<value>The <b>_UserName</b> attribute value.</value>

publicstring UserName

{

get

{

return _UserName;

}

set

{

_UserName = value;

}

}

///<summary>

/// Gets or sets the <b>_Password</b> attribute value.

///</summary>

///<value>The <b>_Password</b> attribute value.</value>

publicstring Password

{

get

{

return _Password;

}

set

{

_Password = value;

}

}

///<summary>

/// Gets or sets the <b>_FirstName</b> attribute value.

///</summary>

///<value>The <b>_FirstName</b> attribute value.</value>

publicstring FirstName

{

get

{

return _FirstName;

}

set

{

_FirstName = value;

}

}

///<summary>

/// Gets or sets the <b>_LastName</b> attribute value.

///</summary>

///<value>The <b>_LastName</b> attribute value.</value>

publicstring LastName

{

get

{

return _LastName;

}

set

{

_LastName = value;

}

}

///<summary>

/// Gets or sets the <b>_Email</b> attribute value.

///</summary>

///<value>The <b>_Email</b> attribute value.</value>

publicstring Email

{

get

{

return _Email;

}

set

{

_Email = value;

}

}

///<summary>

/// Gets or sets the <b>_Phoneno</b> attribute value.

///</summary>

///<value>The <b>_Phoneno</b> attribute value.</value>

publicstring Phoneno

{

get

{

return _Phoneno;

}

set

{

_Phoneno = value;

}

}

///<summary>

/// Gets or sets the <b>_Location</b> attribute value.

///</summary>

///<value>The <b>_Location</b> attribute value.</value>

publicstring Location

{

get

{

return _Location;

}

set

{

_Location = value;

}

}

///<summary>

/// Gets or sets the <b>_Created_By</b> attribute value.

///</summary>

///<value>The <b>_Created_By</b> attribute value.</value>

publicstring Created_By

{

get

{

return _Created_By;

}

set

{

_Created_By = value;

}

Our parameters declaration is finished now I need to create Business logic layer how I have create it follow same process for add one class file now give name called BLL.CS. Here one point don’t forget this layer will act as only mediator between application layer and data access layer based on this assume what this layer contains. Now I am writing the following BLL.CS(Business Logic layer)

#region Insert UserInformationDetails

///<summary>

/// Insert UserDetails

///</summary>

///<param name="objUserBEL"></param>

///<returns></returns>

publicstring InsertUserDetails(BEL objUserDetails)

{

DAL objUserDAL = newDAL();

try

{

return objUserDAL.InsertUserInformation(objUserDetails);

}

catch (Exception ex)

{

throw ex;

}

finally

{

objUserDAL = null;

}

}

#endregion

Here if you observe above code you will get doubt regarding these

what is

BEL objUserDetails

DAL objUserDAL = newDAL();

and how this method comes

return objUserDAL.InsertUserInformation(objUserDetails);

Here BEL objUserDetails means we already created one class file called BEL.CS with some parameters have you got it now I am passing all these parameters to Data access Layer by simply create one object for our BEL class file

What is about these statements I will explain about it in data access layer

Here if you observe above functionality I am getting all the parameters by simply creating BEL objBELUserDetails. If we create one entity file we can access all parameters through out our project by simply creation of one object for that entity class based on this we can reduce redundancy of code and increase re usability

Observe above code have u seen this function before? in BLL.CS i said i will explain it later got it in DAL.CS I have created one function InsertUserInformation and using this one in BLL.CS by simply creating one object of DAL in BLL.CS.

Here you will get one doubt that is why BLL.CS we can use this DAL.CS directly into our code behind we already discuss Business logic layer provide interface between DAL and Application layer by using this we can maintain consistency to our application.

Now our Business Logic Layer is ready and our Data access layer is ready now how we can use this in our application layer write following code in your save button click like this

Hi, really great tutorial here. Before reading this I had some problems understanding when to use an entity layer or property layer. I noticed some tutorial used it, and some didnt. I believe you explained that part very clear here.

Hello friends finally i got final result in 3 tier architectur app.but i have taken only 3 controls.i have posted entire code including proc in my blogspot...so please click this link for further details http://www.shankar-mydotnetsite.blogspot.in/

Hi this post was very nice & 200% useful for 3Tier freshers including me. This shows your technical stuff as well as reputation.Iam expecting similar like this post for basic or freshers who are willing to learn mvc, because we & our blog fans need to update with current trend & technology.So, Please post mvc example.Thank you!

Hi this post was very nice & 200% useful for 3Tier freshers including me. This shows your technical stuff as well as reputation.Iam expecting similar like this post for basic or freshers who are willing to learn mvc, because we & our blog fans need to update with current trend & technology.So, Please post mvc example.Thank you!

Hi, I like your example of three tier example .It is very easy to understand .I have a request for you can you please tell me about stored Procedure ,So i can get best explanation.THANKS A LOT..And keep it up

hi Mr.suresh...........Its very helpful to me and i have a doubt?how can i insert the values in more than tables from windows form to database via DAL and BAL concept...Please Tell me and i am waiting for your ans

Ya its good topic..But i have one doubt on 3 tier architecture.How to fill master and child table using 3 Tier architecture by using Transaction?Please send me answer to this mail id frnz.jun@gmail.com

VOUCHER:• VOUCHER NO (PK) (VAR 30)• DATE (VAR 10)• PAID TO (VAR 30)• AMOUNT (DOUBLE 30)• REASON (VAR 45)• AUTHORIAZED SIGNATURE (TEXT )Voucher is the table and folling are the attribute now i want desining and store procedure with 3-tire architure with coding every thing Please help me out of the proble are mail me sunilkumar.subbu@gmail.com

hello my query is how to implement 3 tiers Architecture in whole asp.net project i.e if there is more than 10 form so how to implement 3 tiers architecture is there create each bal,dal for each databasse codeplease solve my query

Can you please explain, which reference is to be added to which layer.

I have added BLL reference in DLL and Application Layer. Now when am trying to add DLL reference to BLL it says A reference to DLL couldn't be added. Adding this project as a reference will cause circular dependency, and without adding DLL reference to BLL i guess we cant access the methods of DLL in BLL.

hi suresh you have done lots of good work,but according to my work experience this is not best example of 3 tier and also 3 layer architecture.you have just added the three class files which can not be reused it again and again which is the primary goal of layered.the coding flow is good but architecture is not good

namespace DAL{ public class Employee { int employeeID; string lastName; // should be (20) chars only string firstName; // should be (10) chars only string title; // should be (30) chars only string address; // should be (60) chars only string city; // should be (15) chars only string region; // should be (15) chars only string postalCode; // should be (10) chars only string country; // should be (15) chars only string extension; // should be (4) chars only

Hello.. Its very helpful article. But, according to my understanding, Actually 3 tier architecture means it contains Application layer, Business Layer and Data Access Layer and each layer should be in separate machines. Actually the example above illustrates N-layer architecture.. Could u please ellaborate the difference between n-tier and n-layer architecture in brief?

Please help me .. i don't understand this linecmd.Parameters.Add("@Error",sqlDbType.char,500);sqlDbType can't exist in the current context.I also using namespace using System.Data;using System.Data.SqlClient;Yet error can seen..i don't understand.

Bro i understood all clearly.But my question is my web app contains more forms.so i need to connect all with DB.so Which is best method for each form one one DAL code file or one DAL code for all pages ?Please help me,,my id : pk.suhail47@gmail.com

Your are created a tightly coupled 3 tired architecture. If you go to your deployment situation, it will be very hesitate. It mean every layer goes to separate server... you need to create a one service layer for communicate each layer, it will be change your entire architecture and the code.

excellent. but how to use a simple query instead of stored procedure. that is how to pass the query like select * from table do we need to create different methods for different entities? please reply soon.

Hi,I have a problem with web.config fileMy Connection String is stored in web.config file.Which is in UI Layer.Then How can to access my web.config file from Data Acess Layer.Please tell your suggestion .Any help would be appriciated.Thanks

Hi suresh I create one website in 3 tire architecture when I publish with filesystem and upload in live it give error

Server Error in '/' Application.Parser ErrorDescription: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.