Home android where and why you need to use DAGGER2

where and why you need to use DAGGER2




Today at work gave the project, Dagger there everywhere and everywhere! Previously, I did not come across this, since I never had the need for it. I read the Internet and realized that it was supposedly sugar for testing and, as it were, in the project everything should be smoothly and clean.

I had a very long time in the code and understood that it was a porridge without sugar.
And that all that is written there, you can write without Dagger much more compact, it is clearer for everyone and because the smaller the area of ​​the garden – the faster it can be swollen , this is the performance of the application.

The application works with API, stores data in the database and there are very few complex ui with animations. But everywhere there is RX! Straight everywhere, even where it is not required … Why !?
This project is going to long for a long time, no matter how I googled on this topic, everything is unsuccessful.
Absolutely, the application structure is generally distant from Clean .
Incomprehensible inject activity – why? Android already has all the tools for the start of activity. Class Application already singleton and it is not clear why Dagger offers to do the same.

I absolutely do not understand why you need a Dagger? What project must necessarily be built with its use? Or is it just another chip, which shoved into the project in pursuit of fashionable things?

I am surely sure that Dagger was superfluous in that project and I rewrote it without using it. The code has become much cleaner and the application has become more quickly.

Answer 1, Authority 100%

In the comments, I am somewhat excited about “unreal” write an application for 30+ screens without a Di library. A good example is a telegram, in their application under Android there is no third-party Di. Perhaps there is something self-written, did not study the code any carefully.

However, Di libraries still remain an extremely important thing, because:

  1. Constability . any Android-developer with experience of more than two years has experience with third-party DI. Moreover, the absolute majority is familiar with Dagger. Today you work on the project. Tomorrow you will get fired and will work another person. With Dagger, he will figure it out for sure (and will find thousands of tips on the Internet). And with your decision?
  2. Testing . What you read on the Internet. Testing with DI is an order easier than without it. And if you do not write tests, it is bad. Well, either your applications are quite simple. Either you are a genius that is not mistaken.
  3. clean. This is the cleanest architecture that you mentioned in the question. To build it, use the principle of Dependency Inversion . For the use of which, in turn, there is only one really common approach – Dependency Injection . And then you have a choice: to invent a bike, or take ready. And Dagger is a relatively tolerable finished option. (Personal Opinion: In my opinion, only a masochist will choose a Dagger if he previously used Koin or Toothpick and a choice at all)

Now a little bit on the side of the topic of the question.

  • About the performance of the application. It is not important. And not even the second. And not even the third in terms of importance. The application must be enough quick, not to deliver discomfort. No more. Because much more important, for example, for the application to be as much as possible quick in development and modification. Including the forces of developers who have never seen this application before. And more importantly, the application is beautiful. And it is important that it does not fall almost never. And just then there is a speed of the application. And that is not a fact. Therefore, it is necessary to avoid bicycle bicycles. In your fast system, someone later understand (and, probably, rewrite from scratch).

    It is necessary to understand that the above-mentioned above does not concern the super-broom applications developed by hundreds of developers. There’s everything important there.

  • This moment I remained somewhat incomprehensible:

    The application works with API, stores data in the database and there are very few complex UI with animations. But everywhere there is RX!

    I use RX in all projects for almost two years. And in general, never used it to work with animations. Either I, or you do not know something about RX. But I know for sure where Rx works great: this is working with the network, with a database, data processing and much more. And it is very convenient for working with UI (from the simplest to complex without restrictions). Yes, it is much slower than working without him. But Nebekki, what is it comfortable!

  • this is

    The project first is going to

    Of course, the problem. But not big. Example from work. The application is about 70 complex diverse screens, the non-trivial logic of working with data, a plurality of connections between the screens in all directions (such logic is requested by the business). The project on the cold is about 5-7 minutes. For hot – from 5 seconds, depending on the number of changes from the last assembly. And yes, we have everything in RX. And all on Di.

  • And this is just the wines of the previous developer. Di should help create Clean, someone just had hands from one place, did not take you with heritage.

    Absolutely noble the structure of the application, is generally distant from Clean.

  • And here I would argue.

    Application class already Singleton and it is not clear why Dagger offers to do the same.

    Number of problems resulting from incorrect operation with Application , do not count. Yes, at least the same memory leaks from the links stored in it. Activity . You, quite possible, did not make a single mistake. But why risk?

  • here is here

    Another chip that shoved in the project in pursuit of fashionable things?

    You are a few wrong. Although, of course, it depends on the time of writing the project. But now Dagger is more likely from fashion being already spent its old-kind friend. Its popularity is reduced.

  • Well, I will try to answer, probably the main question:

    What kind of project must be built with its [Dagger] using?

    Answer: No. Five minutes to google and you will find a dozen worthy replacements. But you do not build a normal project without DI. Let it be self-written, but in a major project it will be. Either the project will not happen. Either you are a genius and invented something to which none of the developers for 50 years did not think.

Well, some information for reflection. In order to understand the meaning of the DI and RX, you need to work with them. At least half a year. Every day in full-length 8 – hour mode. This is a purely of personal experience and conversations with colleagues. Somewhere after that you begin to understand something. Faster from my friends no one has mastered. But these are the knowledge that is really needed. And these are not new-fashioned things. This is essentially tools to create really Clean projects. There are other tools. But today almost everyone uses di and almost half – Rx. And so far from the first year. So things are proven.

Something too sheets turned out, well, well. I will try to answer extra questions if you ask them in the comments to the best of your knowledge and strength.


Similar question has already been set on Ruso Why do you need-dependency-injection container?

And there is still a small one, but useful to be on Habré

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions