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.!
Its absolutely easy and easiest method. Thanks a lot Prasad!