The Injection

The first topic is dependency injection.

...

I first got introduced to this when I was working on an android application for my six-month internship. I had to paginate a huge amount of images that I would get from an API and display it on a page in the mobile app. So on clicking one of those items, I had to display that on another page enlarged. And when I swipe left or right I need to move to the next/previous image(kinda like a gallery). That means I needed the list of images on this page but I would get them on demand(coz I paginate that)!

Now from what I could make out, this list should be available on this page too which meant I had to pass this as an argument and continue the pagination there. I could not initialize the list there, because it was a PageKeyedDatasource(loads data by page numbers). Here is where Dependency injection comes to play.

I had an object x on a page that depends on another object y on another page. And for that object to be available, I had to either declare an object where y is there(not a good idea & not efficient) or had to pass y as a variable either to a constructor or use a setter(user's need and comfort).

Essentially we are injecting just the required things that we already have & removing the need to redeclare) to the place we need without worrying about what happens to the rest of it. And since x depends on y, voila dependency injection. There are many frameworks that implement this(maybe I will try to cover that too) like Dagger for which there are multiple blogs.

Although I ended up using something entirely different which I will detail in the next post, this helped me to understand quickly what was happening when I was working on a spring boot project where you can auto-wire(inject) constants from the properties file.

This saves up lot's of time and reduces the code, that can be utilized to think and design better systems.

...

Have a good one!