Sagar Suri
03/19/2020, 2:54 AM@Singleton
and @Inject
inside the code instead of Module
?
I have a personal opinion of creating Modules
and defining all my dependencies there instead of marking classes with the above annotations?
Reason being:
1. It help me understand the dependencies and configurations from a single file i.e Module
.
2. It avoid mixing code with configuration(annotations).
What are your opinion on this?
I don’t prefer the following code:
@Module
class BookingDatabaseModule {
@Provides
fun provideBookingDao(database: BookingDatabase) = database.bookingDao()
}
Following is my preferred way of doing it:
@Module
class BookingDatabaseModule {
@Provides
@Singleton
fun provideDatabase(context: Context) = BookingDatabase.newInstance(context)
@Provides
fun provideBookingDao(database: BookingDatabase) = database.bookingDao()
}
What are your opinions?Ianmedeiros
03/20/2020, 12:10 PMIanmedeiros
03/20/2020, 12:11 PMgildor
03/20/2020, 1:37 PMgildor
03/20/2020, 1:38 PMSagar Suri
03/20/2020, 1:38 PMgildor
03/20/2020, 1:40 PMSagar Suri
03/20/2020, 1:40 PMgildor
03/20/2020, 1:41 PMgildor
03/20/2020, 1:43 PMSagar Suri
03/20/2020, 1:45 PMgildor
03/20/2020, 1:45 PMgildor
03/20/2020, 1:46 PMgildor
03/20/2020, 1:47 PMSagar Suri
03/20/2020, 1:48 PMColton Idle
03/23/2020, 6:08 AMSagar Suri
03/23/2020, 9:27 AMgildor
03/25/2020, 3:14 PMColton Idle
03/25/2020, 3:52 PMWhy do you want to have all modules in the same package, if those modules are not related?For me, it's because when I start a new project, I think it would be really nice to have one spot to look at and find out what dependencies I can start injecting into whatever new feature I'm working on. A lot of times when I'm new to an existing codebase and it uses Dagger... I'm left thinking "What the hell is available for me to use?" So I'm not advocating to have all modules in the same package, I'm just saying that I wish there was an easy way to to just see all modules or all provides methods, or just really... What is everything that I can start injecting? That's what I find hard as a 2 week user of Dagger. People mention scabbard, but it seems crazy that if Dagger is used to help provide dependencies in a consistent way, that there would be a way to quickly view what dependencies are injectable if I want them.
Why do you want to know which dependency is available to you, if essentially every class is available? When you inject everything no need to know what is available, also dagger will check it for you on compile timeLet me give you another example. I started working on a project with a barebones Dagger setup. It was basically just used for sharing OkHttp/Retrofit instance. They plan on using dagger more. Okay cool. Makes sense. I code up a new feature in Android with SharedPreferences and then in code review they tell me "Oh this is actually available through Dagger" and it's like. 🤦 I wish there was an easy way to see what dependencies have been set up in dagger for me to use. On the opposite side of the spectrum I've worked on projects with like 1000 dependencies and it's literally impossible to know what I have available to me when building my feature until I kind of start guessing. I guess maybe this could just be solved by docs in the project keeping a master list of everything that's available. Maybe this isn't a problem at all! Maybe I'm just making it up because I'm new to dagger. /shruggie This is a beginner perspective. Would love for someone with more experience to break me out of that thinking.
Why inject is hiding dependency? You hide or expose dependency by making them public/internal, not by defining provides method for themSimilar to what I said above. You are making sense, but it still doesn't help me in the use case where I'm new to a project, and I jump in and start writing a feature "Hmm I'm going to need Retrofit and SharedPreferences (purely hypothetical example) I search for all classes that end in Module, and only find one (in this hypothetical example) and the only thing it provides is Retrofit. So now I'm like, Okay, I will Inject retorfit as a field, but now I need sahredPreferences. Then at code review time, they tell me "Oh actually we have this SharedPrefWrapper class that has an @Inject on it's constructor, so you can use that". I'm complaining about visibility and not knowing what's there quickly.