Click here to Skip to main content
15,891,136 members
Articles / Web Development / ASP.NET
Alternative
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.67/5 (8 votes)
25 Dec 2011CPOL1 min read 11.6K   5   2
I think you are missing some of the key points. Like why?In true practice, they are separated (specifically in the way you have shown) because the logic exists and is running on different systems.For example, a user may be running a client application (presentation layer and business...
I think you are missing some of the key points. Like why?

In true practice, they are separated (specifically in the way you have shown) because the logic exists and is running on different systems.

For example, a user may be running a client application (presentation layer and business logic layer) which calls some services (data access layer), which in turn query a Database (Data layer).

By designing it using N-Tier, one can build an application and deploy quickly using separation of concerns. If the logic is not separated, it is difficult and possibly dangerous as parts of the system will end up exposed that should not be (e.g. no user should be able to directly access your DB).

Also for easy extension, one should use Interfaces and contracts. For example, the Person object should be as follows:

C#
[DataContract]
public class Person
{
   [DataMember]
   public string Firstname {get;set;}

   [DataMember]
   public string Lastname {get;set;}

   [DataMember]
   public int Age {get;set;}
}


This allows the object type to be built in the Access Layer and then serialized to the client.

As for using interfaces, it is more optimal because you need only expose the interface where it is used but can change the innerworkings, however is appropriate. Once you get into injection, you are going beyond "beginner", but the precedence should still be set.

Interfaces can be exposed as a single project library.
C#
public interface IPerson
{
   string FirstName {get; set;}
   string LastName {get; set;}
   int Age {get;}

   void HaveBirthday();
}

public interface IDrinker
{
    bool IsIntoxicated {get;}
}

public interface IReceiveToys
{
    bool IsPlayingWithNewToys {get; }
}


The actual contracts need to use the appropriate attributes to serialize the data from the service calls.

C#
[DataContract]
public class Adult : IPerson, IDrinker
{
   [DataMember]
   public string Firstname {get;set;}

   [DataMember]
   public string Lastname {get;set;}

   [DataMember]
   public int Age {get; private set;}

   public bool IsIntoxicated {get; private set;}

   public void HaveBirthday()
   {
      Age++;
      Celebrate();
   }

   private void Celebrate()
   {
       IsIntoxicated = true;
   }
}
 
[DataContract]
public class Child : IPerson, IReceiveToys
{
   [DataMember]
   public string Firstname {get;set;}
   [DataMember]
   public string Lastname {get;set;}
   [DataMember]
   public int Age {get; private set;}

   public bool IsPlayingWithNewToys {get; private set;}

   public List<IGift> Gifts {get; set;}

   public void HaveBirthday()
   {
      Age++;
      UnWrapGifts();
   }
   private void UnWrapGifts()
   {
       if(Gifts.Count > 0)
           IsPlayeWithNewToys = true;
   }
}


[EDIT]HTML Tags slightly off.

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 good points added... Pin
johannesnestler27-Feb-12 20:42
johannesnestler27-Feb-12 20:42 
GeneralMVP! MVP! MVP! I just got going on my next customer's pro... Pin
chuckamus_prime20-Dec-11 3:21
chuckamus_prime20-Dec-11 3:21 

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.