Click here to Skip to main content
Click here to Skip to main content
Alternative Tip/Trick

Tagged as

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

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

Share

About the Author

Collin Jasnoch
Engineer
United States United States
No Biography provided

Comments and Discussions

 
GeneralReason for my vote of 5 good points added... Pinmemberjohannesnestler27-Feb-12 21:42 
GeneralMVP! MVP! MVP! I just got going on my next customer's pro... Pinmemberchuckamus_prime20-Dec-11 4:21 

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 | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 25 Dec 2011
Article Copyright 2011 by Collin Jasnoch
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid