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

Tagged as

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

, 25 Dec 2011
Rate this:
Please Sign up or sign in to vote.
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:
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. Smile | :)

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

 
GeneralRe: thanks! PinmemberEduard Lu5-Dec-11 20:31 
GeneralReason for my vote of 5 exactly what i am looking for! thank... PinmemberAja wakim1-Dec-11 16:38 
GeneralRe: thanks aja PinmemberEduard Lu1-Dec-11 16:44 
GeneralOne question, what's the reason to implementing a BLL? You c... Pinmembermonolicciolo30-Nov-11 21:38 
GeneralRe: you can put your validation inside the BLL before returning ... PinmemberEduard Lu30-Nov-11 21:47 
GeneralReason for my vote of 3 ok PinmemberGanesanSenthilvel30-Nov-11 15:20 
GeneralReason for my vote of 5 Very nice... incredibly rudimentary,... PinmemberBrianBissell30-Nov-11 6:54 
GeneralRe: Thanks brian PinmemberEduard Lu30-Nov-11 14:16 
GeneralReason for my vote of 1 :) PinmemberHorváth Aladár29-Nov-11 23:05 
GeneralRe: FYI. 1 is bad, 5 is good. Considering that your comment is a... PinmemberMarcus Kramer30-Nov-11 3:45 
GeneralRe: :) PinmemberEduard Lu30-Nov-11 14:15 
GeneralThat is really a a nice to understand example.could you plea... PinmemberYashar_Khan29-Nov-11 19:39 
GeneralReason for my vote of 5 5 from me as well. PinmemberMarcus Kramer29-Nov-11 10:03 
GeneralRe: thanks Marcus PinmemberEduard Lu29-Nov-11 19:57 
GeneralReason for my vote of 5 Straight to the point. Just the way... PinmemberSainey29-Nov-11 9:30 
GeneralRe: thanks Sainey PinmemberEduard Lu29-Nov-11 19:57 
GeneralI second what Reiss has noted. This is a very good tip, but ... PinmemberMarcus Kramer28-Nov-11 6:24 
GeneralRe: renamed. Thanks for the comments. PinmemberEduard Lu28-Nov-11 13:52 
General<b>Eduard Lu wrote:The best way to design your own applicati... PinmemberReiss27-Nov-11 23:37 
QuestionBeginner article PinmemberSean Botha27-Dec-11 9:36 
QuestionSimple and superb PinmemberSuchi Banerjee, Pune29-Nov-11 14:34 
AnswerRe: Simple and superb PinmemberEduard Lu30-Nov-11 14:20 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 25 Dec 2011
Article Copyright 2011 by Eduard Lu
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid