The blog has moved to the new site F10Debug.com

Monday, April 4, 2011

Three tier architecture in asp.net





Three Tier Architecture in Asp.net
      

       3-Tier architecture is client server architecture consists of three main layers which are separate from each other.
  
  • Presentation Layer: Contains actual display of pages like Html and .aspx etc, where data is presented to user or input is taken from user.   
  • Business Access Layer (BAL): Also called as Business logic layer, Contains business logic validations or calculations related with the data. 
  • Data Access Layer (DAL): It contains methods that helps BAL to connect to database and perform required action e.g.: insert, update, delete etc. 

-->
Implementation of three tier architecture:      
 
-->
I have created one project as shown above name as 3-tier. For implementation of three tier architecturewe need to separate all three layers,  so I have created business layer (AddBAL.cs) , Data access layer (AddDAL.cs) and presentation layer (Default.aspx) separately. We need to just reference BAL in presentation layer.
-->
Data Access Layer (DAL) :
AddDAL.cs
using System;
using System.Data;
using System.Configuration;
public class AddDAL

    {

        string conStr = ConfigurationManager.ConnectionStrings
                        ["ConnectionStringName"].ToString();

       // Add records into database

        public int AddMethod(string fName, string lName)

        {

            SqlConnection conn = new SqlConnection(connStr);

            conn.Open();

            SqlCommand Cmd = new SqlCommand("AddData", conn);


Cmd.CommandType = CommandType.StoredProcedure;

            try

            {

                Cmd.Parameters.AddWithValue("@fName", fName);

                Cmd.Parameters.AddWithValue("@lName", lName);

                return Cmd.ExecuteNonQuery();

            }

            catch

            {

                throw;

            }

            finally

            {

                Cmd.Dispose();

                conn.Close();

                conn.Dispose();

            }
        } 
-->
Business Access Layer (BAL):



AddBAL.cs
 
using System;


using System.Data;


using System.Configuration;


using System.Web;


    // Summary description for PersonBAL3
 
    public class AddBAL
    {
        // Add records into database

        public int AddMethod(string fName, string lName)
        {

            AddDAL ObjAdd = new AddDAL ();
            try
            {
                return ObjAdd. AddMethod (fName, lName);
            }
            catch
            {
                throw;
            }

            finally
            {
 
                ObjAdd = null;
 
              }           }   }
 
-->
Presentation Layer :  
Default.aspx 
 
Default.aspx.cs
protected void AddRecords(object sender, EventArgs e)

    {
        int intTotal = 0;

        //insert records

        AddBAL ObjBAL = new AddBAL ();

        string firstName = txtFName.Text;

        string lastName = txtLName.Text;
        
        try
        {
            intTotal = ObjBAL.AddMethod(fName, lName)
         }

        catch (Exception Ex)

        {

            lblMessage.Text = Ex.Message.ToString();

        }

        finally

        {

            ObjBAL = null;

        }        
    }
-->

        19 comments:

        1. Simple & Grateful for beginers.
          Thank you.

          Kajan

          ReplyDelete
        2. really very simple and usefull...
          one doubt: what I have to give in the place "AddData" in AddDal. Is it SQL command? please provide an eg. Thanks in advance.

          ReplyDelete
        3. one more doubt is
          you have given
          intTotal = ObjBAL.AddMethod(fName, lName)
          It is firstname and last name instead of fName and lName respectively right?
          You never used fName and lName in the code anywhere?

          ReplyDelete
        4. since I am new in dot net can you pls write simple stored procedure also?
          thanks in advance.

          ReplyDelete
        5. ok i will write on stored proc, give me some time....

          ReplyDelete
        6. when I create AddBAL.cs class, the below line willbe created automatically
          public class AddBAL
          {
          public AddBAL()
          {
          //
          // TODO: Add constructor logic here
          //
          }
          }

          where should i write this code this AddMethod function.

          public int AddMethod(string firstName, string lastName)
          {
          AddDAL objDAL = new AddDAL();
          try
          {
          return objDAL.AddMethod(firstName, lastName);
          }
          catch
          {
          throw;
          }
          finally
          {
          objDAL = null;
          }
          }
          in
          public class AddBAL directly or inside public AddBAL() ??
          ..

          Please write a description...

          ReplyDelete
        7. public class AddBAL
          {
          //this is constructor which creates every time when you create class....it is use to define or declare variable of class...
          public AddBAL()
          {
          //
          // TODO: Add constructor logic here
          //
          }

          public int AddMethod(string firstName, string lastName)
          {
          AddDAL objDAL = new AddDAL();
          try
          {
          return objDAL.AddMethod(firstName, lastName);
          }
          catch
          {
          throw;
          }
          finally
          {
          objDAL = null;
          }
          }

          ReplyDelete
        8. before writing the stored procedure..could you please write another way to write data in access database.
          I created one access database with one table 'tab_Name' with two fields fname and lname.
          I used sql connection to connect access db. can you pls modify the DAL according to that.

          ReplyDelete
        9. This is not for inserting data...it is just example of how to use three tier architecture.

          ReplyDelete
        10. please add the tables or stored procedures what you created to this program...

          ReplyDelete
        11. When i try to deploy this 3-Tier application,i'm getting error at bal object that is created inside of code behind page.S0, please give me solution for this.send me solution to 'info2smanohar@gmail.com'

          ReplyDelete
        12. Thank you very much for the example. I tried some logic in my application and it worked fine. Whatever I have found all over the net were a little bit complex. This is a way too easy for a beginner. Thanks a lot Jatin..

          ReplyDelete
        13. thanks for a fine working code for a beginner..

          ReplyDelete

        14. It is really a great work and the way in which u r sharing the knowledge is excellent.
          Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article. Dot Net Training in chennai | Dot Net Training institute in velachery

          ReplyDelete


        15. It is really a great work and the way in which u r sharing the knowledge is excellent.
          Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article. dot net training and placement in chennai | dot net training institute in velachery

          ReplyDelete
        16. This comment has been removed by the author.

          ReplyDelete
        17. "Great blog created by you. I read your blog, its best and useful information. You have done a great work. Super blogging and keep it up.php jobs in hyderabad.
          "

          ReplyDelete