ARTICLE

Extend 3 Layer ASP.NET Application to 4 Layer to Achieve Higher Level Abstraction

Posted by Sukesh Marla Articles | ASP.NET Programming August 19, 2012
In this article we will understand the meaning of letter N in N-Layer applications and the concept of one of the OOPS pillar Abstraction.
Reader Level:

In this article we will understand the meaning of letter N in N-Layer applications and the concept of one of the OOP pillar Abstractions.

OOP - Abstraction

Have you ever tried to find how a PC starts when you press the power button, such as how the fan starts; my big question is whether anytime you cared about such things.

Except from some crazy people (obviously I am one of them) most people say NO. The reason is there are much more important tasks in your life than exploring these things.

This is called Abstraction

Didn't understand

In Simple Words hiding actual implementation, means all the unnecessary things are hidden from the end user.

LayrApp1.jpg

As I said in my earlier article about ASP.Net sessions and OOP, OOP exists everywhere; we just have to think the right way.

Now consider a project manager with some of his team members that want to build a stock market project where the best stock values will be displayed on the screen.

Now, how to start? It's the big question.

  • Stock data will be collected from various services (some made by internal developer and some by external vendors).
  • Write an algorithm to calculate the best stock value.
  • Display the value in the screen.

LayrApp2.jpg

Now it seems very easy. In part 1 we collected, in part 2 we build our business logic, and finally in part 3 we displayed.

And while working on each layer we were not at all required to care about the other 2 layers.

What we learned

As we divide the system into smaller logical parts, the system becomes simpler and easy to understand, therefore easy to develop.

Now according to the complexity of the system we may divide it into any number of logical units. That's where the letter N comes from.

In my previous article about 3-Tier architecture using ASP.Net we learned how to break a complex system into layers to make code management much easier.

Please go though that article for an example before moving any further.

There we had 3 Layers, as in:

LayrApp3.jpg

Now if you think or notice properly then the CustomerDAL has two logics or functionalities; they are:

  1. Communication with SQL Server using SQL Classes.
  2. Creating queries and mapping data to queries to perform actions.

Now as the application grows, as we go on adding new options like say Order, Supplier and so on, we find that the first part remains the same for all, such as database communication logic. So it will be better if we make a 4th layer which will act as the database communicator and by that we achieve 2 things:

  1. Reusability
  2. We are not required to concentrate towards the database connectivity logic after that.

Let's take a code at the new layer we developed; see:

public class SqlHelper

{

          private const string ConnectionString = @"Data Source=SUKESH-PC\S2;Initial Catalog=3LayerApp;Persist Security Info=True;User ID=sa;Password=aa";

 

          public static DataTable GetDataTable(string Query,Dictionary<string,Object> QueryParameters)

          {

                   using(SqlConnection objConnection = new SqlConnection(SqlHelper.ConnectionString))

                   {

                             SqlCommand objSelectCommand = objConnection.CreateCommand();

                             objSelectCommand.CommandText = Query;

                             if(QueryParameters != null)

                             {

                                      foreach(string parameter in QueryParameters.Keys)

                                      {

                                                objSelectCommand.Parameters.AddWithValue(parameter, QueryParameters[parameter]);

                                      }

                             }

                             SqlDataAdapter objAdapter = new SqlDataAdapter(objSelectCommand);

                             DataTable dtCustomer = new DataTable();

                             objAdapter.Fill(dtCustomer);

                             return dtCustomer;

                   }

          }

          public static int ExecuteNonQuery(string Query, Dictionary<string, Object> QueryParameters)

          {

                   using(SqlConnection objConnection = new SqlConnection(SqlHelper.ConnectionString))

                   {

                             SqlCommand objSelectCommand = objConnection.CreateCommand();

                             objSelectCommand.CommandText = Query;

 

                             if(QueryParameters != null)

                             {

                                      foreach(string parameter in QueryParameters.Keys)

                                      {

                                                objSelectCommand.Parameters.AddWithValue(parameter, QueryParameters[parameter]);

                                      }

                             }

 

                             objConnection.Open();

                             return objSelectCommand.ExecuteNonQuery();

                   }

          }

}

Now in these options I am not required to be concerned with database connections, especially about connection close issues. What a great relief.

You can download the sample code attached for demonstration.

You can also download reusable DataAccessLayer for your applications here.

Summary

We learned how we can implement abstraction in any application to make code and logic management much easier, also we learned how we can extend a 3-layer application to 4-layer.

Hope you enjoyed reading this article.
 

COMMENT USING
Employers - Post Free Jobs