听说你又一次放弃了Dagger2,来听老夫说道说道

关于Dagger2是啥,用法网上已经有很多了。不过听说你已经很多次Dagger2从入门到放弃了,来来,让我这个老中医给你治一治。老夫手法纯熟,不会让你有任何痛苦的,放心读下去吧。

诊断

咳咳,看你这架势病的不清,应该是陌生代码恐惧症,你看看下面的代码。会不会害怕。

喂!!!你怎么跑了,快回来,还没治病呢!!

用法回顾(注入第一式)

首先,对于一般来说,想创建一个对象,我们肯定采取new的形式:

但是这样耦合度较高,presenter的变化受限于Activity,也就是说假如我们的presenter的参数变成三个了,又要改Activity代码。所以使用Dagger2的好处就很明显了,如果改了构造器,只需要改对应的Module就可以。

@Module

上面提到了Module,这是什么呢?其实这是一个提供参数的地方,将new Presenter所需要的参数,写到这里面来了。看示例代码:

这是Module的写法,就是把new Presenter(Context,View)写到了里面。

@Inject

Module怎么知道presenter构造器需要几个参数呢,答案是@Inject ,只需要在Presenter的构造器注解@Inject

@Component

连接Presenter和Module的东东,我们只需要写一个接口,来返回Presenter对象,Dagger2就会自动生成对应的对象给我们。

最终联系

现在看回第一段代码

DaggerShipDetailComponent这个类是build之后Dagger2自动为我们生成的类,查看源码它实现了ShipDetialComponent这个方法,所以inject()函数自然会为我们返回一个presenter来啦。

用法回顾(注入第二式)

你可能犯嘀咕了,你这种写法不科学!跟我看到的不一样。咳咳,不要急,老夫再给你说另一种用法。这种方法的代码如下:

对应的Component为:

其他的写法都一样。 此时你心里肯定犯嘀咕了,么么的,你刚才说是inject()返回presenter所以可以赋值,这个void你怎么解释! 是不是又要放弃拉,年轻人,不要轻言放弃,听老夫一言。

想要知道Dagger2 怎么做的那么神奇,必须知道他的原理,原理怎么看?当然是看他生成的代码呗!

Dagger2原理

第一式解密

首先看第一种方法的原理,找到DaggerShipDetailComponent 发现代码如下:

可以看到Component确实实现了我们定义的Component,所以他的inject()方法肯定是返回了一个presenter

老夫没有骗你吧,至于这个provider如何拿到的newPresenter,这里给你留个家庭作业。现在应该有些头绪了吧,Dagger2没有想象中那么难,对不对?接下来看看让你懵逼的第二式。

第二式

直接看生成的代码!在DaggerShipDetailComponent发现了

继续往下看,看到了一个init方法

接下来顺藤摸瓜,找到了ShipDetailActivity_MembersInjector,只看关键部分源码

相信聪明的你已经明白了,为什么返回void还能注入,不知道你有没有发现,第二式的presenter没有用private修饰,在injectShipDetailPresenter(ShipDetailActivity,Provider<ShipDetailPresenter>)的时候将 activity.presenter = Proivider.get()!

如果让我用两个字来形容的话,我只能说

太他妈精彩

嘛,这次会诊就到这里吧,回家记得勤加锻炼,早日康复,再见。

听说你又一次放弃了Dagger2,来听老夫说道说道》有74个想法

  1. I just want to say I am newbie to blogging and truly savored your web site. Likely I’m going to bookmark your site . You really come with excellent article content. Appreciate it for sharing with us your website page.

  2. I must show my thanks to you just for rescuing me from such a setting. Just after surfing around throughout the search engines and finding techniques that were not powerful, I assumed my life was over. Living minus the approaches to the issues you have resolved all through the guide is a crucial case, and ones which may have in a wrong way affected my career if I hadn’t noticed your web blog. That knowledge and kindness in dealing with all the pieces was valuable. I’m not sure what I would’ve done if I had not discovered such a point like this. I can also now look ahead to my future. Thanks for your time so much for this skilled and result oriented help. I will not be reluctant to endorse the sites to anybody who desires assistance on this subject.

  3. I must get across my love for your kind-heartedness for men and women who should have guidance on your situation. Your personal dedication to getting the solution all-around had become remarkably powerful and have frequently empowered guys like me to arrive at their ambitions. Your own helpful facts signifies a whole lot a person like me and especially to my peers. Best wishes; from all of us.

  4. I wanted to create you this very small observation to thank you yet again for your personal unique strategies you have featured at this time. It has been quite extremely open-handed with people like you to give freely what most of us might have advertised for an e-book to get some profit on their own, precisely considering the fact that you might well have tried it if you ever decided. These techniques in addition acted to become good way to be sure that other people have the same passion just as my personal own to understand lots more in terms of this problem. I’m certain there are some more fun sessions up front for many who browse through your website.

  5. My husband and i ended up being very happy that John could do his web research with the ideas he received from your own weblog. It is now and again perplexing to simply be making a gift of thoughts which often most people have been selling. Therefore we fully understand we’ve got the writer to give thanks to for that. Those illustrations you’ve made, the straightforward site menu, the relationships your site assist to foster – it’s got mostly astounding, and it’s really making our son and the family imagine that this concept is interesting, which is tremendously pressing. Thanks for all the pieces!

  6. I wanted to construct a note so as to thank you for all of the great advice you are sharing on this website. My time-consuming internet look up has at the end of the day been paid with incredibly good strategies to write about with my colleagues. I ‘d point out that we site visitors are unequivocally fortunate to be in a notable place with very many lovely individuals with valuable advice. I feel somewhat lucky to have discovered your entire web pages and look forward to many more excellent times reading here. Thanks again for a lot of things.

  7. I just wanted to jot down a brief note in order to appreciate you for some of the pleasant suggestions you are posting at this site. My prolonged internet search has at the end been rewarded with useful facts to exchange with my best friends. I ‘d claim that most of us readers are undeniably blessed to exist in a very good site with very many awesome individuals with interesting advice. I feel somewhat fortunate to have used your entire web page and look forward to so many more excellent moments reading here. Thanks again for all the details.

  8. I wanted to post you a very small word so as to give thanks over again regarding the wonderful techniques you’ve documented in this article. It was certainly shockingly generous with people like you to deliver without restraint what many individuals would have advertised as an e-book to make some cash on their own, especially seeing that you might have tried it in case you considered necessary. The creative ideas likewise worked as the fantastic way to realize that most people have the identical interest just as my personal own to know somewhat more with respect to this problem. I am sure there are millions of more pleasant times ahead for individuals that view your blog post.

  9. A lot of thanks for each of your labor on this site. Ellie take interest in getting into investigations and it’s really easy to understand why. I notice all concerning the compelling means you create useful tricks by means of the web site and even boost contribution from visitors on that subject plus my princess is actually learning a great deal. Enjoy the remaining portion of the new year. You are performing a fabulous job.

  10. Does your blog have a contact page? I’m having a tough time locating it but, I’d like to shoot you an e-mail. I’ve got some ideas for your blog you might be interested in hearing. Either way, great website and I look forward to seeing it expand over time.

  11. I simply wished to thank you so much again. I’m not certain the things I might have made to happen in the absence of the actual secrets contributed by you on this industry. It truly was the scary scenario for me personally, nevertheless taking a look at a new well-written way you processed that took me to weep with happiness. I’m just thankful for the support and in addition have high hopes you find out what an amazing job you happen to be carrying out educating most people with the aid of a site. Most likely you have never encountered any of us.

  12. Be sure that you have Silhouette Studio Designer Edition running and adhere to these easy actions to start! While it is crucial to have Silhouette Studio Designer Edition to import SVGs there is yet another step you’ve got to take to receive them actually cut. I am aware I’ve missed lots of nice anime collection, so should you have any suggestion, don’t hesitate to share about it in the comment section!

  13. I simply have to advise you that I am new to wordpress blogging and incredibly enjoyed your site. More than likely I am inclined to save your blog post . You literally have fantastic article material. Acknowledge it for swapping with us all of your url document

  14. I really intend to inform you that I am new to writing a blog and clearly adored your review. Very possible I am likely to save your blog post . You undoubtedly have stunning article information. Get Pleasure From it for discussing with us your main blog report

  15. Gday here, just got receptive to your blog through Yahoo and bing, and found that it’s quite entertaining. I’ll appreciate should you decide retain this approach.

  16. Thank you so much for giving everyone an extremely brilliant chance to discover important secrets from this website. It really is so nice plus stuffed with fun for me and my office colleagues to search the blog really thrice weekly to study the newest guides you have got. Of course, I’m just at all times happy with your very good inspiring ideas served by you. Certain 4 facts in this post are truly the finest we have all ever had.

  17. I enjoy you because of each of your efforts on this web page. Ellie really likes carrying out investigations and it is simple to grasp why. Most of us hear all relating to the compelling form you produce rewarding thoughts through this website and as well boost contribution from visitors on that topic while our simple princess is truly starting to learn a whole lot. Enjoy the rest of the year. You have been performing a useful job.

  18. Thank you for all your valuable work on this site. Kate really loves engaging in research and it’s easy to see why. All of us know all regarding the powerful medium you make worthwhile information through the web site and in addition encourage contribution from other ones on this topic while our favorite simple princess is really starting to learn a whole lot. Enjoy the rest of the new year. You are performing a terrific job.

  19. Our company have been actually reconsidering 2 from these for approximately 2 full weeks now, They are actually really pleasant! I hope they keep their condition for numerous years as well as don’t sag where the person is located.

  20. I am also writing to let you understand what a terrific encounter my cousin’s princess gained reading your site. She came to understand lots of details, most notably how it is like to have an ideal giving heart to let other folks really easily learn a number of extremely tough subject matter. You really surpassed readers’ expectations. Thank you for providing the precious, trusted, revealing and also easy guidance on the topic to Tanya.

  21. Unquestionably believe that which you stated. Your favorite reason seemed to be on the web the easiest thing to be aware of. I say to you, I definitely get irked while people consider worries that they just don’t know about. You managed to hit the nail upon the top and defined out the whole thing without having side-effects , people could take a signal. Will probably be back to get more. Thanks

  22. you are truly a good webmaster. The site loading velocity is incredible. It sort of feels that you are doing any unique trick. Furthermore, The contents are masterpiece. you’ve done a magnificent task in this topic!

  23. I have been surfing on-line greater than three hours lately, yet I by no means discovered any interesting article like yours. It¡¦s lovely value enough for me. Personally, if all webmasters and bloggers made excellent content as you did, the web will likely be a lot more helpful than ever before.

  24. Undeniably believe that which you said. Your favorite reason seemed to be on the web the easiest thing to be aware of. I say to you, I definitely get annoyed while people consider worries that they just do not know about. You managed to hit the nail upon the top and defined out the whole thing without having side effect , people could take a signal. Will probably be back to get more. Thanks

  25. Good blog! I really love how it is easy on my eyes and the data are well written. I’m wondering how I might be notified when a new post has been made. I have subscribed to your RSS which must do the trick! Have a nice day!

  26. I must voice my passion for your kindness supporting men and women who have the need for guidance on the field. Your very own dedication to getting the solution up and down has been certainly invaluable and has frequently encouraged people like me to reach their goals. This warm and friendly information entails so much a person like me and still more to my colleagues. Best wishes; from each one of us.

  27. I must express some thanks to the writer for rescuing me from this type of crisis. After looking out throughout the the net and obtaining ideas which are not helpful, I believed my life was done. Being alive devoid of the answers to the problems you’ve resolved as a result of your main short article is a crucial case, and the kind that might have in a negative way damaged my career if I hadn’t discovered your blog. Your personal expertise and kindness in maneuvering all areas was very useful. I’m not sure what I would’ve done if I had not come upon such a thing like this. It’s possible to now look forward to my future. Thanks very much for this skilled and result oriented help. I won’t be reluctant to suggest your blog post to anybody who will need direction about this matter.

  28. I simply wanted to post a note to thank you for all of the lovely suggestions you are giving here. My particularly long internet investigation has at the end been honored with reasonable tips to exchange with my visitors. I would claim that most of us readers are quite lucky to exist in a fabulous network with very many outstanding professionals with helpful advice. I feel somewhat privileged to have seen your entire weblog and look forward to so many more excellent times reading here. Thank you again for everything.

  29. I’m just writing to make you know what a great discovery my friend’s princess developed visiting your blog. She noticed plenty of issues, with the inclusion of what it is like to possess a great helping character to have men and women without hassle grasp several multifaceted topics. You really exceeded people’s desires. I appreciate you for offering these helpful, trustworthy, revealing as well as cool tips about that topic to Evelyn.

  30. Thanks a lot for providing individuals with such a memorable possiblity to read articles and blog posts from this website. It is always very superb and stuffed with amusement for me personally and my office friends to search your site at least 3 times every week to find out the new tips you have got. Not to mention, I am certainly satisfied for the astounding advice served by you. Selected two points in this posting are certainly the finest I’ve ever had.

  31. We’re a group of volunteers and starting a new scheme in our community. Your website provided us with valuable information to work on. You have done an impressive job and our whole community will be grateful to you.

  32. I have been browsing online more than three hours lately, yet I by no means found any attention-grabbing article like yours. It¡¦s pretty worth enough for me. In my view, if all webmasters and bloggers made excellent content material as you probably did, the internet will probably be much more useful than ever before.

  33. I simply have to notify you that I am new to online blogging and absolutely enjoyed your website. Likely I am prone to remember your blog post . You indeed have extraordinary article materials. Admire it for share-out with us your favorite internet site article

  34. I really have to notify you that I am new to putting up a blog and pretty much loved your webpage. Most likely I am likely to remember your blog post . You indeed have amazing article content. Acknowledge it for sharing with us your very own blog write-up

  35. Hello here, just got alert to your wordpress bog through Search engines like google, and found that it’s very informational. I’ll truly appreciate if you keep up this informative article.

  36. I simply needed to appreciate you once again. I am not sure what I would’ve taken care of in the absence of those pointers provided by you relating to such question. Completely was the daunting dilemma in my circumstances, nevertheless seeing a specialised technique you dealt with it forced me to weep over contentment. I am grateful for this advice and thus pray you realize what an amazing job that you are undertaking instructing the mediocre ones with the aid of your websites. Most likely you have never met all of us.

  37. I¡¦m no longer certain the place you’re getting your info, however good topic. I needs to spend a while studying much more or understanding more. Thank you for fantastic info I used to be looking for this information for my mission.

  38. I must express thanks to this writer just for rescuing me from this particular incident. After surfing through the world-wide-web and meeting methods which are not pleasant, I figured my entire life was gone. Being alive without the presence of strategies to the problems you’ve resolved through your entire site is a critical case, and the ones that would have negatively damaged my career if I hadn’t discovered your blog post. Your actual skills and kindness in dealing with all the details was tremendous. I am not sure what I would have done if I hadn’t come upon such a solution like this. I can at this time look forward to my future. Thanks a lot so much for this skilled and sensible help. I won’t think twice to endorse your blog post to any person who should get guidelines about this matter.

  39. I’m so happy to read this. This is the type of manual that needs to be given and not the random misinformation that is at the other blogs. Appreciate your sharing this greatest doc.

  40. Very interesting info !Perfect just what I was searching for! “Peace, commerce and honest friendship with all nations entangling alliances with none.” by Thomas Jefferson.

  41. I would like to express my love for your kind-heartedness giving support to those individuals that need help with this particular area. Your special dedication to getting the message around became exceptionally valuable and have consistently made men and women like me to attain their pursuits. Your warm and helpful suggestions means a great deal a person like me and much more to my office workers. Thanks a ton; from everyone of us.

  42. Thanks for sharing superb informations. Your site is very cool. I’m impressed by the details that you’ve on this web site. It reveals how nicely you perceive this subject. Bookmarked this web page, will come back for more articles. You, my pal, ROCK! I found just the info I already searched everywhere and simply couldn’t come across. What a perfect web-site.

  43. You really make it seem so easy with your presentation but I find this topic to be actually something which I think I would never understand. It seems too complicated and extremely broad for me. I am looking forward for your next post, I’ll try to get the hang of it!

发表评论

电子邮件地址不会被公开。