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

Tagged as

Stats

72.9K 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)



Comments and Discussions

 
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
Eduard Lu29-Nov-11 20:57 
GeneralReason for my vote of 5 Straight to the point. Just the way... Pin
Sainey29-Nov-11 10:30
Sainey29-Nov-11 10:30 
GeneralRe: thanks Sainey Pin
Eduard Lu29-Nov-11 20:57
Eduard Lu29-Nov-11 20:57 
GeneralI second what Reiss has noted. This is a very good tip, but ... Pin
fjdiewornncalwe28-Nov-11 7:24
professionalfjdiewornncalwe28-Nov-11 7:24 
GeneralRe: renamed. Thanks for the comments. Pin
Eduard Lu28-Nov-11 14:52
Eduard Lu28-Nov-11 14:52 
General<b>Eduard Lu wrote:The best way to design your own applicati... Pin
Reiss28-Nov-11 0:37
professionalReiss28-Nov-11 0:37 
QuestionBeginner article Pin
Sean Botha27-Dec-11 10:36
Sean Botha27-Dec-11 10:36 
I can remember when I first started out most of these articles was impossible for a newby to make out. Yours is easy to understand and follow. I agree with the use of Interfaces and contracts but other than that great job
QuestionSimple and superb Pin
Suchi Banerjee, Pune29-Nov-11 15:34
Suchi Banerjee, Pune29-Nov-11 15:34 
AnswerRe: Simple and superb Pin
Eduard Lu30-Nov-11 15:20
Eduard Lu30-Nov-11 15:20 

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.