Answer the question
In order to leave comments, you need to log in
Why is Dependency Injection needed in Android development?
Actually the bottom line is that I can not fully "stick" in this topic. Watched a bunch of videos, read a bunch of tutorials. I don't know, maybe it's because I'm a little tired and not thinking, but, nevertheless, many people say that Dependency Injection is a fantastic thing that helps a lot in development. And here I sit and think: "Is it convenient?".
In short, guys, explain to me that I'm 5 years old, and I also have a suspicion of an extra chromosome: "Why do I need Dependency Injection?". It is highly desirable that, using the example of Dagger2.
In advance, I thank you for your understanding and I beg you, do not throw stones, we are all sinners of neosilatory (in one way or another))))
Answer the question
In order to leave comments, you need to log in
It would be nice to start with an understanding of what DI is. Let's go to Wikipedia:
If expressed not in a clerical way, but in ordinary Russian, then DI is when you provide your component (for example, a class) with the dependencies it needs from the outside , and do not create them yourself in the constructor, or through initialization at the place of the field declaration. That is not so:
public class Api {
....
private final HttpClient client = new OkClient();
}
public class Api {
....
private final HttpClient client;
public Api(@NonNull HttpClient client) {
this.client = client;
}
}
I don't have an answer, but a clarification of the question.
Why DI on the server (Spring) is clear.
But in Android there is always one user, one session, and there is always a singleton Application, where a lot of things are initialized and which is available everywhere through Context. And whatever one may say, Context is everywhere and there is nothing terrible about it.
Why is the option of using one module (Application) that provides all the necessary dependencies bad?
Moreover, even when using dagger, the same Application is still twitching everywhere to get a component to call inject.
PS. Also, examples on Dagger are a little confusing where this very context is injected, why inject it if it is already everywhere?
если кратко, то для того же что и везде, вы главнео не смотрите на теоретиков кайфа, начинайте с малого, работа с бд, с апи ... дальше поймете где нужно
Artem Gapchenko все хорошо рассказал, от себя добавлю только Дмитрий Полищук. Dagger2 практический ликбез по ра...
Имхо, хорошо о IoC и DI написанно в книге Spring 4 Для профессонилов. Понятно что там все по другому, нежели в Dagger, но теория хорошо описанна.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question