My journey into software architecture with ruby
As a ruby practitioner here is a report on how I began my journey into software architecture over the course of a weekend.
I am currently working on a web application with Rails. As the application grows, my ActiveRecord (AR) models grow with all the business logic in it. It is obvious by reading the tests linked to these models that my classes violate the S in the SOLID principles.
After a few searches it seems that my problem is very common and well known by the more experienced rubyists : I developed the Rails app syndrom.
ActiveRecord all the things
The most extreme example I found is a class derived from AR that have no state to save in the database. The
SpongeBob class whose reponsibility is to instanciate other AR objects by parsing the content of a markdown string and to persist the state of those objects into the database. For sure there is no point for
SpongeBob himself to inherit from AR.
Looking for inspiration I began to search for ‘service object’.
SpongeBob to Uncle Bob
My first hit although not very convincing brought me to two very interesting resources :
- 7 Patterns to Refactor Fat ActiveRecord Models
- and somewhere in the comments a conference by Uncle Bob from 2011 : “Architecture the Lost Years” (~1h).
During his show Robert passes a few messages:
The web is a delivery mechanism
The top level architecture should reflect the intent of an application
What I like is the boundary he draws around the application. As an engineer I like to think about systems by first defining their boudaries, then it’s easy to observe what flows in and out and to understand the behaviour and responsibility/authority of the system. To have the application being able to live on its own, decoupled from the web framework and the database sounds like a dream to my Rails oriented mind. But what a nice dream to have.
At the end of the talk I had more questions than answers. Why everyone seems to continue to live inside Rails ? How to properly turn Rails into a plugin of my application ? 4 years after the lost years where are the working implementations of these ideas ?
Go read some code
It wasn’t long before I crossed the path of Trailblazer which seems to be a very comprehensive set of components in which Rails can be easily plugged in. Nick Sutterer even wrote a whole book about it. Clean. isn’t it?
Lotus Container arch is heavily inspired by Clean Architecture.
Extracted from these slides by Luca Guidi
I find it really nice to have a lightwheight and modular framework made purposely to play nicely with the concepts of the Clean Architecture. I followed the getting started guide and it doesn’t feels less productive/intuitive than the Rails way. After digging deeper in its APIs it may become my next framework of choice.
It is only the beginning
That was a lot of stuff for me to digest in a single weekend, but also a lot of fun. I am very happy with what I learned and the perspective I gained.
Back to my current app, I will start small by extracting my application specific logic from my AR models into Active Interactors or something similar.
Of course I continue to experiment with the Lotus framework. And may use it for a side project very (very) soon!
If you want to react, complement or question this article, ping @em_hack