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
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)
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
#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
#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
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