Dagger 2: When to use constructor injections and when to use field injections?

℡╲_俬逩灬. 提交于 2019-12-03 01:24:07

Use constructor injection. if you can't, use property injection.

Rule 1 seems ok, like decorations or attributes you can use Property(field) injection.

Rule 2 seems ok, because who uses your class they have to follow your constructor. They may not know they have to intilaize your property also.

Rule 3 It's not just good for unit test. It's good for applying Single Responsibilty. It's easier to see your object graph.Otherwise you will hide it with property.

If we come in your question, yes there a lot of parameters in your constructor. But the solution is not property injection. You can refactor your code and use aggregate services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!