3-Tier architecture is one of the most popular approaches in software development.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 classesComponents of 3-Layer Architecture
How it LooksCode WalkthroughWhat we are developing.We are developing a customer screen which supports 2 operations:
Now customers may be of the typesGold, 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 Layerpublic 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;PersistSecurity 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: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.
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: