What is Clean Architecture?
Clean architecture is a software design philosophy that separates the elements of a design into ring levels. An important goal of clean architecture is to provide developers with a way to organize code in such a way that it encapsulates the business logic but keeps it separate from the delivery mechanism.
Clean architecture was created by Robert C. Martin and promoted on his blog, Uncle Bob. Like other software design philosophies, clean architecture attempts to provide a cost-effective methodology that makes it easier to develop quality code that will perform better, is easier to change and has fewer dependencies.
You can watch our video for a brief intro about Clean Architecture at Hackney Council:
We have 2 project folders:
- The actual project implementation
- The Test Folder - which will mirror the implementation
If we look at our implementation Folder, we will notice that our code is structured into various code files and these files are all categorised, containing different subfolders.
Data Structure Folders:
- The three highlighted lines above are the behavioural folders
- This folder will hold all of the structures of data that we will expect to get in return to the calling client
- We use this folder to GET a Request or SEND a Response Make sure the way you structure the object response is clear and easy to understand
- In this folder we set up the End Points for our APIs
- This folder is used to manipulate data
- In here we do any calculations within our app
- In here we do our conversion from one type of data structure to the next one (e.g: convert a domain object to a response one)
- The responsability of the Gateway is to handle the interaction between your API and any external dependancies (such as another API or a Database)
- Gateway also has to be able to manipulate data in some way
- In this folder, we set up the data structures that our Gateway will use
- The UseCase
- This is where we handle all of the behaviour/logic in our application.
A simplified diagram of how we process requests in our APIs
Generally, in Gateway and Use Cases, we tend to have Interface Folders.
- When your API interacting with Boundaries, we don't interact with classes directly, but with the Interfaces
- It makes the app more testable
- It allows us to do dependancy injection (we can set up an interface and this gets injected in the app whenever it's needed)
- The Interface Folder defines the contract for your implementation
- Any class that implements an Interface MUST have the 'Execute' method, otherwise your program won't compile properly