A simple way to start with N-tier architecture


I want to introduce beginners to the world of professionals. This code is very simple and shows the basic of n-tier architecture. What goes where? is the basic question when a beginner wants to start with n-tier. So here are the answers.

In This article, I want to introduce intermediate .NET developers to the world of professional. ASP.NET development with C#.
N-tier architecture: The simplest explanation of N-tier architecture is the "divide and conquer" approach.

Here is a simple example of n-tier architecture:

Object Layer:
A class which includes properties.

public class Student
{
public string Name {get;set;}
public string City {get;set;}
public int College {get;set;}
}


Data Access Layer:
A Class which interacts with database.

public sealed class StudentDAL
{
Student Student = new Student();
public static int GetCollege(Student)
{
try
{
SQLConnection sqlcon = new SQLConnection(ConfigurationManager
.ConnectionStrings["myDB"]
.ConnectionString);
SQLCommand sqlcmd = new SQLCommand("SELECT college FROM Student_tb WHERE
name=@name AND city=@city",
sqlconn);
sqlcmd.CommandType = CommandType.CommandText;
sqlcmd.Parameters.AddWithValue("@name ", student.name);
sqlcmd.Parameters.AddWithValue("@city", student.city);
sqlcon.Open();
int retval = (int)sqlcmd.ExecuteScalar();
}
catch(Exception ex)
{
//log
}
finally
{
sqlcmd.Dispose();
sqlcon.Close();
sqlcon = null;
}
}
}


Business Logic Layer:
The layer that serves as intermediate for Data Access Layer and Application Layer.

public sealed class StudentBLL
{
public static int GetCollege(string name, string city)
{
return StudentDAL.GetCollege(name, city);
}
}


Application/Presentation Layer:
This layer actual includes user data.

Student Student = new Student();

Student.name= "Prasad Kulkarni";
Student.city = "Aurangabad";

txtCollege.Text = StudentBLL.GetCollege(student);


A n-tier architecture will have processing nodes according to ones need. You can add more layers which your application might be needing. Layers refer to a logical grouping of components which may or may not be physically located on one processing node.
There are many ways to optimize this one. For example, add a Utility Layer wherein you can place your settings/setup of project. In example given above, you can create a function that will return the connection string for your application to communicate with your database server.

In addition, practicing stored procedure is better than using command text. In above example, I have used command text just to show you my query.

By using n-tier you can achieve separation, re-usability and independence.

All the best.!


Comments

Guest Author: Aboli09 Mar 2012

Its absolutely easy and easiest method. Thanks a lot Prasad!

Guest Author: Animesh09 Mar 2012

That is really easy to understand & use.
Thank you sir.

Guest Author: Amol Chatraapati09 Mar 2012

Really Good Article!!

Guest Author: kapil kulkarni09 Mar 2012

Your article is simple & good to learn .It definatly helps to begineer or fresher to know the base of N-tier Architecture.

Author: Siva Prasad20 Mar 2012 Member Level: Gold   Points : 0

nice one prasad kulkarni.

Author: Prasad Kulkarni20 Mar 2012 Member Level: Silver   Points : 0

Thank you all

Author: Siva Prasad23 Mar 2012 Member Level: Gold   Points : 0

I have a small suggestion, In Business Logic Layer, Its better to call Student class's object as a method parameter instead of string name, string city parameters.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: