Click here to Skip to main content
15,124,353 members
Articles / Web Development / ASP.NET
Tip/Trick
Posted 27 Nov 2011

Tagged as

Stats

70.6K views
31 bookmarked

N-Tier: Begginer's guide in designing their application

Rate me:
Please Sign up or sign in to vote.
4.73/5 (23 votes)
25 Dec 2011CPOL
N-Tier
A great way to design your own application is to use the N-tier architecture.

This is just a simple tip that I will share and it is for you to check out.

Note: The following layers are exposed as different projects under one solution.

Object Layer: Create a class with properties:
C#
public class Person
{
   public string Firstname {get;set;}
   public string Lastname {get;set;}
   public int Age {get;set;}
}

Data Access Layer: The layer that interacts with database:

C#
public sealed class PersonDAL
{
   Person person = new Person();
   public static int GetAge(string firstname, string lastname)
   {
      try
      {
         SQLConnection sqlcon = new SQLConnection(ConfigurationManager
                                                      .ConnectionStrings["myDB"]
                                                      .ConnectionString);
         SQLCommand sqlcmd = new SQLCommand("SELECT age FROM Person_tb WHERE
                               firstname=@firstname AND lastname=@lastname",
                               sqlconn);
         sqlcmd.CommandType = CommandType.CommandText;
         sqlcmd.Parameters.AddWithValue("@firstname", firstname);
         sqlcmd.Parameters.AddWithValue("@lastname", lastname);
         sqlcon.Open();
         int retval = (int)sqlcmd.ExecuteScalar();
      }
      catch(Exception ex)
      {
         //Log ex
      }
      finally
      { 
         sqlcmd.Dispose();
         sqlcon.Close();
         sqlcon = null;
      }
   }
}

Business Logic Layer: The layer that serves as bridge for DAL and Application Layer:

C#
public sealed class PersonBLL
{
   public static int GetAge(string firstname, string lastname)
   {
      return PersonDAL.GetAge(firstname, lastname);
   }
}

Application/Presentation Layer: The layer where users input data:

C#
Person person = new Person();
person.Firstname = "Eduard";
person.Lastname = "Lu";

string firstname = person.Firstname;
string lastname = person.Lastname;

txtAge.Text = PersonBLL.GetAge(firstname, lastname).ToString();


There are still a lot of ways to optimize this one. You can add more layers which your application might be needing. For example, add a Utilities Layer wherein you can place your configuration/settings and your error/transaction logger. In my example, you can create a function that will return the connection string for your application to communicate with your database server.

Also, creating stored procedure is better than using command text. In my example, I used command text just to show you my query.

Message me if you have concerns. :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


Comments and Discussions

 
General[My vote of 1] [My vote of 1] Pin
CelPlusPlus12-Sep-14 8:06
MemberCelPlusPlus12-Sep-14 8:06 
QuestionThose aren't tiers Pin
PIEBALDconsult5-Apr-12 6:33
professionalPIEBALDconsult5-Apr-12 6:33 
GeneralRe: thanks Reiss Pin
Eduard Lu28-Nov-11 23:00
MemberEduard Lu28-Nov-11 23:00 
GeneralRe: Good article btw, now voted Pin
Reiss28-Nov-11 22:54
professionalReiss28-Nov-11 22:54 
Good article btw, now voted
GeneralWe use this model exclusively, as highly detailed on Spaanja... Pin
EngleA20-Dec-11 6:06
MemberEngleA20-Dec-11 6:06 
GeneralMVP! MVP! MVP! I just got going on my next customer's projec... Pin
chuckamus_prime20-Dec-11 4:22
Memberchuckamus_prime20-Dec-11 4:22 
GeneralReason for my vote of 1 Short summary of the N-tier approach... Pin
DavidOwen19-Dec-11 19:59
MemberDavidOwen19-Dec-11 19:59 
GeneralI wonder why the presentation layer should know about the bu... Pin
bradut8-Dec-11 8:46
professionalbradut8-Dec-11 8:46 
GeneralReason for my vote of 5: Its precise and concise, should rea... Pin
Uday P.Singh5-Dec-11 21:43
MemberUday P.Singh5-Dec-11 21:43 
GeneralRe: thanks! :) Pin
Eduard Lu5-Dec-11 21:45
MemberEduard Lu5-Dec-11 21:45 
GeneralReason for my vote of 5 A practical example I needed to unde... Pin
Marius Coetzee5-Dec-11 18:40
MemberMarius Coetzee5-Dec-11 18:40 
GeneralRe: thanks! Pin
Eduard Lu5-Dec-11 21:31
MemberEduard Lu5-Dec-11 21:31 
GeneralReason for my vote of 5 exactly what i am looking for! thank... Pin
Aja wakim1-Dec-11 17:38
MemberAja wakim1-Dec-11 17:38 
GeneralRe: thanks aja Pin
Eduard Lu1-Dec-11 17:44
MemberEduard Lu1-Dec-11 17:44 
GeneralOne question, what's the reason to implementing a BLL? You c... Pin
Roberto Mazzone30-Nov-11 22:38
MemberRoberto Mazzone30-Nov-11 22:38 
GeneralRe: you can put your validation inside the BLL before returning ... Pin
Eduard Lu30-Nov-11 22:47
MemberEduard Lu30-Nov-11 22:47 
GeneralReason for my vote of 3 ok Pin
Ganesan Senthilvel30-Nov-11 16:20
MemberGanesan Senthilvel30-Nov-11 16:20 
GeneralReason for my vote of 5 Very nice... incredibly rudimentary,... Pin
BrianBissell30-Nov-11 7:54
MemberBrianBissell30-Nov-11 7:54 
GeneralRe: Thanks brian Pin
Eduard Lu30-Nov-11 15:16
MemberEduard Lu30-Nov-11 15:16 
GeneralReason for my vote of 1 :) Pin
Horváth Aladár30-Nov-11 0:05
MemberHorváth Aladár30-Nov-11 0:05 
GeneralRe: FYI. 1 is bad, 5 is good. Considering that your comment is a... Pin
fjdiewornncalwe30-Nov-11 4:45
professionalfjdiewornncalwe30-Nov-11 4:45 
GeneralRe: :) Pin
Eduard Lu30-Nov-11 15:15
MemberEduard Lu30-Nov-11 15:15 
GeneralThat is really a a nice to understand example.could you plea... Pin
Yashar_Khan29-Nov-11 20:39
MemberYashar_Khan29-Nov-11 20:39 
GeneralReason for my vote of 5 5 from me as well. Pin
fjdiewornncalwe29-Nov-11 11:03
professionalfjdiewornncalwe29-Nov-11 11:03 
GeneralRe: thanks Marcus Pin
Eduard Lu29-Nov-11 20:57
MemberEduard Lu29-Nov-11 20:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.