Abstracting Like A Boss

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.  



Say Hello To The New Mercenary

Two month back, I was the type of dev that had to be passionate about an idea or product to contribute just a line of code. I could dump a project because the project owners or team where not obsessed with commit standards, PR standards, clean code architecture. If my idea which I always thought was better, was not picked I could get really pissed and decide to leave. So for the majority of 7 months I decided stay away from any other project, except my full time job and a little side gig of mine where I had at least 80% control of certain decisions. 

An incident occurred on April that really humbled me, an experience i will never forget for a very long time. My full time job delayed salary payment for about 3. During that period I had to crash at my close friend's place just to be able to eat, my family too were affected and some folks on my payroll. 

I wrote the most code i've ever within that 3 weeks all year, I had to source for side gigs and for the first time in a long time I was humble to accept things as the were. No pride or ego, I just wanted one thing, to get paid to write codes. I got onboarded into projects I disliked their architecture or the way they had done certain things, but I did not care. I was willing to learn it that way, write my codes that way just to survive. To be honest I had a mental shift, I learnt a valuable lesson┬á´╗┐"Passion and Obsession should never stop you from making a lot of money". ´╗┐I'm sure this might change, I might write another post saying not to pay attention to what I have written here and that is fine, but for now and in this moment, the money is important, the passion for doing it right can come later.´╗┐