views:

798

answers:

9

Hi,

Presently I am working using single tier architecture. Now I am wanting to learn how to write code using 3 tier architecture. Please can you provide me with a simple example?

+2  A: 

A 3-tier architecture usually has the following components:

  1. Client Browser
  2. Web server hosting the ASP.NET application
  3. Some backend storage such as database that is being accessed by the ASP.NET application

So to answer your question on how to write code for a 3-tier architecture, you develop an ASP.NET application that communicates with a data storage.

Darin Dimitrov
if you have a firewall/loadbalancer in front of your websites, you could even argue you have a 4 tier architecture. ;^)
Toad
Usually physical network appliances are not accounted. You could have the client browser, the web server hosting the application and the database all on the same physical machine, it is still a 3-tier architecture.
Darin Dimitrov
Would you consider the browser part of the architecture?... Why not: 1. Web server serving static content or rendering a template; 2. Server-side business logic and dynamic content generation; 3. Database.
Daniel Vassallo
sleske
if i write connetion class in business layer how can i get connection string which is in web.config file in user inteface layer.
Surya sasidhar
Put it in the constructor of your data access layer class. When this class is instantiated (in the interface layer), you pass the value from `web.config`.
Darin Dimitrov
+2  A: 

Presentation layer: put everything that is related to user interface. (What the user sees)

Business layer: everything that is related to the logic of the application (How is the information coming from presentation layer treated)

Data layer: provide an abstraction of the underlying data source(s) (Where and how the information coming from/going to business layer is stored)

Each layer should know as less as possible about the other and it should be a top down approach:

  • the data layer should know nothing about business and presentation
  • business layer should know about data but not about presentation
  • presentation should know about business but not about data

Simple example:

Website:

  • Presentation: all the graphical things, fields where user inserts data, menus, pictures, etc.
  • Business: all constraints about the data (unique name, name without symbols, valid date, etc), methods for manipulating business objects (create new user, add new order, etc)
  • Data: Methods that access the underlying database.
Victor Hurdugaci
A: 

Here's a tutorial about 3 tier architecture:

http://www.beansoftware.com/ASP.NET-Tutorials/Three-Tier-Architecture.aspx

Tony
+3  A: 

3-tier architecture can have different meanings depending on context. Generally it means that responsibilities in the application are divided between different tiers. Typically, 3-tier refers to :

  • presentation tier" (actual user interface)
  • logic tier (application/business logic)
  • data tier (database, data storage)

The details vary by application.

Wikipedia, as usual, has a nice overview: http://en.wikipedia.org/wiki/Multitier_architecture

A simple example would be a typical business app:

  • presentation: browser, or fat client
  • logic tier: business logic, typically in an application server (based on J2EE, ASP.NET or whatever)
  • data tier: a database, typically a RDBMS such as MySQL or Oracle
sleske
+12  A: 

Wikipedia have a nice explanation: Multitier architecture:

'Three-tier' is a client-server architecture in which the user interface, functional process logic ("business rules"), computer data storage and data access are developed and maintained as independent modules, most often on separate platforms.

alt text

Web development usage

In the web development field, three-tier is often used to refer to websites, commonly electronic commerce websites, which are built using three tiers:

  • A front end web server serving static content, and potentially some cached dynamic content.
  • A middle dynamic content processing and generation level application server, for example Java EE, ASP.net, PHP platform.
  • A back-end database, comprising both data sets and the database management system or RDBMS software that manages and provides access to the data.
Rubens Farias
i am using visual studio dot net 2005. can i use visual sourcesafe Mr. Rubens Farias
Surya sasidhar
@Surya, while this isn't related to your original question (it's doesn't count as a tier), YES, you should to use some kind of source control tool like SourceSafe or TFS
Rubens Farias
Preferably not sourcesafe, a newer scm tool would be a good idea, particularly if you are just starting out.
Paddy
+5  A: 

By "tier" do you mean a "layer" in your software stack? The word "tier" is better used to describe the physical components of your system. If you are using ASP.NET, you probably already have a "3 tiered" system -

  1. Browser displaying web pages
  2. IIS Server hosting your app
  3. Database Server with your database

But you are possibly putting all of your code into a single software "layer" - specifically, the code behind file of your aspx pages. You want to move from a single layer to a 3 layer approach. The classic "3 layer" software architecture consists of the following -

  1. Presentation Layer

  2. Business Logic Layer (BLL)

  3. Data Access Layer (DAL)

alt text

For a typical ASP.NET app, you might apply this as follows. First, you create a LINQ2SQL file (.dbml) containing the objects for your database access. This is your Data Access Layer (DAL).

Next you might create a DLL to contain your Business Logic Layer (BLL). This layer will access the database via the DAL, manipulate it as required, and then expose it via a simple interface. For example, if your application displays a client list, your BLL might have a public function called GetClientList() which returned a list of clients.

Finally you would set up your code behind files to instantiate the BLL and wire it up to the interface components. This is your Presentation Layer. For example, it might take the data returned from your GetClientList() function and bind it to a data grid on the web form. The idea is to have the presentation layer as thin as possible.

This seems a little long-winded to describe, but it's pretty straight-forward once you have done it a couple of times. You will find that separating out your application like this will make it much easier to maintain, as the separation of concerns leads to cleaner code. You will also find it much easier to upgrade or even replace your presentation layer, as it contains very little smarts. Finally, you will get to a point where you have a number of very useful BLL libraries that you can easily consume in new applications, greatly improving productivity.

CraigS
A: 

A good tutorial, with complete source control download of a well written tiered application would be here:

http://nerddinnerbook.s3.amazonaws.com/Intro.htm

This isn't a tutorial about tiered architecture, but it's a well written app and gives some insight into why you might consider this architecture.

Additionally, as has only been briefly touched on above, this is about keeping your logic/storage/presentation code separate, so if you have to change one of them (e.g change from asp.net front end to a desktop application), it's not so hard to do.

Paddy
+4  A: 

This is what I have in my project. More than just a traditional 3-tier architecture.

1.) Application.Infrastructure

  • Base classes for all businessobjects, busines object collection, data-access classes and my custom attributes and utilities as extension methods, Generic validation framework. This determines overall behavior organization of my final .net application.

2.) Application.DataModel

  • Typed Dataset for the Database.
  • TableAdapters extended to incorporate Transactions and other features I may need.

3.) Application.DataAccess

  • Data access classes.
  • Actual place where Database actions are queried using underlying Typed Dataset.

4.) Application.DomainObjects

  • Business objects and Business object collections.
  • Enums.

5.) Application.BusinessLayer

  • Provides manager classes accessible from Presentation layer.
  • HttpHandlers.
  • My own Page base class.
  • More things go here..

6.) Application.WebClient or Application.WindowsClient

  • My presentation layer
  • Takes references from Application.BusinessLayer and Application.BusinessObjects.

Application.BusinessObjects are used across the application and they travel across all layers whenever neeeded [except Application.DataModel and Application.Infrastructure]

All my queries are defined only Application.DataModel.

Application.DataAccess returns or takes Business objects as part of any data-access operation. Business objects are created with the help of reflection attributes. Each business object is marked with an attribute mapping to target table in database and properties within the business object are marked with attributes mapping to target coloumn in respective data-base table.

My validation framework lets me validate each field with the help of designated ValidationAttribute.

My framrwork heavily uses Attributes to automate most of the tedious tasks like mapping and validation. I can also new feature as new aspect in the framework.

A sample business object would look like this in my application.

User.cs

[TableMapping("Users")]
public class User : EntityBase
{
    #region Constructor(s)
    public AppUser()
    {
        BookCollection = new BookCollection();
    }
    #endregion

    #region Properties

    #region Default Properties - Direct Field Mapping using DataFieldMappingAttribute

    private System.Int32 _UserId;

    private System.String _FirstName;
    private System.String _LastName;
    private System.String _UserName;
    private System.Boolean _IsActive;

    [DataFieldMapping("UserID")]
    [DataObjectFieldAttribute(true, true, false)]
    [NotNullOrEmpty(Message = "UserID From Users Table Is Required.")]
    public override int Id
    {
        get
        {
            return _UserId;
        }
        set
        {
            _UserId = value;
        }
    }

    [DataFieldMapping("UserName")]
    [Searchable]
    [NotNullOrEmpty(Message = "Username Is Required.")]
    public string UserName
    {
        get
        {
            return _UserName;
        }
        set
        {
            _UserName = value;
        }
    }

    [DataFieldMapping("FirstName")]
    [Searchable]
    public string FirstName
    {
        get
        {
            return _FirstName;
        }
        set
        {
            _FirstName = value;
        }
    }

    [DataFieldMapping("LastName")]
    [Searchable]
    public string LastName
    {
        get
        {
            return _LastName;
        }
        set
        {
            _LastName = value;
        }
    }

    [DataFieldMapping("IsActive")]
    public bool IsActive
    {
        get
        {
            return _IsActive;
        }
        set
        {
            _IsActive = value;
        }
    }

    #region One-To-Many Mappings
    public BookCollection Books { get; set; }

    #endregion

    #region Derived Properties
    public string FullName { get { return this.FirstName + " " + this.LastName; } }

    #endregion

    #endregion

    public override bool Validate()
    {
        bool baseValid = base.Validate();
        bool localValid = Books.Validate();
        return baseValid && localValid;
    }
}

BookCollection.cs

/// <summary>
/// The BookCollection class is designed to work with lists of instances of Book.
/// </summary>
public class BookCollection : EntityCollectionBase<Book>
{
    /// <summary>
    /// Initializes a new instance of the BookCollection class.
    /// </summary>
    public BookCollection()
    {
    }

    /// <summary>
    /// Initializes a new instance of the BookCollection class.
    /// </summary>
    public BookCollection (IList<Book> initialList)
        : base(initialList)
    {
    }
}
this. __curious_geek
A: 

Hi! I have a question.. until now I just found out I was building a 4-tier model, I know the logic is the same but there're layers for entities, so every class has its properties, and those properties are the fields of the DBtables. So, I have the costumers class and inside there's a field that's a foreign key of other table named CustomersType, hence there's a class named CustomersType, so, I've read that in the Costumers Class for the idCustomerType I have to create the field not as an integer, but as the type of the CustomersType class. You guys know what I mean? My question is. How do I use this CustomersType Datatype? 'cause I don't know in what cases I will be using it and what methods are inside that function. e.g.

Class bCustomers { int idClient; String name; bCustomersType idCustomerType;

public int IDCLIENT{set; get;} public String NAME {set; get; } public bCustomersType IDCUSTOMERTYPE { What goes in here? } }

Class bCustomersType { int idCustomerType; String CustomerTypeDesc;

public int... you know the rest hehe! }

I hope you can help me.

Miguel