Layered Architecture for .NET

Layered Architecture for .NET

Rate This
  • Comments 4

The Layered Architecture Design Pattern promotes the concept of separation of concerns where code of similar responsibilities are being factored into layers. It is purely a logical design but it can be combined with physical design patterns such as the N-tier architecture to deliver highly scalable and impressive distributed enterprise applications.

Data Layer (a.k.a. Data Access Layer or DAL)
The data layer is where we keep our components that handle the insertion (Create), selection (Read), modification (Update) and deletion (Delete) of data - or better known as CRUD operations. While it is simplistic to think that data usually comes from a database, in reality, data can come from or go into various sources as well i.e. Web Services, Flat Files, Message Queues, XML files, SharePoint Lists etc.

Therefore, components that deal with database tables are called Data Access Components (DAC) and those that deal with other data sources will be called Data Agents (DA) i.e. Service Agents, File Agents, Queue Agents etc.

Data Access technologies that you can use in .NET are ADO.NET, Enterprise Library Data Access Application Block and ADO.NET Entity Framework.


Business Layer (a.k.a. Business Logic Layer or BLL)
The business layer is where the heart of our application resides. It contains all the processing logic to make the application possible. The Business Component (BC) is where you put these processing logic where each can be coded into independent business methods. Traditionally, we are required to chain-up the business methods manually in code to form the business process but fortunately today, we have workflow technologies.

If you can isolate each business method to function on its own, you can exposed them as a Workflow Activity (WFA). These workflow activities can then be used by a Workflow (WF) component to orchestrate the business processes.

Windows Workflow Foundation is the workflow technology that can be used in .NET.

Services Layer (a.k.a. Messaging Layer)
The services layer plays the most important role in the architecture to enable the functionality of the system to be exposed to client and external applications. It is also the key to achieving multi-platform and interoperable solutions.

Services components expose the functionality of business components or workflows via Contracts. In the Services-Orientation world, contracts are the interfaces where both service providers and service consumers agree on and should be immutable. Contracts are not just limited to describe the service and its operations but can also be used to describe the messages (i.e. Message Contracts) that are to be sent and received.

You can use Services (SI) to represent components that expose business components directly and Workflow Services (WFS) to represent services that expose workflow functionality. The reason for this is because Workflow Services are usually long running and may have special requirements such as correlation.

Services technologies that can be used in .NET are Windows Communication Foundation (WCF), Workflow Services and ASP.NET WEB API.


Presentation Layer (a.k.a. User Interface Layer)
The presentation layer should not need much explanation. It is basically the part of the system where the user interacts with. Your screens, forms, web pages and reports are all User Interfaces (UI) which are part of the presentation layer. User Interfaces can make use of User Process Components or Controllers (UIC) to communicate with the back-end and to navigate or process the UI.

A carefully designed layered application should be able to support any form (or platform) of UI. If you are able to encapsulate all your processing logic behind the service layer, you can have whatever UI (Web, Desktop or Mobile) that you desire to connect to it - even UI-less external systems.

Presentation technologies that can be used in .NET are Windows Presentation Foundation (WPF), Silverlight, ASP.NET Web Forms, ASP.NET MVC, Windows Phone, Windows Store Applications and Windows Forms.


Shared Types
So far we have covered the components in all the layers but we have not yet discussed about how data is being passed between them. Traditionally, .NET developers use DataSets and DataTables but these are heavy-weight objects. Entities are Plain-Old-CLR-Objects (POCO), which means they are just classes with properties that ferry data across your layers. Sometimes, they are also called Data Transfer Objects (DTO).

It is recommended that you do not put any processing logic inside the Entities. If there are any processing logic, they should be placed in the business components. The reason is because when entities are being serialized to non .NET platforms, your processing logic may not carry over.

Some property values in entities can be represented using Enumerations (Enums) for easier readability. Example, it is much better to strongly-type your Status property with an Enum to show meaningful statuses such as Pending, Cancelled or Approved, instead of 0, 1 or 2.


Frameworks (a.k.a. Cross-cutting Framework)
In every system, there are bound to be code that can be shared across all the layers i.e. logging, auditing, validation and etc. You can treat these components as Framework components that can be shared by any of the layers. Framework components can be from 3rd-party (i.e. Microsoft Enterprise Library) or any custom in-house built components i.e. string manipulation functions, custom validation functions, extension methods etc.

 

For more details about Layered Architecture for .NET, please feel free to drop by to our MVP Serena Yeoh’s blog here.

  • This is how we wrote applications in 2003 (see: .NET 1.0). I guess everything ancient is new again.

  • Dear Microsoft Malaysia,

    please write a system following your own advice. And put it into production. And maintain it for a few years. And report back your experience. I'll wait...

  • We've created and maintained 2 different large scale systems in production 5+ and 9+ years, one as SaaS using a very similar architecture. This is actually a pretty good option for very large, long-term projects (i.e., not just a web app here or there). Also, this doesn't exclude the use of other designs for the UI layer such as MVC/MVVM. If you try to use just MVC or related for a large project you quickly realize that there is a whole lot hidden behind the M (for example, all services, business logic, DAL, cross-cutting, etc.). Not saying that can't be accounted for, but MVC typically doesn't do a lot to prescribe the best way to organize large complex systems and services 'behind the M' if you will. That's not the purpose of MVC, it is a UI-oriented design. Also, not saying this is the only viable option, but it is definitely something to take seriously for larger long-term systems where you have huge amounts of complexity to manage over time (along with good process, unit testing, version ctrl, etc.). Anyway, just wanted to give some insight into working systems using a very similar model very successfully. If you have specific criticisms/questions do post. There are definitely pros/cons to everything, but I'm willing to stand behind layered architecuture in general.

  • Dear Microsoft Malaysia,

    I'm confused. You start with:

    "The Layered Architecture Design Pattern promotes the concept of separation of concerns where code of similar responsibilities are being factored into layers."

    Could you elaborate on the following points?

    1. How layered architecture is a design pattern? I always thought that layered architecture is one of many architectural patterns. Design patterns being "general reusable solution to a commonly occurring problem within a given context in software design" (en.wikipedia.org/.../Software_design_pattern).

    I would appreciate if you could describe or point me to some resources about Layered Architecture as design pattern.

    2. How separation of concerns results in "similar responsibilities being factored into layers"? I always thought that separation if concerns means exactly the opposite: moving similar responsibilities together and separating different responsibilities into separate classes/components.

    Thank you.

Page 1 of 1 (4 items)
Leave a Comment
  • Please add 2 and 7 and type the answer here:
  • Post