Design an Application with 3 Layer Architecture

What is and How to design an app with Layer Architecture Paradigm

In this post, we’ll cover some basic aspects of how to design an App with Layer Architecture

This post is originally posted at https://petran.substack.com

What Is Layer Architecture

Layer Architecture is an approach to separating a code base into logical layers.

Each logical layer :

  • Have a hierarchical order
  • layer-1rst β€”> layer-2nd β€”>layer-3rd β€”> … β€”>layer-nth
  • Have a specific concept that characterizes all the code that contains
  • Can only depend on the lower layers (dependency flows only downwards)
  • Have components that can communicate with each other ( try to avoid circular dependencies)
Sample N-Layer

Design an App With 3 Layer Architecture

Let’s assume that we want to build a REST full app for serving some endpoints with the help of a framework such as Laravel or Spring Boot or some other framework.

We are going to create 3 Layers with distinct responsibilities

#1 Application Layer

This layer has the responsibility to include all code that needs the app to live and run.

The application layer contains :

βœ… All the code from the framework that is related to running the app

βœ… Controllers we build to serve endpoints over HTTP

βœ… Custom middle-wares we build

Sample App Layer

#2 Domain Layer

The domain layer contains all the code we write to run the business requirements (domain logic).

Domain layer is the heart of the application due that contains all the critical business logic

βœ… All domain logic

❌ Direct access to the database

❌ Direct calls at 3rd party services with HTTP

❌ Direct access with file system

❌ Direct calls with external packages

❌ Anything that is not pure business logic

Sample Domain Layer

#3 Infrastructure Layer

The infrastructure layer contains all the code that isn’t related to the Application and Domain layer.

βœ… All the Database operations

βœ… Integrations with 3rd Party services

βœ… Logging mechanisms

βœ… File IO operations

βœ… Email operations

βœ… External Packages

βœ… Application utilities such as date-time, sorting, UUID generators, and other utils

Sample Infrastructure Layer

How to Add Code On Layers

New code must follow our design philosophy and the context of each layer.

Bind New Code Blocks with The Layer Design Architecture

For example :

  • New controllers for REST endpoints β†’ App Layer
  • UI views (HTML pages, template engine files, etc) β†’ App Layer
  • All business Use cases go to β†’ Domain Layer
  • All Services that support business requirements β†’ Domain Layer
  • Queries with databases β†’ Infrastructure Layer
  • Integration with 3rd Party Systems (Stripe, e-mailers, Google APIs etc) β†’ Infrastructure Layer
  • Reading and Writing at file system β†’ Infrastructure Layer

Keep In Mind That

The layers are mainly logical, this means that the code on each layer might not have a clear separation on the file system

Why Design And Build An App With 3 Layer Architecture

βœ… Have a clear separation of concerns between the layers

βœ… Have a clear dependency flow

βœ… Have a clear view of where to add new code

βœ… Isolate failures on one layer

βœ… Increase the testability of the code base

βœ… Increase code base clarity and DX (development experience)

βœ… Dependency Isolation of your critical Business logic code

--

--

Petros Koulianos πŸ’€β˜ πŸ‘½

Software Engineer πŸ‘½ | Building applications for health industry | Work with JavaScript, Typescript, PHP | My NewsletterπŸ“© at petran.substack.com