Programming using Layered Dot Net Architecture

how to write efficient code and how to represent the information which take advantage of layered design of .net framework. You must also now aware about how you can write code efficiently and improve performance of application and avoids errors due to poor coding style.

Programming using Layered Dot Net Architecture

Note: To make you understand the concept i have started with example

OK, so Lets Start by understanding need of writing a layered programming.

Well for this article I will focus on Programming structure, but you can implement with other programming structures too...

Ok, so +ve points for layered programming with

i) Opening of connection for a short time and Closing it as soon as database task is completed, it thus help in improving performance of application.

ii) Better usage of Error-Handling, to make sure that connection is closed even if the exception occurs.

iii) Follow Stateless design, taking all needed information as parameter and return all the data through return value. It helps in load balancing and it would be easy while implementing web-service.

iv) Storing connection string in app.config file allow us to made a single change, to change a connection string. It helps in connection pooling.

Now lets understand how to undergo the procedure to achieve above advantages... Programming Style consist of Layered design which describes as bellow:

1. User Interface Layer

User Interface consist of Forms which are been designed to interact with User.

2. Stub for Accessing Database Layer

Stub for accessing database layer is been prepared by creating classes which contains a specific details related to access data and task such as adding, editing and deleting functionality..

Note: Connection string is retrieved from section of the app.config file, rather than hard-coded. This is beneficial for connection pooling, which ultimately improves performance of application.

<add key="connStr" value="data source=(local);Initial Catalog=Northwind;Integrated Security=SSPI">

Stub consist of two class files:

2.1) A data details class and

2.2) A database utility class.

Here I have taken Example of filling basic employeed details

2.1) Data Details Class: It consist of details of record which is manipulated.

public class EmpDetails
/* Declaring Property for each field. */
private int employeeId;
public int EmployeeId
get{return employeeId;}
set{employeeId = value;}

private string firstName;
public string FirstName
get{return firstName;}
set{firstName = value;}

private string lastName;
public string LastName
get{return lastName;}
set{lastName = value;}

/* Constructor definition */
public EmpDetails(int employeeId,
string firstName,string lastName)
this.employeeId = employeeId;
this.firstName = firstName;
this.lastName = lastName;

2.2) Database Utility Class: It consist of different data manipulation operation (such as add, delete, update, select) which is performed to the actual database.

public class EmpDB


string connStr;

/* Retrieving Connection String in Constructor*/
public EmpDB()

connStr = ConfigurationSettings.AppSettings["connStr"];


/* Insertion of employee details*/
public int InsertEmployee(EmpDetails emp)

// Opening Connection
SqlConnection conn = new SqlConnection(connStr);

// Creating Command

SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;

cmd.CommandText = "procInsertEmployee";

// Adding required parameter
cmd.Parameters.Add(new SqlParameter("@EmployeeId",SqlDbType.Int));
cmd.Parameters["@EmployeeId"].Direction = ParameterDirection.Output;

cmd.Parameters.Add(new SqlParameter("@FirstName",SqlDbType.NVarChar,20));

cmd.Parameters["@FirstName"].Value = emp.FirstName;

cmd.Parameters.Add(new SqlParameter("@LastName",SqlDbType.NVarChar,20));

cmd.Parameters["@LastName"].Value = emp.LastName;




// Executing Command and retrieving result.

int RecordsAffected = cmd.ExecuteNonQuery();
int EmpId;

if(RecordsAffected != 0)

EmpId = (int)cmd.Parameters["@EmployeeId"].Value;


EmpId = -1;

return EmpId;


catch(SqlException ex)


throw new ApplicationException("Data Error");




// Ensuring that Connection is closed even if exception occurs.


// Similarly Perform different database opperation
// Check out "EmpDB.cs" for demo code.
public int UpdateEmployee(EmpDetails emp)

public int DeleteEmployee(int EmployeeId)


public EmpDetails GetEmployee(int EmployeeId)


public EmpDetails[] GetAllEmployee()



3. Database Layer

Database layer is server where MS SQL Server is installed, or where the database is actually stored. This code design is independent of database layer.


Thus by end of this article, you have understand how to write efficient code and how to represent the information which take advantage of layered design of .net framework. You must also now aware about how you can write code efficiently and improve performance of application and avoids errors due to poor coding style.

Hope to hear your useful comments so that together we can make programming easy and efficient.

Related Articles

Object Serialisation in dot net

This article provides an overview of the serialization used in Microsoft .NET and the usage of the same.For example, serialization is used to save session state in ASP.NET and to copy objects to the clipboard in Windows Forms. It is also used by remoting to pass objects by value from one application domain to another.

More articles: Dot Net


No responses found. Be the first to comment...

  • 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: