question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use Dagger `Qualifier` instead of `Named`

See original GitHub issue

Problem

Currently, we are using Named annotation to define dependencies that have same return types.

@Provides
@Named("full_date")
fun provideDateFormatterForFullDate(locale: Locale): DateTimeFormatter = DateTimeFormatter.ofPattern("d-MMM-yyyy", locale)

@Provides
@Named("date_for_user_input")
fun provideDateFormatterForUserInput(locale: Locale): DateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy", locale)

This is problematic because it is not type-safe, a developer can find the issue once they build the project.

Proposal 1

We can define Qualifiers for the different dependencies and use those.

@Qualifier
@Retention(AnnotationRetention.SOURCE)
annotation class FullDate

Then while providing & injecting the dependencies we can Qualifier instead of Named

@Provides
@FullDate
fun provideDateFormatterForFullDate(locale: Locale): DateTimeFormatter = DateTimeFormatter.ofPattern("d-MMM-yyyy", locale)

@field:[Inject FullDate]
lateinit var dateFormatter: DateTimeFormatter

This will allow us to be more type-safe when providing and injecting our dependencies. There is no additional overhead, Named itself is a Qualifier so they both behave the same way.

Problems

  • With this approach, we have to define Qualifier for every dependency. I feel like this will increase the amount of code we have to maintain.

Proposal 2

While proposal 1 is the preferred approach, I can understand that we have to define new annotations every time there is a new dependency with the same return type as an already defined dependency. So the easiest approach to make sure using @Named is safe is to define a variable inside Object with the name of the dependency to make sure it’s same across all usages.

object DepsNames {
  const val FULL_DATE = "full_date"
}

@Provides
@Named(FULL_DATE)
fun provideDateFormatterForFullDate(locale: Locale): DateTimeFormatter = DateTimeFormatter.ofPattern("d-MMM-yyyy", locale)

So, what does everyone think? Is it something we should look into? Any other ideas?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
imran0101commented, Aug 5, 2020

Qualifier Proposal +1

1reaction
msasikanthcommented, Aug 5, 2020

I kind of like the qualifier approach since we are removing the usage of primitives and adding information via a type itself. Maintaining a bunch of different annotations isn’t that much overhead over maintaining a list of constants, IMO. In addition the qualifiers are compile time only (correct me if I am wrong here) so it won’t have any impact on our build.

Yes, it will behave the same way Named behaves so it should not affect our builds anymore than Named.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dagger 2 for Android, Part III ー The @Qualifier and @Named ...
In this article, I will talk about how to use the @Qualifier and @Named annotation. This is part of my Dagger 2 blog...
Read more >
How to use dagger qualifier annotations to provide different ...
On your first entry , add @Named("authorized") and on second, add @Named("basic") . You can add additional implementations in a similar fashion.
Read more >
Qualifiers in Dagger-Android Development | by Dheeraj Andra
@Qualifier annotation is provided by javax inject package and is used to qualify the dependency. For example, a class can ask both, a...
Read more >
Dagger Core Semantics
Dagger is a fully static, compile-time dependency injection framework for both Java and Android. It is developed by the Java Core Libraries Team...
Read more >
Dagger 2 @Named Annotation & Qualifiers Tutorial - YouTube
Learn Dagger 2 Dependency Injection in Android. In this video - learn how to use @ Named annotation, what is the need of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found