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

This article explains how to pass data across layers in a uniform fashion using an entity class. We know that tiered architecture is a very popular architecture in the software development field. So, we will create a different layer to meet our business needs and to follow the separation of concerns in software development.

The next question is, how will we pass data across layers? The answer is not simple. There are many ways and patterns to follow for this problem. In this article, we will see how to pass data across layers uniformly using an entity class.

What is Entity class?

Obviously, it is a valid question. Entities are nothing but our business objects. For example, we want to save some information of a patient in an EMR application, to do that we can create a patient entity. It's nothing but a normal C# class containing information about a patient and functions to manipulate the patient.

Let's create one simple entity class

We have just not explained that entitles are nothing but classes. Let's create a Person Entity. Have a look at the following code.

  1. public class Person  
  2. {  
  3.     public Int16 ID { getset; }  
  4.     public string name { getset; }  
  5.     public string surname { getset; }  
  6. } 

This is a very simple example of an entity class. It contains three properties called ID, name and surname.

We will now create a simple 3-Tier architecture project and we will transfer an object of the entity class above from one layer to another layer.

Create Data Access Layer

Let's start with the bottom-most layers. We will create a Data Access Layer that will read and insert data in a SQL Server database. Have a look at the following code.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.Data.SqlClient;  
  5. using WindowsFormsApplication1.DTO;  
  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.         List<Person> List = new List<Person>();  
  14.         Person objP = null;  
  15.         public List<Person> Read()  
  16.         {  
  17.             con.ConnectionString = ConString;  
  18.             if (ConnectionState.Closed == con.State)  
  19.                 con.Open();  
  20.             SqlCommand cmd = new SqlCommand("select * from Person",con);  
  21.             try  
  22.             {  
  23.                 SqlDataReader rd = cmd.ExecuteReader();  
  24.                 while (rd.Read())  
  25.                 {  
  26.                     objP = new Person();  
  27.                     objP.ID = Convert.ToInt16(rd.GetValue(0));  
  28.                     objP.name = rd.GetString(1);  
  29.                     objP.surname = rd.GetString(2);  
  30.                     List.Add(objP);  
  31.                 }  
  32.                 return List;  
  33.             }  
  34.             catch  
  35.             {  
  36.                 throw;  
  37.             }  
  38.         }  
  39.         public List<Person> Read(Int16 Id)  
  40.         {  
  41.             con.ConnectionString = ConString;  
  42.             if (ConnectionState.Closed == con.State)  
  43.                 con.Open();  
  44.             SqlCommand cmd = new SqlCommand("select * from Person where ID= "+ Id +"", con);  
  45.             try  
  46.             {  
  47.                 SqlDataReader rd = cmd.ExecuteReader();  
  48.                 while (rd.Read())  
  49.                 {  
  50.                     objP = new Person();  
  51.                     objP.ID = rd.GetInt16(0);  
  52.                     objP.name = rd.GetString(1);  
  53.                     objP.surname = rd.GetString(2);  
  54.                     List.Add(objP);  
  55.                 }  
  56.                 return List;  
  57.             }  
  58.             catch  
  59.             {  
  60.                 throw;  
  61.             }  
  62.         }  
  63.         public Int32 Insert(Person tmp)  
  64.         {  
  65.             try  
  66.             {  
  67.                 con.ConnectionString = ConString;  
  68.                 if (ConnectionState.Closed == con.State)  
  69.                     con.Open();  
  70.                 SqlCommand cmd = new SqlCommand("INSERT INTO Person VALUES (@name,@surname)", con);  
  71.                 cmd.Parameters.AddWithValue("@name", tmp.name);  
  72.                 cmd.Parameters.AddWithValue("@surname", tmp.surname);  
  73.                 return cmd.ExecuteNonQuery();  
  74.             }  
  75.             catch  
  76.             {  
  77.                 throw;  
  78.             }  
  79.         }  
  80.     }  
  81. } 

Here we have implemented two overloaded functions to read the data. One will dump all data from the table and another will fetch information of a specific person. The Insert function will insert a single set of information into the database.

Note that the return type of the function or argument of the Insert method is an object of the entity class. Within the read function we are reading data from the database object and transforming it to a business object.

Create Business Logic Layer

We will now create a Business Logic Layer that will communicate with the both User Interface layer (Presentaion Layer) and Data Access Layer. Have a look at the following code.

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

The functions are very simple, the GetPerson() function will return a list of entity objects and the SavePerson() function will transfer an object of the entity class to the Data Access Layer. So, overall all the functions are transferring an object of the entity class across layers.

Create Presentation Layer

This is a high-level layer where the user will provide input to and get output from the system. Create one simple Windows Forms form as in the following.

Write the following code in the button's click event and the form's load event.

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

In the form's load event data will be returned in the form of a list and we are directly assigning the list to a grid view object. The save function will send data to a business object in the form of an entity.

Here is our project structure:

PassData1.jpg

Here is the output screen, by default the data will be loaded from the database.

PassData2.jpg

Let's try to insert new data.

PassData3.jpg

After the save:

PassData4.jpg

Ok, it has been saved.

Conclusion

In this article, we learned how to pass data in a layered architecture in a uniform fashion. Hope you have understood the concept. In a future article, we will see a few more ways to pass data in a layered architecture.


Similar Articles