Pass Data in Layered Architecture: Part-2: Non Uniform Style

Welcome to Pass Data in Layered Architecture article series, in this article we are explaining various approaches to transfer data from layer to layer in a Layered Architecture. In our previous article we saw how to pass data in a uniform fashion using the Business Object class. You may visit it here.

Pass Data in Layered Architecture: Part -1: Uniformly Using Entity Class

This article explains how to transfer data in a non-uniform fashion. For example if we have a 3-layer architecture then we will transfer data non-uniformly from the User Interface to the Business Layer and the Business Layer to the Data Access Layer.

Here is our diagram.

im1.jpg

Ok, let's create a small Windows application step-by-step. At first we will create a folder structure in an empty Windows application. Here is our folder structure.

im2.jpg

Here we have created three layers, the User Interface Layer (Windows Forms form), Business Logic Layer (BLL) and Data Access Layer (DAL). Now let's start to write code.

Create Presentation Layer

This is the layer where the user will interact with the system. Let's create a Windows Forms form and drag and drop a few controls like below.

im3.jpg

Here is the code for the user interface.

  1. using System;  
  2. using System.Data;  
  3. using System.Drawing;  
  4. using System.Windows.Forms;  
  5. using WindowsFormsApplication1.BLL;  
  6. using WindowsFormsApplication1.DTO;  
  7. namespace WindowsFormsApplication1  
  8. {  
  9.     public partial class Form1 : Form  
  10.     {  
  11.         public Form1()  
  12.         {  
  13.             InitializeComponent();  
  14.         }  
  15.         private void button1_Click(object sender, EventArgs e)  
  16.         {  
  17.             try  
  18.             {  
  19.                 PersonBLL p = new PersonBLL();  
  20.                 this.dataGridView1.DataSource = p.GetPersons(Convert.ToInt16(this.txtID.Text));  
  21.             }  
  22.             catch  
  23.             {  
  24.                 MessageBox.Show("Error Occurred");  
  25.             }  
  26.         }  
  27.         private void Form1_Load(object sender, EventArgs e)  
  28.         {  
  29.             try  
  30.             {  
  31.                 PersonBLL p = new PersonBLL();  
  32.                 this.dataGridView1.DataSource = p.GetPersons();  
  33.             }  
  34.             catch  
  35.             {  
  36.                 MessageBox.Show("Error Occurred");  
  37.             }  
  38.         }  
  39.         private void button2_Click(object sender, EventArgs e)  
  40.         {  
  41.             //Save Data  
  42.             PersonBLL objBLL = new PersonBLL();  
  43.             try  
  44.             {  
  45.                 if (objBLL.SavePerson(this.txtname.Text, this.txtsurname.Text))  
  46.                 {  
  47.                     MessageBox.Show("Data Saved successfully");  
  48.                     Form1_Load(nullnull);  
  49.                 }  
  50.             }  
  51.             catch  
  52.             {  
  53.                 MessageBox.Show("Exception occurred");  
  54.             }  
  55.         }  
  56.     }  
  57. } 

So, we are seeing that when the data is saved it is passed through the function as a parameter. And when it is loaded the data is provided as a database object here a DataTable). Now we will implement the Business Logic Layer.

Create Business Logic Layer

Let's create a PersonBAL.cs file and use the following code into it.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Data;  
  7. using WindowsFormsApplication1.DAL;  
  8. using WindowsFormsApplication1.DTO;  
  9. namespace WindowsFormsApplication1.BLL  
  10. {  
  11.     public class PersonBLL  
  12.     {  
  13.         public DataTable GetPersons()  
  14.         {  
  15.             try  
  16.             {  
  17.                 PersonDAL objdal = new PersonDAL();  
  18.                 return objdal.Read();  
  19.             }  
  20.             catch  
  21.             {  
  22.                 throw;  
  23.             }  
  24.         }  
  25.         public DataTable GetPersons(Int16 ID)  
  26.         {  
  27.             try  
  28.             {  
  29.                 PersonDAL objdal = new PersonDAL();  
  30.                 return objdal.Read(ID);  
  31.             }  
  32.             catch  
  33.             {  
  34.                 throw;  
  35.             }  
  36.         }  
  37.         public Boolean SavePerson(string Name, String Surname)  
  38.         {  
  39.             try  
  40.             {  
  41.                 PersonDAL objDal = new PersonDAL();  
  42.                 return objDal.Insert(Name, Surname) > 0 ? true : false;  
  43.             }  
  44.             catch  
  45.             {  
  46.                 throw;  
  47.             }  
  48.         }  
  49.     }  
  50. } 

So, here we are seeing that when the data is obtained from the DataAccess Layer it's provided as a Database object (DataTable) and when data goes to the dipper layer (Data Access Layer) the data is passed through the function as a function parameter. Now we will create a Data Access Layer to process database operation.

Create Data Access Layer

Let's create a Data Access Layer to process the data in the SQLServer database.

  1. using System;  
  2. using System.Text;  
  3. using System.Threading.Tasks;  
  4. using System.Data;  
  5. using System.Data.SqlClient;  
  6. namespace WindowsFormsApplication1.DAL  
  7. {  
  8.     public class PersonDAL  
  9.     {  
  10.         public string ConString = "Data Source=SOURAV-PC\\SQL_INSTANCE;Initial Catalog=test;Integrated Security=True";  
  11.         SqlConnection con = new SqlConnection();  
  12.         DataTable dt = new DataTable();  
  13.         public DataTable Read()  
  14.         {  
  15.             con.ConnectionString = ConString;  
  16.             if (ConnectionState.Closed == con.State)  
  17.                 con.Open();  
  18.             SqlCommand cmd = new SqlCommand("select * from Person", con);  
  19.             try  
  20.             {  
  21.                 SqlDataReader rd = cmd.ExecuteReader();  
  22.                 dt.Load(rd);  
  23.                 return dt;  
  24.             }  
  25.             catch  
  26.             {  
  27.                 throw;  
  28.             }  
  29.         }  
  30.         public DataTable Read(Int16 Id)  
  31.         {  
  32.             con.ConnectionString = ConString;  
  33.             if (ConnectionState.Closed == con.State)  
  34.                 con.Open();  
  35.             SqlCommand cmd = new SqlCommand("select * from Person where ID= " + Id + "", con);  
  36.             try  
  37.             {  
  38.                 SqlDataReader rd = cmd.ExecuteReader();  
  39.                 dt.Load(rd);  
  40.                 return dt;  
  41.             }  
  42.             catch  
  43.             {  
  44.                 throw;  
  45.             }  
  46.         }  
  47.         public Int32 Insert(String name, string Surname)  
  48.         {  
  49.             try  
  50.             {  
  51.                 con.ConnectionString = ConString;  
  52.                 if (ConnectionState.Closed == con.State)  
  53.                     con.Open();  
  54.                 SqlCommand cmd = new SqlCommand("INSERT INTO Person VALUES (@name,@surname)", con);  
  55.                 cmd.Parameters.AddWithValue("@name", name);  
  56.                 cmd.Parameters.AddWithValue("@surname", Surname);  
  57.                 return cmd.ExecuteNonQuery();  
  58.             }  
  59.             catch  
  60.             {  
  61.                 throw;  
  62.             }  
  63.         }  
  64.     }  
  65. }   

It's very clear that we are returning a Database object in a read operation and returning an integer variable in case of an insert operation.

Here is the output screen:

im4.jpg

We can search for a specific Person by providing an ID. Here is the screen for the Insert operation.

im5.jpg

Conclusion

This article has explained how to transfer data across layers in a non-uniform fashion. Hope you have understood the concept. In a future article, we will see a few more styles to pass data across layers.


Similar Articles