Free Jobs
Reference




Dot Net Forums: ASPNET (77) C#.NET (39) VB.NET (8) AJAX (21) Silver Light (1) WCF (10) WPF (1) WWF (1) .NET 3.5 (3) SQL SERVER (38) Other (3)

Topic: Simple hello world Three tier architecture in ASP.NET and C# Designing and Developing

To get the Posted Questions details daily in your mail Click Here

Share this Post on Facebook/Twitter/Orkut Bookmark and Share





User: dhiraj5079 Post Date: 18 Nov 2010 09:33

Introduction:
While developing enterprise applications you often hear the term three tier architecture.In this article we will see a simple three tier Architecture Three tier architecture website in ASP.NET and C#.



Main Content:
Designing a Three-Tier Architecture means designing the application in three
interconnected layers with one layer interacting with other layer without
knowing much detail about the other.

In three tier architecture a layer should not interact with all the other
layers. There should be some rules for interaction. Generally the three
layers are referred as Presentation Layer, Business Access Layer (BAL) and
Data Access Layer (DAL).So Ideally Presentation Layer should be
interacting with Business Access Layer and Business Access Layer should be
interacting with Data Access Layer and there should be no interaction
between Presentation Layer and Data Access Layer As shown in the picture
Below.



But before implementing the 3 tier architecture we must understand its advantage.
The main advantages of three tier architecture are as below:

a) Developing the applications becomes easier as different tiers can be developed by separate group of developers once the interface between the layers is defined.
b) It is easier to modify or replace any tier without affecting the other tiers so maintainability of code increases.
c) The tiers like Data Access Layers can be developed in a generic way and can be used
In other application like plug and play.
d) Good security policies can be implemented between tiers, especially in financial applications.


Simple Facts:
What is Presentation layer or User Interface Layer or UI Layer:

When we are talking about layers we are talking about C# or VB code layers
What ever you write in .cs file means code behind file can be considered

As Presentation layer.

What is Business Access layer or Business Logic Layer or BAL
Layer:

There may be many way to implement a BAL Layer and one of them is
Adding a bal.cs file to the app.code folder of the website.
This file should contain the business logic. One example of business logic
may be To validate an uploaded XML file with a given schema. So this kind
of activity we should do in bal.cs i.e. Business Logic layer.

What is Data Access layer or DAL Layer:
There may be many way to implement a DAL Layer and one of them is
Adding a dal.cs file to the app.code folder of the website.
This file should contain the data access logic. One example of data access
logic may be To make connection to database and insert user entered data
to database.

So in this article I will be giving you a very simple ASP.NET website developed in
Three tier architechture.Note that the same can be easily applied to a windows application.
This article code is written in ASP.NET and C#.

In this article we will be doing a login page Default.aspx. This page takes a user name and password as an input and checks in the database whether the user name password matches or not. If the user name password matches then it transfers it to Home.aspx And if the user name password does not match then it remains on the Default.aspx
Page with with message to the user "Invalid Username or Password!" as given in the image below.


Presentation Layer code

The code behind file Default.aspx.cs code should be as given below
protected void ButtonLogin_Click(object sender, EventArgs e)
{
//This code is to check the username password of user.
string username = TextBoxUserName.Text;
string password = TextBoxPassword.Text;

//Creating the object of Business access layer(bal)
BAL balObject = new BAL();
//calling the function CheckForUserNamePassword of business access
layer.
bool matched = balObject.CheckForUserNamePassword(username,
password);

if (matched == true)
{
//Adding the username in session object to be used in the
FresherHome page for welcome message.
Session["UserName"] = username;
//Direct to the Fresher home page.
Response.Redirect("Home.aspx");
}
else
{
//Username password did not match so show the message to the
user.
LabelMessage.Text = "Invalid Username or Password!";
LabelMessage.Style.Add("Color", "Red");
}
}

Create the two .cs file in the solution explorer as given in the Image
below.


One is Business Access Layer BAL.cs
Other is Data Access Layer DAL.cs.

Business Access Layer code

Now your Bal.cs Code should be like as below:

//This function takes out all the users table data from database and
compares
//the username password combination.
public bool CheckForUserNamePassword(string username, string password)
{
bool matched = false;
//Creating the object of data access layer class(dal)
DAL dalObject = new DAL();

//Calling the method of GetAllUsersData of data access layer class
(dal)
DataSet dsUsers = dalObject.GetAllUsersData();
int noOfRows = dsUsers.Tables["UsersTable"].Rows.Count;

for(int i = 0 ;i {
//Comparing for correct username password combination.
//If both isername and password in the same row match with the
corresponding username password
//entered in the text box at login page the matched is set
true . other wise match remains flase
if ((dsUsers.Tables["UsersTable"].Rows[i]["username"].ToString
() == username) && (dsUsers.Tables["UsersTable"].Rows[i]
["password"].ToString() == password))
{
matched = true;
}
}

return matched;

}



Data Access Layer code


Creating DAL Layer Code:

public DataSet GetAllUsersData()
{
//Reading database connection string form web.config file.
string ConnectionString = ConfigurationManager.ConnectionStrings
["DataBaseConnectionString"].ToString();

//Creating connection object
//Note for this to compile we also need to add one extra using
System.Data.SqlClient; at the top of the page.
SqlConnection connection = new SqlConnection(ConnectionString);

//Specifying the name of the stored Procedure "GetAllUsersDataSP"
to be used in the command object
SqlCommand cmd = new SqlCommand("GetAllUsersDataSP", connection);

cmd.CommandType = CommandType.StoredProcedure;

SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet dsUsers = new DataSet();
//The line below opens the database connection ,executes the
command
//and closes the connection all in one line.
da.Fill(dsUsers, "UsersTable");

//RETURNS THE FULL DATA of users to BAL LAYER FROM WHERE this
function was called.
return dsUsers;


}

In this we have used GetAllUsersDataSP as the stored procedure name
Which brigs user data to dal layer and gives it to BAL Layer.
The Code is well commented for understanding purpose.

Conclusion:
Thus we are calling the functions of Bal Layer from Presentation layer
And BAL is calling functions in DAL Layer.We should not call a Function of
DAL Layer from Presentation Layer.



About Writer:
Hi,I am IT Professional From Mumbai


Writer's Link: http://

Writer's Email:







Comments:
Post Comment





Similar Articles Top ArticlesLatest Articles Other Articles by this Author







www.freejobsreference.com Copyright © 2009-2011 | All Rights Reserved
All content of this website is protected by copyright laws and should not be reproduced in any form or by any means without written permission
(Best viewed in IE 8.0+ or Firefox 3.0+ at resolution of 1024 * 768 or higher)