3 Tier Architecture Using ASP.NET

3-Tier architecture is one of the most popular approaches in software development.

1.gif

Tier Vs Layer

Tier indicates a physical separation of components.

Here separate assemblies/services are made to represent each component.

Layer indicates a logical separation of components with the help of namespaces and classes

Components of 3-Layer Architecture

  • Presentation Tier: in simple terms, the UI with which the end user actually interacts.
  • Data Tier: Where actual data is stored.
  • Business Tier: It acts a bridge between Data Tier and the Presentation Tier. It collects raw data from the Presentation Tier, checks for validations, converts them to a standard format and finally sends them to the Data Tier. Similarly it collects data from the Data Tier, purifies it and sends it to the Presentation Tier for display.
  • Business Objects: Its nothing but a class representation of actual data.

How it Looks

2.gif

Code Walkthrough

What we are developing.

We are developing a customer screen which supports 2 operations:

  1. Add New Customer
  2. List All Customers.

Now customers may be of the types

Gold, Silver or Normal which represents Id as 1, 2 and 3 respectively.

Note: In the database CustomerTypeId will be stored but in the grid the descriptive text will be shown.

Step 1: Create Business Objects required - we need 2 things,

one representing the customer type and anotehr representing the customer itself.

public class ClsCustomerType
{
       public ClsCustomerType(int id)
       {
             this.Id = id;
             switch(Id)
             {
                    case 0:
                    this.Name = "--Select--";
                    break;
                    case 1:
                    this.Name = "Gold";
                    break;
                    case 2:
                    this.Name = "Silver";
                    break;
                    case 3:
                    this.Name = "Normal";
                    break;
             }
       }
       public int Id
       {
              get;
              set;
        }
        public string Name
        {
              get;
              set;
         }
}
public class ClsCustomer
{
        public string CustomerCode
        {
               get;
               set;
        }
        public string CustomerName
        {
               get;
               set;
        }
        public ClsCustomerType CustomerType
        {
                get;
                set;
         }
    }
}

Step 2: Create Business Access Layer

public class ClsCustomerBAL
{
        public List<ClsCustomerType> LoadCustomerTypes()
        {
               return new List<ClsCustomerType>()
               {                                   
                       new ClsCustomerType(0),new ClsCustomerType(1),new ClsCustomerType(2),new ClsCustomerType(3)
               };
        }       
        public IEnumerable<ClsCustomer> LoadCustomers()
        {
               DataTable dtCustomer=new ClsCustomerDAL().LoadCustomers();
        }                                   
        public void SaveRecord(ClsCustomer objCustomer)
        {
                new ClsCustomerDAL().SaveRecord(objCustomer);
         } 
         public ValidationResult ValidateCustomerCode(string strPriCustomerCode)
         {
                if(strPriCustomerCode.Trim() == string.Empty)
                {
                        return ValidationResult.Empty;
                 }
                 else if(strPriCustomerCode.Trim().Length < 5)
                 {
                         return ValidationResult.InValid;
                  }
                  return ValidationResult.Valid;
         }
}

Step 3:  Next we need a Data Access Layer:

using System.Data;
using System.Data.SqlClient;
using Entities;
namespace DAL
{
      public class ClsCustomerDAL
      {
             private const string ConnectionString = @"Data Source=SUKESH-PC\S2;Initial Catalog=3LayerApp;Persist
Security Info=True;User ID=sa;Password=aa"

             public DataTable LoadCustomers()
             {
                    //Load Data From Database
              }
              public void SaveRecord(ClsCustomer objCustomer)
              {
                    //Save Data Into Database
              }                
       }
}

Step 4: That's it; now the only thing remaining is creation of the UI:

3.gif

In the UI the code behind we will just call the functions created on BAL, there will not be any database logic, and neither wll there be validation logic.

Hope this explanation was enough to understand, how to create a simple 3-Layer application using ASP.Net also I hope you enjoyed reading this article.

Please download the source code to better understand the functionality.

Make sure to put some good comments.


Similar Articles
Just Compile LLP
Just Compile is an IT based firm based out of Mumbai. Application Development, Staffing, Training