Click here to Skip to main content
Click here to Skip to main content

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

By , 25 Dec 2011
 
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:
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:
 
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:
 
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:
 
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)

About the Author

Eduard Lu
New Zealand New Zealand
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionThose aren't tiersmemberPIEBALDconsult5 Apr '12 - 5:33 
GeneralRe: thanks ReissmemberEduard Lu28 Nov '11 - 22:00 
GeneralRe: Good article btw, now votedmemberReiss28 Nov '11 - 21:54 
GeneralWe use this model exclusively, as highly detailed on Spaanja...memberEngleA20 Dec '11 - 5:06 
GeneralMVP! MVP! MVP! I just got going on my next customer's projec...memberchuckamus_prime20 Dec '11 - 3:22 
GeneralReason for my vote of 1 Short summary of the N-tier approach...memberDavidOwen19 Dec '11 - 18:59 
GeneralI wonder why the presentation layer should know about the bu...memberbradut8 Dec '11 - 7:46 
I wonder why the presentation layer should know about the business layer in
txtAge.Text = PersonBLL.GetAge(firstname, lastname).ToString();
I thought that the object itself should communicate with the BLL, like:
txtAge.Text = person.Age;
so that the UI would be completely agnostic of how things are done.
GeneralReason for my vote of 5: Its precise and concise, should rea...memberUday P.Singh5 Dec '11 - 20:43 
GeneralRe: thanks! :)memberEduard Lu5 Dec '11 - 20:45 
GeneralReason for my vote of 5 A practical example I needed to unde...memberMarius Coetzee5 Dec '11 - 17:40 
GeneralRe: thanks!memberEduard Lu5 Dec '11 - 20:31 
GeneralReason for my vote of 5 exactly what i am looking for! thank...memberAja wakim1 Dec '11 - 16:38 
GeneralRe: thanks ajamemberEduard Lu1 Dec '11 - 16:44 
GeneralOne question, what's the reason to implementing a BLL? You c...membermonolicciolo30 Nov '11 - 21:38 
GeneralRe: you can put your validation inside the BLL before returning ...memberEduard Lu30 Nov '11 - 21:47 
GeneralReason for my vote of 3 okmemberGanesanSenthilvel30 Nov '11 - 15:20 
GeneralReason for my vote of 5 Very nice... incredibly rudimentary,...memberBrianBissell30 Nov '11 - 6:54 
GeneralRe: Thanks brianmemberEduard Lu30 Nov '11 - 14:16 
GeneralReason for my vote of 1 :)memberHorváth Aladár29 Nov '11 - 23:05 
GeneralRe: FYI. 1 is bad, 5 is good. Considering that your comment is a...memberMarcus Kramer30 Nov '11 - 3:45 
GeneralRe: :)memberEduard Lu30 Nov '11 - 14:15 
GeneralThat is really a a nice to understand example.could you plea...memberYashar_Khan29 Nov '11 - 19:39 
GeneralReason for my vote of 5 5 from me as well.memberMarcus Kramer29 Nov '11 - 10:03 
GeneralRe: thanks MarcusmemberEduard Lu29 Nov '11 - 19:57 
GeneralReason for my vote of 5 Straight to the point. Just the way...memberSainey29 Nov '11 - 9:30 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 25 Dec 2011
Article Copyright 2011 by Eduard Lu
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid