Sunteți pe pagina 1din 19

Reader Level:

Articles

How to Implement 3 Layered Architecture


Concepts in ASP.Net
By Sharath kumar on Oct 11, 2014
In this article you will learn how to implement 3-Tier Architecture concepts in ASP.Net.

inShare3

7122

For more information visit my site: New AspDotNet Concepts.


Hi friends, I have created a simple registration page using Three-Tier Architecture concepts in
ASP.NET. We use Three-Tier Architecture concepts in higher-level companies where there
are three separate layers formed. There are three layers in 3-Tier Architectures as given
below:

Presentation Layer (UI Layer)

Business Access Layer (BAL)

Data Access Layer (DAL)

Where each layer is uniquely important in the role of the project.


1. Presentation Layer (UI)
The Presentation Layer is nothing but a user interface that every user sees on your computer,
mobile and Windows screen. You can say that a design part of any application is known as
Presentation Layer. The user can post input and get output on your Presentation Layer only.
The ASP.NET .axpx file is known as a the Presentation Layer.
2. Business Access Layer (BAL)
The Business Access Layer acts as a mediator layer between the Presentation layer and the
Data Access layer. This layer is used to transfer the data between the Presentation Layer and
Data Access Layer. This layer is mainly used for Validations and calculations purposes. Every
validation and calculation of data is held on that layer only. I have also implemented
Property layer or Entity Layer concepts in Business Access Layer. It is an optional layer if
you are working on small project. But if you are working on large projects then you need to
include this layer in your 3-Tier Architecture Applications. It is used to enhance the security
and prevent to brokering the application.
3. Data Access Layer (DAL)
This layer only communicates with the Business Access Layer. The Data Access Layer
contains the methods that helps a Business Access Layer. The Business Layer class's methods
call the Data Access Layer Class methods to do some required actions with database such as
insertion, deletion, updates and so on. All database related connection codes are written in
this layer only such as SQL query, stored procedure and so on.
You can easily understand the exact concepts of 3-Tier Applications as shown below:

What are the working process of 3 Layers


When any user post data from your presentation layer (user Interface layer) --> then this data
first goes to the Business Access Layer then the validation and calculation is done on this
layer then this data passes data to the Data Access Layer then the Data Access Layer fetches
the required data or inserts the data into the database. Then the Data Access Layer passes the
required data to the business Access Layer. Then the Business Access Layer sends the
required data to the Presentation Layer. Then the Presentation Layer is responsible for
displaying the required data to the user's computers or mobiles or Windows.
Why we use 3-Tier Architecture
There are the following reasons to use 3-Tier Architecture in our ASP.NET applications:

To increase the security in application.

To easily maintain the application.

To easily modify or change in application.

To reduce the server over load.

To reduce the loading time of the application.

Examples

All companies working in a software environment always use 3-Tier Architecture


concepts in their applications, whether they are working on small projects or large
projects. if you are using 3-Tier concepts then it will be more complex but more
understandable to the users.

Suppose you are working on a project in a team of 1000 members. There are many
members shifted day by day due to some problems. When any new member is
assigned for this project, then he can easily understand the concept of 3-Tier
Architecture and be involved in the project. If the companies are working on a 1-Tier
Architecture then the shifting of an employee from one project to another project is
not possible.

In 3-Tier, you can easily update any tier of code. But in 1-Tier, it is not possible.

Advantages of 3-Tier Architecture

Each layer always uses your separate code, so it is easy to modify the code.

It is helpful to easily maintain and understand the large projects.

You can easily change your graphical environment.

It is more secure because users can't access the database directly.

You can easily change any layer of code without affecting the other two layers.

It is a more consistent application.

Disadvantages of 3-Tier Architecture

Its takes more time to build.

Many people encounter problems because they lack good knowledge of OOP
concepts and other C# programming such as class, object, property and so on.

3-Tier Architecture is more complex to build.

There are some steps to implement the 3-Tier Architecture concepts in ASP.NET applications.
In this application, I will explain how to build a 3-Tier registration and login page in
ASP.NET as given below:

I have created this login page using a 3-Tier architecture.

Inside the BuninessLayer class library, I have created a class named


BuninessLayer.cs.

Inside the DAL class library I created a class named dataLayer.cs.

Inside the Registration3Tier application I created the web form RegisterData.aspx.

In the following, I rovided code for all the classes and web forms.

Step 1
1. create database skyworld
2.
3. use skyworld
4.

5. create table myregisterdata(sno int identity(1,1) primary key,firstname varchar(30),las


tname varchar(30),username varchar(30),password varchar(30),houseaddress varchar(
30),phoneno varchar(30))
6.
7. select * from myregisterdata
8.
9. create procedure userdata
10. (@spfirstname varchar(30),@splastname varchar(30),@spusername varchar(30),@sp
password varchar(30),@sphouse varchar(30),@spphone varchar(30))
11. as
12. begin
13. insert into myregisterdata values (@spfirstname,@splastname,@spusername,@sppass
word,@sphouse,@spphone)
14. end
15. go

Step 2

RegisterData.aspx Code
1. <table style="height: 304px; width: 393px">
2. <tr>
3. <td>First Name</td>
4. <td>:</td>
5. <td><asp:TextBox ID="txtfirst" runat="server" ></asp:TextBox></td>
6. </tr>
7.
8. <tr>
9. <td>Last Name</td>

10. <td>:</td>
11. <td><asp:TextBox ID="txtlast" runat="server" ></asp:TextBox></td>
12. </tr>
13. <tr>
14. <td>User Name</td>
15. <td>:</td>
16. <td><asp:TextBox ID="txtuser" runat="server" ></asp:TextBox></td>
17. </tr>
18. <tr>
19. <td>Password</td>
20. <td>:</td>
21. <td><asp:TextBox ID="txtpassword" runat="server" ></asp:TextBox></td>
22. </tr>
23. <tr>
24. <td>Address</td>
25. <td>:</td>
26. <td><asp:TextBox ID="txtaddress" runat="server" ></asp:TextBox></td>
27. </tr>
28. <tr>
29. <td>Phone No</td>
30. <td>:</td>
31. <td><asp:TextBox ID="txtphoneno" runat="server" ></asp:TextBox></td>
32. </tr>
33. <tr>
34. <td></td><td></td>

35. <td><asp:Button ID="btnsubmit" runat="server" Text="Submit"


36. onclick="btnsubmit_Click" />

<asp:Button ID="btnreset"

37. runat="server" Text="Reset" onclick="btnreset_Click" /></td>


38. </tr>
39.
40. </table>
RegisterData.aspx.cs Code
1. using System;
2. using System.Collections.Generic;
3. using System.Linq;
4. using System.Web;
5. using System.Web.UI;
6. using System.Web.UI.WebControls;
7. using BuninessLayer;
8. using dataLayer;
9.
10. namespace Registration3Tier
11. {
12.

public partial class RegisterData : System.Web.UI.Page

13.

14.

objectdataBus dataregister = new objectdataBus();

15.

studentlist studentdata = new studentlist();

16.
17.

protected void Page_Load(object sender, EventArgs e)

18.

19.
20.

21.
22.

protected void btnreset_Click(object sender, EventArgs e)

23.

24.

txtfirst.Text = "";

25.

txtlast.Text = "";

26.

txtuser.Text = "";

27.

txtpassword.Text = "";

28.

txtaddress.Text = "";

29.

txtphoneno.Text = "";

30.

31.
32.

protected void btnsubmit_Click(object sender, EventArgs e)

33.

34.

studentdata.FirstName = txtfirst.Text;

35.

studentdata.LastName = txtlast.Text;

36.

studentdata.UserName = txtuser.Text;

37.

studentdata.password = txtpassword.Text;

38.

studentdata.Address = txtaddress.Text;

39.

studentdata.PhoneNumber = txtphoneno.Text;

40.
41.

try

42.

43.

string result = dataregister.record_insert(studentdata);

44.

if (result != null)

45.

46.

Label1.Text = "inserted Successfully";

47.

48.

else

49.

50.

Label1.Text = "Not registerd Successfully";

51.

52.
53.

54.
55.

//catch (Exception info)

56.

//{

57.

// throw info;

58.

//}

59.

finally

60.

61.

studentdata = null;

62.

63.
64.

}
}

65. }

Step 3
Modify the BL layer BuninessLayer.cs as below:
1. using System;

2. using System.Collections.Generic;
3. using System.Linq;
4. using System.Web;
5. using dataLayer;
6.
7. namespace BuninessLayer
8. {
9.

public class objectdataBus

10.

11.
12.

dataLayer.datainfo datanew = new dataLayer.datainfo();

13.
14.

public string record_insert(studentlist abc)

15.

16.

try

17.

18.

return datanew.registration_details(abc);

19.

20.

catch (Exception e)

21.

22.

throw e;

23.

24.

finally

25.

26.

datanew = null;

27.

28.

29.
30.

31. }

Step 4
Modify the DL Layer DataLayer.cs code as below:
1. using System;
2. using System.Collections.Generic;
3. using System.Linq;
4. using System.Web;
5. using System.Data;
6. using System.Data.SqlClient;
7. using System.Configuration;
8.
9. namespace dataLayer
10. {
11.
12.

public class datainfo

13.

14.
15.

string dataconfig = ConfigurationManager.ConnectionStrings["configdata"].To


String();

16.
17.

SqlConnection sqlcon;

18.

SqlCommand sqlcmd;

19.

SqlDataAdapter sqldap;

20.

DataSet ds;

21.
22.

public string registration_details(studentlist user_details)

23.

24.

sqlcon = new SqlConnection(dataconfig);

25.

sqlcmd = new SqlCommand();

26.

sqlcmd.Connection=sqlcon;

27.

sqlcmd.CommandType=CommandType.StoredProcedure;

28.

sqlcmd.CommandText="userdata";

29.

sqlcon.Open();

30.
31.

try

32.

33.

sqlcmd.Parameters.AddWithValue("@spfirstname",user_details.FirstN
ame);

34.

sqlcmd.Parameters.AddWithValue("@splastname",user_details.LastNa
me);

35.

sqlcmd.Parameters.AddWithValue("@spusername",user_details.UserN
ame);

36.

sqlcmd.Parameters.AddWithValue("@sppassword",user_details.passw
ord);

37.

sqlcmd.Parameters.AddWithValue("@sphouse",user_details.Address);

38.

sqlcmd.Parameters.AddWithValue("@spphone",user_details.PhoneNu
mber);

39.
40.

return sqlcmd.ExecuteNonQuery().ToString();
}

41.

catch (Exception showerror)

42.

43.
44.

throw showerror;

45.

46.

finally

47.

48.

sqlcmd.Dispose();

49.

sqlcon.Dispose();

50.

51.
52.
53.

}
}

54. }
Write the C# property codes to get and set the data as given below:
1. public class studentlist
2. {
3.
4.

public string FirstName { set; get; }

5.

public string LastName { set; get; }

6.

public string UserName { set; get; }

7.

public string password { set; get; }

8.

public string Address { get; set; }

9.

public string PhoneNumber { set; get; }

10. }

Output

Or:
1. using System;
2. using System.Linq;
3. using System.Web;
4.
5.

/// <summary>

6.

/// Summary description for Business_Object

7.

/// </summary>

8.

public class Business_Object

9.

10.

//Declared Registration Variables

11.

private string user_name;

12.

private string password;

13.

private string qualification;

14.

private string age;

15.

private string mobile;

16.

//use propertis concepts in C#,to get and set the value in variables

17.

18.

public string Username_value

19.

20.

get

21.

22.

return user_name;

23.

24.

set

25.

26.

user_name = value;

27.
28.

}
}

29.
30.

public string pass_value

31.

32.

get

33.

34.

return password;

35.

36.

set

37.

38.

password = value;

39.
40.

}
}

41.
42.

public string Qualification_value

43.

44.

get

45.

46.

return qualification;

47.

48.

set

49.

50.

qualification = value;

51.
52.

}
}

53.
54.

public string Age_value

55.

56.

get

57.

58.

return age;

59.

60.

set

61.

62.

age= value;

63.
64.

}
}

65.
66.

public string mobile_value

67.

68.

get

69.

70.

return mobile;

71.

72.

set

73.

74.

mobile = value;

75.
76.
77. }

}
}

S-ar putea să vă placă și