3-Tier Architecture In Asp.net Using C#.

Here we will see how to implement three tier architecture(also known as n-tier architecture) in asp.net application.Three tier architecture is a well known architecture in software development and you can implement it in web based as well desktop applications.

Three tier architecture basically means dividing your project into three layers

1) User Interface Layer [UI] (Also known as Application Layer or Presentation Layer)

2) Business Access Layer [BAL] (Also known as Business Logic Layer)

3) Data Access Layer [DAL]

The advantage of this functionality is it makes a logical separation of components so if in near future we want to replace / change our database we need to change only data layer or if we need to change user interface we need to change only user interface layer and rest remains the same.

It possible for the parallel development of each tier as they work independently. It also provides a security layer for our database as it cannot be directly accessed through Application Layer. Below is the diagram which will provide you a better picture of how actually three tier application works.

L

Here we have done a small example to understand the three tier architecture.

[ Note : This is only a simple example for learning and understanding how the three tier architecture works and functions. You can modify the code as per your requirement.]

The following figure shows the 3-tier architecture of our application that we are going to make.

solutionexplorer

Step 1) Create a Asp.net Web Application project [Three_Tier_Application] for our presentation layer.

Step 2) Create a simple page in the application where we will have our User Interface. Here we have created a simple form where the end user will be able to register himself. We will see the code bind file later(Step 6) once we are done with other layers.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <table>
            <tr>
                <td colspan="2" align="center" bgcolor="lightblue">User Registration
                </td>
            </tr>
            <tr>
                <td>Name
                </td>
                <td>
                    <asp:TextBox ID="txtname" Width="150px" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Address
                </td>
                <td>
                    <asp:TextBox ID="txAddress" Width="150px" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>EmailID
                </td>
                <td>
                    <asp:TextBox ID="txtEmailid" Width="150px" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Mobile Number
                </td>
                <td>
                    <asp:TextBox ID="txtmobile" Width="150px" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2">
                    <asp:Button ID="BtnSave" runat="server" Width="100px" Text="Save" OnClick="BtnSave_Click" />
                </td>
            </tr>
             <tr>
                <td align="center" colspan="2">
                    <asp:Literal runat="server" ID="ltr_msg"></asp:Literal>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

Step 3) Create new project [BusinessObject] for the database object model. Add a class
(UserBO.cs) which will basically hold the getter and setter values for the respective tables.

public class UserBO
    {
        private string _Name;
        private string _Address;
        private string _EmailID;
        private string _Mobilenumber;

        // Get and set values

        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }

        public string address
        {
            get
            {
                return _Address;
            }

            set
            {
                _Address = value;
            }
        }

        public string EmailID
        {
            get
            {
                return _EmailID;
            }

            set
            {
                _EmailID = value;
            }
        }

        public string Mobilenumber
        {

            get
            {
                return _Mobilenumber;
            }
            set
            {
                _Mobilenumber = value;
            }
        }
    }

Step 4) Create new project [DataAccess] to connect business layer to your database. Add a class (UserDA.cs) which will hold all the data manipulations methods. Here we have created a simple method for registering a new customer.

[Note : You need to provide references to other project in order to use their functionality]
References

Business logic reference

[Note : Import Business Object Layer Namespace in Data Access Layer]

 public class UserDA
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Myconstr"].ToString());

        public int AddUserDetails(UserBO ObjBO) // pass your business object here 
        {
            try
            {
                SqlCommand cmd = new SqlCommand("sprocUserinfoInsertUpdateSingleItem", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Name", ObjBO.Name);
                cmd.Parameters.AddWithValue("@Address", ObjBO.address);
                cmd.Parameters.AddWithValue("@EmailID", ObjBO.EmailID);
                cmd.Parameters.AddWithValue("@Mobilenumber", ObjBO.Mobilenumber);
                con.Open();
                int Result = cmd.ExecuteNonQuery();
                cmd.Dispose();
                return Result;
            }
            catch
            {
                throw;
            }
            finally
            {
                con.Close();                
            }
        }
    }

Step 5) Create new project [Businesslogic] for specifying all business rules that needs to be satisfied before accessing the data layer. Add a class(UserBL.cs) which validate the data and call the data access methods for data manipulation.

Business logic reference

[Note : Import Business Object Layer and Data Access Layer Namespace in Business Logic Layer]

 public class UserBL
    {
        public int SaveUserregisrationBL(UserBO objUserBL) // Business Object
        {
            try
            {
                UserDA objUserda = new UserDA(); // Data access object

                return objUserda.AddUserDetails(objUserBL); // Data access method call
            }
            catch
            {
                throw;
            }
        }
    }

Step 6) Once all the layers are created we will write to code in our presentation layer.

All Layer references

[Note : Import Business Object Layer and Business Logic Layer Namespace in Presentation Layer]

 protected void BtnSave_Click(object sender, EventArgs e)
    {
        UserBO ObjUBO = new UserBO(); //Create business object and assign values to it.
        ObjUBO.Name = txtname.Text;
        ObjUBO.address = txAddress.Text;
        ObjUBO.EmailID = txtEmailid.Text;
        ObjUBO.Mobilenumber = txtmobile.Text;

        UserBL objUBL = new UserBL(); // Create business logic object and call the required method.

        int result = objUBL.SaveUserregisrationBL(ObjUBO);

        if (result > 0)
        {
            ltr_msg.Text = "<font color='green'>Data saved successfully...!</font>";            
        }
        else
        {
            ltr_msg.Text = "<font color='red'>Failed</font>";
        }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s