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.

using System;

using System.Data;

using System.Drawing;

using System.Windows.Forms;

using WindowsFormsApplication1.BLL;

using WindowsFormsApplication1.DTO;

 

namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            try

            {

                PersonBLL p = new PersonBLL();

                this.dataGridView1.DataSource = p.GetPersons(Convert.ToInt16(this.txtID.Text));

            }

            catch

            {

                MessageBox.Show("Error Occurred");

            }

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            try

            {

                PersonBLL p = new PersonBLL();

                this.dataGridView1.DataSource = p.GetPersons();

            }

            catch

            {

                MessageBox.Show("Error Occurred");

            }

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            //Save Data

            PersonBLL objBLL = new PersonBLL();

            try

            {

                if (objBLL.SavePerson(this.txtname.Text, this.txtsurname.Text))

                {

                    MessageBox.Show("Data Saved successfully");

                    Form1_Load(null, null);

                }

            }

            catch

            {

                MessageBox.Show("Exception occurred");

            }

        }

    }

}

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.
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Data;

using WindowsFormsApplication1.DAL;

using WindowsFormsApplication1.DTO;

 

namespace WindowsFormsApplication1.BLL

{

    public class PersonBLL

    {

        public DataTable GetPersons()

        {

            try

            {

                PersonDAL objdal = new PersonDAL();

                return objdal.Read();

            }

            catch

            {

                throw;

            }

        }

        public DataTable GetPersons(Int16 ID)

        {

            try

            {

                PersonDAL objdal = new PersonDAL();

                return objdal.Read(ID);

            }

            catch

            {

                throw;

            }

        }

        public Boolean SavePerson(string Name, String Surname)

        {

            try

            {

                PersonDAL objDal = new PersonDAL();

                return objDal.Insert(Name, Surname) > 0 ? true : false;

            }

            catch

            {

                throw;

            }

        }

    }

}

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.

using System;

using System.Text;

using System.Threading.Tasks;

using System.Data;

using System.Data.SqlClient;

 

namespace WindowsFormsApplication1.DAL

{

    public class PersonDAL

    {

        public string ConString = "Data Source=SOURAV-PC\\SQL_INSTANCE;Initial Catalog=test;Integrated Security=True";

        SqlConnection con = new SqlConnection();

        DataTable dt = new DataTable();

        public DataTable Read()

        {

 

            con.ConnectionString = ConString;

            if (ConnectionState.Closed == con.State)

                con.Open();

            SqlCommand cmd = new SqlCommand("select * from Person", con);

            try

            {

                SqlDataReader rd = cmd.ExecuteReader();

                dt.Load(rd);

                return dt;

            }

            catch

            {

                throw;

            }

        }

 

        public DataTable Read(Int16 Id)

        {

            con.ConnectionString = ConString;

            if (ConnectionState.Closed == con.State)

                con.Open();

            SqlCommand cmd = new SqlCommand("select * from Person where ID= " + Id + "", con);

            try

            {

                SqlDataReader rd = cmd.ExecuteReader();

                dt.Load(rd);

                return dt;

            }

            catch

            {

                throw;

            }

        }

        public Int32 Insert(String name, string Surname)

        {

            try

            {

                con.ConnectionString = ConString;

                if (ConnectionState.Closed == con.State)

                    con.Open();

                SqlCommand cmd = new SqlCommand("INSERT INTO Person VALUES (@name,@surname)", con);

                cmd.Parameters.AddWithValue("@name", name);

                cmd.Parameters.AddWithValue("@surname", Surname);

                return cmd.ExecuteNonQuery();

            }

            catch

            {

                throw;

            }

        }

    }

}

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.

Up Next
    Ebook Download
    View all
    Learn
    View all