The role of injectors

We just got to know what a dependency injection is and how it receives dependencies from an external client rather than creating them itself. However, the client needs to update its code to pass an instance of 20 MP Camera and 4G Internet dependencies. Any client that wants to consume the Mobile class must create instances of Camera and Internet as the Mobile class depends on them. We eliminated the responsibility for creating the instances of dependencies from the Mobile class and moved them to clients that will consume the Mobile class.

Now, it has become the poor client's problem to create the instances of Camera and Internet. So, to reduce the extra efforts of a client in creating the instances of dependencies, we need injectors that take care of assembling instances of the required Camera and Internet for the client. The dependency injection framework has something called an injector where we register our classes, such as Mobile. We can then request the injector to create the instance of Mobile for us. The injector will then take care of resolving the dependencies and creating mobile, as shown in the following line of code:

var mobile = injector.get(Mobile); 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset