Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Isn't Dagger 2 for Android is not DI framework, but glorified Service Locator?

For example, let's say my Rest adapter created with Retrofit lives inside Application class. I would love to get it inside the Activity, so I write the following code:

public class MainActivity extends Activity {
    @Inject MyRestAdapter mRestAdapter;

    @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ((GlobalApplication) getApplication()).getComponent().inject(this);
    }
}

Granted, it will make the job done. But... How is this different from calling getApplication(), and then explicitly yank the MyRestAdapter to MainActivity? Yes, Dagger 2 will simplify the setup by automatically getting everything to the Activity, but you still need to explicitly tell from where you need these dependencies, and that, if I understand correctly, defeats the whole purpose of DI. Am I right to say that Dagger 2 is "semi-automated service locator", or it's just the tutorials that misled me, and there is correct way to inject dependencies with Dagger 2 into the View or Activity from Application?

like image 736
Alexander Woodblock Avatar asked Feb 02 '16 09:02

Alexander Woodblock


People also ask

Is Dagger a Service Locator?

Because of this, Dagger is not a clean DI but it does provide a good enough service in a special environment like Android. And now, we will look at Koin, why it is not called a Dependency Injector but a Service Locator.

Is Dagger a framework?

Dagger is a fully static, compile-time dependency injection framework for Java, Kotlin, and Android. It is an adaptation of an earlier version created by Square and now maintained by Google. Dagger aims to address many of the development and performance issues that have plagued reflection-based solutions.

What is the use of Dagger 2 in Android?

Dagger 2 is a compile-time android dependency injection framework that uses Java Specification Request 330 and Annotations. Some of the basic annotations that are used in dagger 2 are: @Module This annotation is used over the class which is used to construct objects and provide the dependencies.

Is Service Locator a dependency injection?

Like Dependency Injection, the Service Locator Pattern allows dependencies to be defined externally and apart from the application logic. But rather than injecting, these dependencies are made available upon request by the application.


1 Answers

I've been experimenting with Dagger and it definitely seems to blur the lines between service locator and dependency injection. This is at least true when used with Android activities. With the current version of Dagger, it is possible to write AndroidInjection.inject(this) in an activity's onCreate method. That's basically like saying "find all the services I need and inject them into me." So Dagger is a combination of a some central/global service locator that knows where to get the services and an injector that knows where (i.e. which instance variables) to put those services in the activity. It seems that Android activities force reliance on some kind of singleton/global object.

like image 128
Ted Henry Avatar answered Sep 22 '22 21:09

Ted Henry