.NET Daily

N-Tier Architecture. ASP.NET Example

Introduction

Darius

Darius

Darius Dumitrescu is a creative Senior CMS Consultant with in depth .NET knowledge, focused on Web Development and Architecture Design.


LATEST POSTS

How to personalize based on search engine keywords with Sitecore 10th June, 2017

Sitecore Tracker.Current is not Initialized – During setup of a single Sitecore CD 22nd May, 2017

ASP.net

N-Tier Architecture. ASP.NET Example

Posted on .

Introduction

N-tier architecture is probably one of the most used architecture models in the industry. It is used so often because it’s scalable, extensible, secure and maintainable over time. It also helps the developers with different roles to better do their job without interfering with each other.

In this article I will present a basic n-tier architecture that can be used for creating small up to medium asp.net applications either MVC or WebForms.

But before we start exemplifying the concept, I want first to mention some key aspects independent of this architecture that need to be considered so that the design of your application will remain viable over time:

  • Make sure that your application layers are not dependent on each other. They need to remain independent so that new layers can be added or old technologies to be changed.
  • Single Responsibility Principle. Make sure that every component or module is responsible only for a specific feature or functionality.
  • Principle of Least Knowledge. A component or object should not know about internal details of other components.
  • Don’t Repeat Yourself(DRY). A specific functionality should exist only in one place. It should not be duplicated in any other component.
  • Set up some coding standards for development. This assures code consistency and maintainability.
  • Keep shared functionality such as exception logging or security in a place that be accessed from any application levels (i.e. presentation layer or business layer)

Architecture Schema

Below you have an image with how the architecture schema looks like:

ASP.NET N-Tier Architecture Schema

ASP.NET N-Tier Architecture Schema

 

A Short Description for each Layer

 

Entities Layer

The Entities layer contains all the entities that are used in all the other projects of the application such as tables mappings from database, DTOs (Data Transfer Objects) or ASP.NET MVC Models. Most of the classes in this layer are POCO (Plain Old CLR Objects).

 

Data Access Layer

The Data Access layer contains functionality for Creating, Returning, Updating, Deleting (CRUD) items into the database. In this layer you can use technologies like ADO.NET, nHibernate or Entity Framework.

The entities or the database table mappings classes needed for the above operations will be referenced from Entities layer described above.

This layer should only be shared by the Business Components layer. In Visual Studio, the layer will be represented by a “Class Library” project type.

Business Components Layer

The Business Components layer contains all the core functionality of the application. The purpose is to hold all the custom logic that is applied on the methods that are exposed from the Data Access Layer before they are referenced in the Presentation Layer or the Services Layer. Operations like converting arrays to lists, mathematical calculations or variable conversions are made in this layer.

The entities needed for the above operations will be referenced from Entities layer and the database operation methods will be referenced from Data Access layer.

This layer should be shared only by the Presentation Layer or the Services Layer. In Visual Studio, the layer will be represented by a “Class Library” project type.

Services Layer

The Services Layer gives you the possibility to expose the Business Components Layer as an API to third party systems.
For example, if you want to share with a mobile application some data about the products that you have in your database, this layer should be responsible for that. However, this layer is not a mandatory one. If you consider that your application will not share data with another system, then this layer should not be created.

As I said earlier, the methods that are needed to be exposed in this layer are referenced from the Business Components Layer and this layer should only be shared by the Presentation Layer and other third party systems.

In this layer you can use technologies like WCF Services, Web Services or ASP.NET WebAPI.

Presentation Layer

The Presentation Layer is responsible of hosting the user interface of your application. As an example, in this layer you will have all the .aspx pages and .ascx user controls along with JavaScript and CSS files. The methods responsible for getting data from the database will be referenced only directly from Business Components layer or from Services layer.

In this layer you can use technologies like ASP.NET WebForms or ASP.NET MVC.

NOTE: Be sure that this layer is kept clean of database direct calls or direct access to Data Access layer.

Common Components Layer

The Common Components Layer contains all the common libraries or functionality that can be used in any of the above layers. For example: validation functionality, security libraries, encryption tools or exception logging classes can be added to this layer.

This is an optional layer and it is mandatory not to have any dependencies on the above described layers because this layer can be compiled and shared across multiple projects.

In Visual Studio, the layer will be represented by a “Class Library” project type.

Practical Example

In Visual Studio the described n-tier structure will look like this:

Visual Studio N-Tier Example

Visual Studio N-Tier Example

You can download a Visual Studio solution containing the layer described above from below

Attachments

Darius

Darius

Darius Dumitrescu is a creative Senior CMS Consultant with in depth .NET knowledge, focused on Web Development and Architecture Design.

Comments
  • user

    AUTHOR Jigar Patel

    Posted on 9:01 am June 13, 2014.

    Good Article…

  • user

    AUTHOR Subha

    Posted on 4:13 pm September 9, 2014.

    Thanks, Darius. Its grt article for me.
    Thanks a lot!!!

  • user

    AUTHOR Nicholas

    Posted on 3:44 pm October 1, 2014.

    Love the article but I have a question about third party APIs. In which layer would you reference the third party API?

  • user

    AUTHOR Darius Dumitrescu

    Posted on 8:55 pm October 1, 2014.

    Hi Nicholas,

    I usually create a new Visual Studio project under the presentation layer. After that, if for example you need entities for it, you just create a new folder under Entities layer for it.

    Thanks!

    • user

      AUTHOR Nicholas

      Posted on 9:16 pm October 1, 2014.

      I guess I am a little confused about your response. So if I am accessing the twitter api or something like the google maps api, I would setup a project for it at the presentation level? Would I not put it at a more core level if I wanted to both interact with and even store the api data (addresses after they have been cleaned up and verified by google maps), as much as I would like to display data at my presentation level (a twitter feed). Would I not control my calls to said API from something like a business layer?

      • user

        AUTHOR Darius Dumitrescu

        Posted on 9:27 pm October 1, 2014.

        Yeah, I guess my response is incomplete.

        It actually depends a lot on which type of API you want to integrate. You can have WCF services, you can have classic SOAP services or you can have some more lighter APIs like Google APIs or Twitter API.

        In case of WCF or classic Webservices, where you need to add a reference in Visual Studio, I envision them at Presentation Layer but in case of APIs like Google where you have to play with methods from a certain .dll I agree with you that needs to be added at Business Layer since you would need CRUD operations for them. I think you already know the answer 🙂

  • user

    AUTHOR Sim

    Posted on 4:35 am November 12, 2014.

    Hi Darius

    Thanks for this article.In the N-Tier architecture, we can put each seperate UI,BAL and DAL layer seprately in different server. but in the current architecture, we don’t have any service between BAL and DAL.

    Another question, what is the best approach for using wcf , I mean, best way to connect wcf service (proxy, channe, …)

    I thinks, If you you complete your example with at lest one method, it will be very usefull

  • user

    AUTHOR Justin

    Posted on 11:51 am June 4, 2015.

    Thanks for sharing the logical architecture of the N-tier .net application. It would have been good to have parallel example of physical architecture of N-tier .net application. I was wondering how you would be exposing the service layer from web application which is sitting in a DMZ zone?

    • user

      AUTHOR Darius Dumitrescu

      Posted on 11:54 am June 4, 2015.

      hi Justin,

      Thanks for your reply. I noted your suggestion. Maybe when I have some spare time, I will think about it.

  • user

    AUTHOR JB

    Posted on 6:47 am June 23, 2015.

    If I understand you correctly, the Data Access Layer is responsible for the database related operations only. And Business Component Layer contains code such as Repositories & UnitOfWork? Because they are responsible of converting the Data objects into POCO Entities and be the connector between the Service & the Data Layer right?

    Please correct me if my understanding is wrong.

  • user

    AUTHOR Darius Dumitrescu

    Posted on 8:48 pm June 25, 2015.

    Hi JB,

    Yeah. That pretty much sums up.

  • user

    AUTHOR Suthish Nair

    Posted on 12:51 pm November 5, 2015.

    Hi Darius,

    Great article.. Can we have a separate service layer, with WCF project or web services project (Services folder) or its mandatory to put under presentation folder?

  • user

    AUTHOR Darius Dumitrescu

    Posted on 9:01 pm November 15, 2015.

    Hello Suthish,

    The Services layer is optional. From a cost considerent, it might be an overhead for simple applications.

  • user

    AUTHOR suresh kumar

    Posted on 12:06 pm November 30, 2015.

    Hi Darius,

    Thanks for this article. Where to add third party Dll’s ? Like those dll’s are used to interact with some hardware devices…Is there any advantage to those dll’s in New Folder?

  • user

    AUTHOR Tanima

    Posted on 4:52 pm December 24, 2015.

    Hi Darius, I am developing a file IO and converter .net utility. An excel sheet will be provided as input, my tool will parse and read each row if that excel , carry out some calculations and then produce an XML file with calculated data as an output. My question is, there is no database connectivity here, so what would be a best way to design this app?

  • View Comments (17) ...
    Navigation