Dependency Injection and Service Locator

Discover the secret of Service Locator in the world of Dependency Injection frameworks

Lam Pham
moduloTech

--

Location

Dependency Injection (DI) is a topic that has always been in discussion in the Android community. Personally speaking, DI is not a notion that is hard to understand, and its implementation should not be complex either. However, Android developers still keep talking, discussing and fighting for a clean solution.

From my point of view, despite all the efforts from the community, we are still not getting a satisfying answer to this matter. One of the biggest barriers is the fact that Activity and Fragment are not supposed to have argument constructor (With AndroidX Fragment Factory this might change)and these components can be destroyed/recreated at any time in an application’s lifecycle. This makes implementing an ideal DI in Android almost impossible.

A few years ago, we did not have many choices for DI’s framework: Dagger was the only dominant one and it was also officially recommended by Google. It goes without saying that Dagger is rather complex to implement. Some people feel comfortable using it; but others — and not a small number either- are not. Why did I say so? Well, first of all, Dagger is hard and we did not have a choice, as I have mentioned earlier.

--

--