In this article, we are going to go through a couple of solutions to a common problem when developing mobile applications, and this is Dependency Injection.
We all saw a project that was calling singleton classes from any part of the code without any security when writing data to them, battling with race conditions, and coupling implementations to all these issues. You can also find many third-party libraries that can help to solve or at least manage these problems, but many times we don’t want or we can’t add extra dependencies; in this case, a good approach is to implement the solution that fits your needs in a basic but sufficient way and with native code.
To start with this, we will go through the problem first and see what would actually help us. Let’s say we have a class called AViewModel that needs to use ServiceA; the first thing to think of is to instantiate the service in the initializer or directly inside the class like this: