Introduction
3 tier architectures are use to make the application more understandable, easy to maintain the application and most important is easy to modify the application i.e. flexible application.
Background
3 tier architecture includes 3 main layers. They are,
- Application Layer or Presentation Layer
- Business Logic Layer(BLL) or Business Access Layer(BAL)
- Data Access Layer(DAL)
Application Layer or Presentation Layer
Application Layer or Presentation Layer is nothing but the User interface. This layer has no interaction with the Database and Servers and all. It is used for the interaction between the user and Application.
Business Logic Layer(BLL) or Business Access Layer(BAL)
This layer contains the business logic, calculations related with the data and validating data. It acts as the interface between the Application layer and Data Access Layer
Data Access Layer(DAL)
Data Access Layer is the only layer which interact with the server or database, all the backend operation in the Application is done in this layer only. Lets see the coding part, so that it will understand easily.
Before creating application make sure that you created the database with tables and stored procedures.
Note: The stored procedure parameters(name, type and size) must be same in the DAL and Stored Procedures.
The 3 tier Architecture will like the following, or you can use separate class libraries for DAL and BLL.
Using the code
When the validation and all finished pass the values to the BLL by creating an object for the BL class in the UI and send the data to the method.
BL blObj = new BL();
val = blObj.AddUser(txtName.Text, txtFatherName.Text);
Then pass the data to the DBServices class. Why i am not executing the queries directly in this class itself is, suppose if you are working in a project and you have to execute many queries it will lead to create a query for each time. Instead of that i created queries in a separate class and called it whenever i need.
List<SqlParameter> paraList = new List<SqlParameter>();
paraList.Add(Db.CreateParameter("@Name", name, SqlDbType.VarChar, 50));
paraList.Add(Db.CreateParameter("@FatherName", fatherName, SqlDbType.VarChar, 50));
integerValue = Convert.ToInt32(Db.ExecuteNonQuery("SP_InsertUser", paraList));
Where SP_InsertUser is my StoredProcedure name.
This will sql type create parameter and insert in the table as i written the insert query in my stored procedure itself. The connection string is provided from the web.config and the query will be executed using the DB class.
public static int ExecuteNonQuery(string spName, List parameters)
{
try
{
using (SqlConnection sqlConnection = new SqlConnection())
{
sqlConnection.ConnectionString = connectionString;
using (SqlCommand command = new SqlCommand())
{
command.Connection = sqlConnection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = spName;
if ((parameters.Count > 0))
{
foreach (SqlParameter param in parameters)
{
command.Parameters.Add(param);
}
}
command.Connection.Open();
return command.ExecuteNonQuery();
}
}
}
catch (SqlException ex)
{
return 0;
}
}
Now the data will be inserted and if it success means will returns a integer value 1 else 0.
The stored procedure will be
Create procedure SP_InsertUser(@Name varchar(50),@FatherName varchar(50))
as
begin
Insert into UserTable(Name,FatherName)values(@Name,@FatherName)
end
Points of Interest
I wrote the method of sending string data only. In case if you are sending integer value to the database means
just change the type as int and size as 0.
paraList.Add(Db.CreateParameter("@SNo", SNo, SqlDbType.Int, 0));
Similarly if it is binary data(like image means create a separate method in DB class for that and perform the operation as similar in create parameter except the parameter type.
The Source code for this article can be found in
here