3 Tier Architecture in Asp.net Using C Example

This article explains how to create and implement a 3-tier architecture for our project in ASP.Net.

3 tier architecture
3-tier architecture

What is a Layer?

A layer is a reusable portion of code that performs a specific function.

In the .NET environment, a layer is usually set up as a project that represents this specific function. This specific layer is in charge of working with other layers to perform some specific goal.

 Let's briefly look at the latter situation first.

Data Layer

A DAL contains methods that helps the Business Layer to connect the data and perform required actions, whether to return data or to manipulate data (insert, update, delete and so on).

Business Layer

A BAL contains business logic, validations or calculations related to the data.

Though a web site could talk to the data access layer directly, it usually goes through another layer called the Business Layer. The Business Layer is vital in that it validates the input conditions before calling a method from the data layer. This ensures the data input is correct before proceeding, and can often ensure that the outputs are correct as well. This validation of input is called business rules, meaning the rules that the Business Layer uses to make "judgments" about the data.

Presentation Layer

The Presentation Layer contains pages like .aspx or Windows Forms forms where data is presented to the user or input is taken from the user. The ASP.NET web site or Windows Forms application (the UI for the project) is called the Presentation Layer. The Presentation Layer is the most important layer simply because it's the one that everyone sees and uses. Even with a well structured business and data layer, if the Presentation Layer is designed poorly, this gives the users a poor view of the system.

Advantages of a 3-Tier Architecture

The main characteristic of a Host Architecture is that the application and databases reside on the same host computer and the user interacts with the host using an unfriendly dumb terminal. This architecture does not support distributed computing (the host applications are unable to connect to a database of a strategically allied partner). Some managers find that developing a host application takes too long and it is expensive. These disadvantages consequently led to a Client-Server architecture.

A Client-Server architecture is a 2-Tier architecture because the client does not distinguish between Presentation Layer and Business Layer. The increasing demands on GUI controls caused difficulty in managing the mixture of source code from a GUI and the Business Logic (Spaghetti Code). Further, the Client Server Architecture does not support enough the Change Management. Let us suppose that the government increases the Entertainment tax rate from 4% to 8 %, then in the Client-Server case, we need to send an update to each client and they must update synchronously on a specific time otherwise we may store invalid or incorrect information. The Client-Server Architecture is also a burden to network traffic and resources. Let us assume that about five hundred clients are working on a data server. Then we will have five hundred ODBC connections and several ruffian record sets, that must be transported from the server to the clients (because the Business Layer remains in the client side). The fact that Client-Server does not have any caching facilities like in ASP.NET causes additional traffic in the network. Normally, a server has better hardware than the client, therefore it is able to compute algorithms faster than a client, so this fact is also an additional argument in favor of the 3-Tier Architecture. This categorization of the application makes the function more reusable easily and it becomes too easy to find the functions that have been written previously. If a programmer wants to make further updates in the application then he can easily understand the previous written code and can update it easily.

 Let's start creating a 3-Tier Architecture Application.

1. Create a new  project  using "File" -> "New" -> "Project...".

New Project

2. In Visual C# select "Web".

(Select "ASP.NET Web application" and name it's as: ThreeTierApp)

ASPNET Web application
3. How to add class library to solution:

Add Class Library
After clicking on a new project you would see the following screen.

  • Select "Class Library" from this and name it "BussinessObject".

    Select Class Library

  • Provide the name of the Class library as "BussinessObject".

    Class library as BussinessObject

  • Add another  Class Library to our project.

     (In the same way as you added a BussinessObject.)

  • Name the Class library "Bussinesslogic".
  • After adding you will see as in this view.

    Adding your solution

  • Add the last Class Library  to our  project called "Data Access Layer".
  • In the same way as you added BussinessObject.
  • Name the Class Library "DataAccess".

Class library to DataAccess

After adding, your solution would look like this:

Adding your solution

 Now we are done. Add all layers to our project.

 You can see the three tiers in the image given below.

three tiers

 Basically a 3-Tier architecture contains the following 3 layers:

  1. Application Layer or Presentation Layer  (our web form and UI Part)
  2. Business Logic Layer (Bussinesslogic)
  3. Data Access Layer (DataAccess)

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

Presentation Layer

 Here, I have designed a user interface for the Presentation Layer.

User Interface
Here is the design of "Userregistration.aspx":

  1. < html xmlns = "http://www.w3.org/1999/xhtml" >
  2. < head id = "Head1" runat = "server" >
  3. < title > </ title >
  4. </ head >
  5. < body >
  6. < form id = "form1" runat = "server" >
  7. < div style = "margin: 0px auto; padding-left: 370px; padding-right: 30px;overflow: auto;" >
  8. < div >
  9. < table width = "50%" >
  10. < tr >
  11. < td colspan = "2" style = "background-color: Green; height: 30px;color: White;" align = "center" >
  12.                         User Registration
  13. </ td >
  14. </ tr >
  15. < tr >
  16. < td >  Name </ td >
  17. < td >
  18. < asp:TextBox ID = "txtname" Width = "150px" runat = "server" > </ asp:TextBox >
  19. </ td >
  20. </ tr >
  21. < tr >
  22. < td >
  23.                         Address</ td >
  24. < td >
  25. < asp:TextBox ID = "txAddress" Width = "150px" runat = "server" > </ asp:TextBox >
  26. </ td >
  27. </ tr >
  28. < tr >
  29. < td >  EmailID </ td >
  30. < td >
  31. < asp:TextBox ID = "txtEmailid" Width = "150px" runat = "server" > </ asp:TextBox >
  32. </ td >
  33. </ tr >
  34. < tr >
  35. < td >  Mobile Number </ td >
  36. < td >
  37. < asp:TextBox ID = "txtmobile" Width = "150px" runat = "server" > </ asp:TextBox >
  38. </ td >
  39. </ tr >
  40. < tr >
  41. < td align = "center" colspan = "2" >
  42. < asp:Button ID = "BtnSave" runat = "server" Width = "100px" Text = "Save" OnClick = "BtnSave_Click" />
  43. </ td >
  44. </ tr >
  45. </ table >
  46. </ div >
  47. </ div >
  48. </ form >
  49. </ body >
  50. </ html >

Now let's start to create a table for saving this data using our 3-Tier Architecture.

  1. CREATE table  Userinfo
  2. business
  3. idbigint primary key not null  identity(1,1),
  4. Name  Nvarchar(50),
  5. Address Nvarchar(100),
  6. EmailID Nvarchar(50),
  7. Mobilenumbervarchar (10)
  8. )

Let's start with a business object  first, as in the following:

 Delete Class Class1

 Create a new class name, "UserBO".

Create New class

  Creating UserBO.cs

Then declare variables in UserBO as in the following:

  1. using  System;
  2. using  System.Collections.Generic;
  3. using  System.Linq;
  4. using  System.Text;
  5. namespace  BussinessObject
  6. {
  7.    Publicclass  UserBO
  8.     {
  9. private string  _Name;
  10. private string  _Address;
  11. private string  _EmailID;
  12. private string  _Mobilenumber;
  13. public string  Name
  14.         {
  15. get
  16.             {
  17. return  _Name;
  18.             }
  19. set
  20.             {
  21.                 _Name = value;
  22.             }
  23.         }
  24. public string  address
  25.         {
  26. get
  27.             {
  28. return  _Address;
  29.             }
  30. set
  31.             {
  32.                 _Address = value;
  33.             }
  34.         }
  35. public string  EmailID
  36.         {
  37. get
  38.             {
  39. return  _EmailID;
  40.             }
  41. set
  42.             {
  43.                 _EmailID = value;
  44.             }
  45.         }
  46. public string  Mobilenumber
  47.         {
  48. get
  49.             {
  50. return  _Mobilenumber;
  51.             }
  52. set
  53.             {
  54.                 _Mobilenumber = value;
  55.             }
  56.         }
  57.     }
  58. }

Now in the same way as we created UserBO, create  a new  class, UserDA, in DataAccess.

Create New Class UserDA

  • In Dataaccess
  • We will use this layer for communicating with the database
  • All operations (insert , update, delete and selecting records) for the database is done in this layer.

Now in the same way as we created UserDA:

Create  New Class UserBL.cs  in ( Bussinesslogic )

Create New Class UserBL

  The main thing TO DO

The main thing to do nest is to add the three layers:

  • UserBO.cs
  • UserBL.cs
  • UserDA.cs

But they are not inter connected to each other.

Let's connect them.

DataAccess connections

First right-click on DataAccess then:

  • Click the "Add Reference" tab
  • Select "Project"
  • Inside that you will see all Layer Name
  • Select BussinessObject from that and click "Ok"

Bussinesslogic connections

Then right-click on Bussinesslogic then:

  • Click the "Add Reference" tab
  • Select "Project"
  • Inside that you will see all Layer Name.
  • Select DataAccess from that and click "Ok"

New rebuild all layers.

  Last step

  • Right-click on the project and select "Add references"
  • Click the "Add Reference" tab
  • Select "Project"
  • Inside that you will see all Layer Name.
  • Select all add thn click "Ok"

Now build the project.

UserDA.cs (adding Records)

  1. using  System;
  2. using  System.Collections.Generic;
  3. using  System.Linq;
  4. using  System.Text;
  5. using  System.Data;
  6. using  System.Data.SqlClient;
  7. using  System.Configuration;
  8. using  BussinessObject;
  9. namespace  DataAccess
  10. {
  11. public class  UserDA
  12.     {
  13.           SqlConnection con =new  SqlConnection(ConfigurationManager.ConnectionStrings[ "Myconstr" ].ToString());
  14. public int  AddUserDetails(UserBO ObjBO)
  15.         {
  16. try
  17.             {
  18.              SqlCommand cmd =new  SqlCommand( "sprocUserinfoInsertUpdateSingleItem" , con);
  19.                 cmd.CommandType = CommandType.StoredProcedure;
  20.                 cmd.Parameters.AddWithValue("@Name" , ObjBO.Name);
  21.                 cmd.Parameters.AddWithValue("@Address" , ObjBO.address);
  22.                 cmd.Parameters.AddWithValue("@EmailID" , ObjBO.EmailID);
  23.                 cmd.Parameters.AddWithValue("@Mobilenumber" , ObjBO.Mobilenumber);
  24.              con.Open();
  25. int  Result = cmd.ExecuteNonQuery();
  26.              cmd.Dispose();
  27. return  Result;
  28.             }
  29. catch
  30.             {
  31. throw ;
  32.             }
  33.         }
  34.     }
  35. }

UserBL.cs

  1. using  System;
  2. using  System.Collections.Generic;
  3. using  System.Linq;
  4. using  System.Text;
  5. using  DataAccess;
  6. using  BussinessObject;
  7. namespace  Bussinesslogic
  8. {
  9. public class  UserBL
  10.     {
  11. public int  SaveUserregisrationBL(UserBO objUserBL)
  12.         {
  13. try
  14.             {
  15.                 UserDA objUserda =new  UserDA();
  16. return  objUserda.AddUserDetails(objUserBL);
  17.             }
  18. catch
  19.             {
  20. throw ;
  21.             }
  22.         }
  23.     }
  24. }

Final Output

Output

  1. SELECT  * FROM  Userinfo

Database Table

3 Tier Architecture in Asp.net Using C Example

Source: https://www.c-sharpcorner.com/UploadFile/4d9083/create-and-implement-3-tier-architecture-in-Asp-Net/

0 Response to "3 Tier Architecture in Asp.net Using C Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel