Introduction
I have been obsessed about abstraction in software development since I became the Engineering Lead at Changera. In April 2022, I was promoted to lead Bitmama's sister company, Changera, now Changera Global. My first most important task was to rebuild the entire API. The api was initially written with PHP, then it was rewritten by the CTO and another developer to NodeJs. I came in and I immediately disliked the way things were done. I spent a year contributing to the API that powered Bitmama exchange, and from the lead developer at that time I understood how consistency and structure was important to the success of a working backend system.
My first task was to rewrite the entire system to a microservices, oh boy a real challenge🤤🤤. I needed to prove myself, at Bitmama the lead developer set the terms and I followed, but now I get to set the terms. It was exciting, I did as much research I could do on microservices. Thats where I got to learn about clean-code architecture. I love the level of independence clean code architecture provides. The idea of separating your business logic from framework or dependencies was just so beautiful. I have worked on a lot of project where I had to change database drivers or ORM mid project, the toll it took to rewrite each functionality was heartbreaking. I started exploring clean code architecture in 2022 and I think I perfected it in 2024. I would be be breaking down some of my techniques in this post.
My favourite development tool right now is NestJs, you can read up about NestJs here. NestJs is the best framework for building scalable NodeJs server side applications. Concepts like dependency injection, microservices are easier with NestJs. If you love playing around with weird concept relating to software development you would fall in love with NestJS.
Technique
- You first need to to startup a Nest Js project. You can run git clone https://github.com/nestjs/typescript-starter.git project on your terminal. You can familiarise yourself with NestJs by going through their official docs.
- Create a framework folder in your src folder. The framework is where you store dependencies, tools that powers a functionality. For example I have a project and I don't know whether to use TypeORM or Sequelize. I can write both implementation and just inject it into my business logic using an abstract class. I can do the same with emails, redis and notifications functionalities.
- Create a core folder in your src folder. This folder contains your entities, dtos, enums, types, interfaces and abstract classes. Your abstract classes is the gateway to interact with your dependencies set up in your framework folder.
- Setup your controllers and services. I prefer the controller has its own folder, away from the services. I believe its more cleaner that way. Adhering to the principles of clean-code architecture we can create a user-case folder in our services folder. The user-case folder will contain the functionalities of our app. For example if I need to work on an authentication functionality, I can create an auth folder and write my business logic in it.
Deep Dive
- I did a code sample on this repository. You can check out the full source code here.
- The generic repository for Sequelize and TypeORM contains the entire core database implementation, then injected into this abstract class to be used in our code logic. You have access to all the methods implemented in the generic repository can be used when you import the DatabaseServiceModule into your functionality module.
- Here is a sample of how to make crud operation after injecting the generic class to the abstract class
- If you focus on any of the db function, and ctrl + click, it should go into the abstract class definition. Our entire business logic is separated from the business logic defined in our use-case folders.
Going To Bed😴😴
My post might not be too technical but you can clone my repository and play around with my codebase to understand how clean code architecture works.