3 tier sample RRS feed

  • Question

  • User1726897791 posted
    Nothing is better than sample codes, here is a simplified 3 tier architecture with sample code that I usually use: 
    (Just to demonstrate 3 tier architecture, might not be a good example for other purpose)
    1. Database:
    1.1 Table:
    CREATE TABLE Employees (
       fname VARCHAR(32),
       lname VARCHAR(32)
    1.2 Stored Procedure: 
    CREATE PROCEDURE dbo.GetAllEmployees
       SELECT id, fname, lname FROM dbo.Employees
    2. Presentation Layer: 
    2.1 aspx file: 
          <form id="Form1" method="post" runat="server">
             <asp:DataGrid id="dgEmployees" runat="server"></asp:DataGrid>
    2.2 Code-behind, aspx.cs file: 
       protected DataGrid dgEmployees;
       private void Page_Load(object sender, System.EventArgs e) {
          if(!IsPostBack) this.DataBind();
       public override void DataBind() {
          EmployeeCollection employees = new EmployeeCollection();
          this.dgEmployees.DataSource = employees;
    3. Business Logic Layer:
    3.1 Employee class
    public class Employee {
       protected int id;
       protected string firstName;
       protected string lastName;
       public int Id { 
          get { return this.id; }
          set { this.id = value; 
       public string FirstName { 
          get { return this.firstName; }
          set { this.firstName = value; } 
       public string LastName { 
          get { return this.lastName; } 
          set { this.lastName = value; } 
       /* for next method,  you can use seperate mapper class, or even a seperate mapper tier */
       public void CreateFrom(DataRow dr) {
          if(dr != null) {
             this.id = dr.IsNull(0) ? 0 : Convert.ToInt32(dr[0]);
             this.fname = dr.IsNull(1) ? string.Empty : Convert.ToString(dr[1]);
             this.lname = dr.IsNull(2) ? string.Empty : Convert.ToString(dr[2]);
    3.2 Employee collection class
    public class EmployeeCollection : System.Collection.CollectionBase {
       public Employee this[int index] { 
          get { return (Employee)this.InnerList[index]; } 
          set { this.InnerList[index] = value; } 
       public int Add(Employee employee) {
          return this.InnerList.Add(employee);
    /* for next method, you can use a seperate mapper class, or even a seperate mapper tier */
       public int LoadAll() {
          DataSet ds = EmployeeDA.LoadAll();
          if(ds != null && ds.Tables != null && ds.Tables.Count > 0)
             foreach(DataRow dr in ds.Tables[0].Rows) {
                Employee emp = new Employee();
    Sunday, July 30, 2006 10:29 AM

All replies

  • User-503940700 posted


    Just to clarify: there is a difference between layers and tiers. What you have shown as an example seems to me as a "layered" structure (which means logical separation and not physical).

    Why? Because when we talk of tiers, we talk to each components talking to each other across machines (remotely or across LAN). So we need some kind of serilaization also (which seems missing from your example).

    Let me know your thoughts.



    Sunday, July 30, 2006 3:44 PM
  • User1726897791 posted

    Hi Vivek,

    Although it sounds good, it's still the first time that I have to face how to distinguish these two words: layer and tier.

    3 tier or 3 layer, to me they are logical concepts, and we always have lots of options to implement, if you want, I've already seen three physical assemblies for each layer for the exemple code I provided.

    When you say accross machines, I believe you were talking about distributed application. It seems you concentrated on implementation.

    Any organization or Consortium offically differentiated 'tier' and 'layer'? I do agree using 'tier' for distributed application sounds better than 'layer', but as to 3-tier and 3-layer, different or doesn't matter, I really don't know Vivek. If you have any support links or materials, please let me know.



    Sunday, July 30, 2006 11:07 PM
  • User-503940700 posted

    Hi Yong,

    No. Tiers and layers are not similar in "logical sense". See these links:



    Let me know you comments.



    Monday, July 31, 2006 2:32 PM
  • User1726897791 posted

    Hi Vivek,

    Thank you so much for all the information you provided, it really helped me a lot. I interchangablly using 'tier' and 'layer' before, but now I came around to your point of view. Sound and reasonable. Thanks!

    Monday, July 31, 2006 4:42 PM
  • User-2027737976 posted

    I think there is a link between the two. If we don't "layer" our application properly, we won't have the flexiblity of deploying the different layers into different tiers. You can't deploy a non-layered design into different tiers. To put in other words, this "layering" facilitates the tier architecture.

    As an example, if we put all our code in the code-behind (DB access & Business) then all we have is one assembly.

    Tuesday, August 1, 2006 4:34 PM
  • User1831162422 posted

    Yong, thanks for sharing the sample codes.

    I just started to learn C#, and I wanted to make it into proper architecture, which I'm trying out the 3-layered architecture that you mentioned too.

    And thanks Vivek for telling me the difference between tier & layer. I always thought they are the same. :p

    Wednesday, August 2, 2006 12:55 AM
  • User1726897791 posted

    Hi petehbourne, it's a very good point, I like it:
    "layering" facilitates the tier architecture.

    Based on the sample code I provided, here comes one typical deploy approach:

    On database server, we have:
       Table "Employees";
       Stored Procedure: "GetAllEmployees";

    In assembly "BLL.dll", we have:
       class "Employee";
       class "EmployeeCollection";

    In assembly "DAL.dll", we have:
       class "EmployeeDA";

    And surely we have the 3rd assembly for our web application, in which we will use classes from "BLL.dll".

    Let's assume our web application's assembly is "MyWebApp.dll", then the dependicies between them will be:

       MyWebApp.dll --> BLL.dll --> DAL.dll --> database stored procs.

    Let me first apologize and provide the missed sample code for data access layer.

    4 Data Access Layer

    public class EmployeeDA {
       public DataSet LoadAll() {
          string connectionString = "server=myserver;database=mydatabase;uid=myuserid;pwd=mypasswd";
          SqlConnection conn = new SqlConnection(connectionString);
          SqlCommand cmd = new SqlCommand("GetAllEmployees", conn);
          cmd.CommandType = CommandType.StoredProcedure;
          SqlDataAdapter da = new SqlDataAdapter(cmd);
          DataSet ds = null;
          try {
             ds = new DataSet();
          } catch(Exception ex) {
          } finally {
          return ds;

    Thursday, August 3, 2006 3:16 PM
  • User-503940700 posted

    Hi petehbourne,

    I tend to disagree with this "linking". Allow me to elaborate:

    We never think of "layers" first as we need to decide whether our application needs to support "tiers" or not. We can have multiple layers in one tier also. As a thumb rule, we should avoid tiers unless absolutely necessary. After deciding on the "tiers", we come to layers. Ofcourse our application will have layers (at-least) equal to the number of tiers, but there can be more layers than tiers.

    Reverse is not true. A n-layered application can have only one tier.

    So we cannot really say that we have a "link" between the two as both are different things and analyzing division into tiers comes first and then we decide about layers.



    Thursday, August 3, 2006 3:32 PM
  • User440948002 posted
    do you have a downloadable copy of the solution? for VS?
    Tuesday, August 8, 2006 6:51 AM
  • User1726897791 posted

    I just typed in directly, sorry about that. Actually I don't know how to upload a downloadable copy.

    Thursday, August 10, 2006 10:22 AM
  • User440948002 posted

    I thought you might have it stored somewhere on your own website....


    If you want or would allow me, send it to me in a zip and I can put it up on my website and let people download it from their if you want/let me....


    if you do include your EULA  ...what is that freeware licence scheme the people at linux  have?


    GNU? GPL something? it has escaped me...do you know what it is?

    Thursday, August 10, 2006 6:07 PM