Android Combat--dagger2 a story for bosses and employees

Source: Internet
Author: User
Tags prepare

This article mainly contains the following content

  • The new company opens: Preface
  • New Project Meeting Discussion: Introduction of Dagger2
  • New Employee onboarding (Hensen): Dagger2 basic Use (@Component, @Inject, @Module, @Named)
  • New Employee onboarding (Jenny): Modular Development of Dagger2 (@Module, @Singleton)
  • Boss is angry (boss): Dagger2 Global Singleton use (@dependencies, @Scope)
  • Employees have resigned: source Download, conclusion

Finally to the Hensen teacher to blow the water time, perhaps you look accustomed to the text description of the Dagger2, see a long time will be visual fatigue, because I also come over, so today in a way, make up a story will form to let the audience change the taste. Dagger2 is not a general difficult, but difficult, as a novice level of Hensen teachers can only for you to go to Dagger2 exploration, a few days, a look at the following I will be the results of the expedition to tell you

Finally, our company opened, the name of the new company is called the Network Technology Co., Ltd., the following is the character introduction

    1. Boss: Bosses, big God-level characters
    2. Hensen: Veteran staff, responsible for the project's user module
    3. Jenny: Staff at the Ashes level, responsible for the project's network module

Boss: Today just found a good project, hey, financing a 2 million, it is time to recruit in the project made out, I should think about the project with what framework good. See the recent Dagger2 so fire, it's better.

1, the introduction of Dagger2

Dagger2 is a dependency injection framework, and the dependency injection framework is the best decoupling tool for the MVP architecture, not only to make your code more modular, but also more organized

2, Dagger2 's dependence

The dependencies used in the project, if you are Gradle2.2 below, need to add apt plugin, etc., here does not demonstrate, Gradle2.2 is a long time ago, I believe many people do not have

3, the basic principle of Dagger2

Here can be simply understood as multiple modules (module) through component directly injected into the activity, we can compare multiple module to become a different needle potions, component analogy to needle, container analogy for the buttocks, We mix it with different needles, put it in the syringe, and hit our asses, that's the basic principle of Dagger2.

4. Annotations of Dagger2

Here's a quick introduction to the annotations we need to use.

    1. @Inject: This annotation is usually used where you need to rely, that is, the process of needle water hitting the buttocks
    2. @Module: Modules is similar to our module, which is a different kind of potion, providing a variety of examples and objects
    3. @Provides: In modules, the method we define is to use this annotation to tell Dagger2 what instances and objects we want to provide, that is, the manufacturers that become different needles.
    4. @Component: Injector, @inject and @module Bridge, its main role is to connect the two parts, that is, our syringe + needle
    5. @Scope: Scope used by Dagger2, local or global

5, Dagger2 three-step song

    1. Create a module that provides a usage instance
    2. Create component to connect the bridge between module and activity
    3. Inject injected and started to inject in the activity

Hensen: Good, boss, today is very happy to come to the Network Technology Co., Ltd., do you have any task to command it?

Boss: Come on, take Dagger2 as the framework of our new project, you are in charge of the user module, you will finish it tonight

Hensen: Okay, boss, I'm on my way to work.

Hensen was used to the MVC framework, so he first set up the MVC and Dagger2.

1, the main logic: the first is to define a few controllers as the main functional logic

2, module creation: Just finished learning Dagger2 Hensen, know Dagger2 rely on injection of ideas, with their own to hit the butt needle is the same reason, now need needles and needles to inject potions into our buttocks, where the buttocks is activity, The controller will be in the activity.

3, conmponet create: Prepare the potion, prepare needle needle below

4, inject injection: Here are ready, this time must pay attention to remember rebuild the project, it will generate the code we need , then you can start the injection

Run the program, the console successfully prints out the information

5, the use of named: To do here, Hensen has done the general, the remaining one usercontroller do not know how to do, because he has two construction methods, Dagger2 will go to the default will go to call which construction method, so Hensen began to find information, He found it possible to do so, and he used the @named method to provide two different construction methods

Then the same name should be used in the same time.

Run the program, the console prints out the information

6, the use of custom named: Hensen is a pursuit of the perfect person, think the program can also put this named again, so it looked at the source of named, copied a copy as their own comments

So it looks very comfortable to use, the effect is the same as above, but in a different way

This is no problem, Hensen's work is basically finished, and the following can give the boss to deliver the task

Jenny: Hello boss, I am Jenny, today is very high-hearted to the company, I am personally good at the network this piece, what tasks can I command?

Boss: So ah, then you write a network module, let Hensen user management connected to the Internet chant, the best use of okhttp

Jenny: Yes, well, boss, I'll go to work first.

Because Jenny's dagger not good, so can only read Hensen code, and then according to the meaning of the simulation of their own set of dagger out

1, the main logic: the first is to create a network module responsible for the main logic, provide get and post methods

2, module creation: Here is for Hensen ready controller instance, directly add this module to be able to use

The first is not very understand Jenny, let Hensen directly with their module, its meaning as shown below, you can directly put their module into the Hensen, but Hensen in order not to offend the new companion, it added, it knows this does not do well, And also persuaded Jenny to write component.

3, the creation of componet: let this module into another activity, here to note, if the other component use inject method also passed loginactivity, here will be an error, So a componet can correspond to multiple inject to the activity, but cannot repeat

4, module use: Hensen received Jenny good network module, directly add it to loginactivity in the good, note Remember to rebuild once again will generate the corresponding daggerhttpcomponet

5, the use of Singleton: Hensen as the old staff, know okhttp can not be repeated creation, let Jenny put it to a single case, Jenny sounded also very reasonable, so on the data, how to do a single case in Dagger2, it according to the method of data found below, Add the @singleton Word to the method that provides a singleton in the module, and then add the @singleton Word to the componet that uses it, and then complete the

6, but soon, Hensen found a headache, because useractivity use is usercomponet, loginactivity use httpcomponet, they create a httpclient is a different object. Only the activity using httpclient in the same componet is a singleton. This is because Singleton is bound with componet, different componet are definitely different singleton, so there is no way . Hensen is checking for problems by printing addresses

Boss:jenny, you this single case can not be like this, in different componet will appear different singleton, you have to put it in application, so that is the overall is only one

Jenny: Boss, this. I can't, what do I do?

Boss: Well, it looks like I have to play.

Boss also began to write a set of app-level dagger out, and provide okhttp single case out, Big Boss come

1, module creation: The boss starts from the demand to write, needs to give what, and is @singleton single case

2, the creation of component: The boss also know Dagger2, so do not hesitate to take the second step

3, application create: The boss know that only in the application variable will be instantiated the entire program once, so put here okhttpclient Global is a singleton is completely right, Remember to re-rebuild once to generate the corresponding daggerappcomponet

Of course, how can the boss make a small mistake, remember to register the MyApplication in application

4, the scope of the creation: The boss personally wrote a scope, told us, then do not use Singleton, with the activityscope I provided, so that the single case will be effective. here is because Singleton is also a scope,scope and scope can not conflict, that is, the boss used the singleton, we will use the error, we must use the boss to provide additional activityscope to not error

5, Jenny also secretly gave their own okhttpclient commented out, here to do so in order to follow the procedure does not give an error

6, the use of dependencies: the boss wrote well, we will start using the boss, Hensen as familiar with the Dagger2, of course, is the first use for the good, the following is the Hensen modified content, here need to pay attention to the boss of the module in advance, Before you can use the. appcomponet (Myapplication.getappcomponet ())

7, will not Dagger2 Jenny also began to imitate Hensen's writing, began to modify their own code, the following is Jenny modified code

8. Hensen, by the way, also embeds Jenny in her activity.

9, the last Hensen through the printing information found that the boss is really a single case Ah, finally solved the different component will not appear in different singleton objects

Hensen: Boss I want to resign, every day overtime, pay less, workload and so much, and this Dagger2 too annoying, make me scalp numbness
Jenny: Boss, I'm not going to Dagger2, I'm quitting.

1. Source code Download

2. Conclusion

Study Dagger2 really is a face Meng, if not a certain thinking ability or very difficult, IELTS finally staff can not endure Dagger2 so complex things, directly resigned, haha haha, so say the choice frame is very important, if you do not mind study new technology, Or first put Dagger2 let it free to go, lack of a also not lack of, or to learn cost-effective relatively high frame bar

Review the contents of this lesson:

      1. @Module, @Component, @Inject Basic use (Everyone)
      2. @Named create an instance of a different parameter (Hensen)
      3. @Singleton complete An instance of a single component (Jenny)
      4. @Singleton and @scope maintain the same instance (Boss) in different component

Android Combat--dagger2 a story for bosses and employees

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.