Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DI - Adding basic manual injection #32

Merged
merged 2 commits into from
Jan 23, 2024

Conversation

hamorillo
Copy link
Contributor

@hamorillo hamorillo commented Jan 23, 2024

#14

I did some research about DI in libraries. What I've seen is that, in general, libraries don't usually use another library for the DI. I would say there are two main reasons for that:

  • Avoid adding another external dependency in the library that you'll need to release within your artifact.
  • Prevent the library clients from dependency conflicts.

My first idea was to search for libraries using HILT. The short answer is: You can't use HILT in a library (If you don't fully control the app that is going to use the library at it also uses HILT, so it's not a valid solution for a public library).

Dagger is definitely a viable option. There is a limited amount of info on the internet, and I couldn't find several projects using it.

I didn't investigate Koin, but I'm pretty sure we could use it if we wanted.

Taking the previous info into account plus the expected side/complexity of our library, I think the best option for us is to use basic manual injection. Basically, to be able to implement good quality tests without. You can find a basic example here.

This PR follows the Google example and creates a test to verify the idea behind the manual injection.

Please, feel free to leave your comments. I'm opening the discussion about DI with this PR.

Comment on lines +53 to +61
testImplementation("io.mockk:mockk-android:1.13.9")
testImplementation("io.mockk:mockk-agent:1.13.9")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Mockk for mocking objects. It's more modern and designed for Kotlin code.

More info:

Mockito vs Mockk
Mockk GuideBook

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL about Mockk, thanks for the links

@hamorillo hamorillo requested review from maxme and mlumeau and removed request for maxme and mlumeau January 23, 2024 10:39
Copy link
Member

@mlumeau mlumeau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good first implementation but as we quickly grow the codebase we'll need to split GravatarSDKContainer into sub-containers (network, image loading, oauth flow, db, ...)

@hamorillo hamorillo force-pushed the hamorillo/14-manual-depency-injector branch 2 times, most recently from 4a9c4ff to 109f366 Compare January 23, 2024 12:31
@hamorillo hamorillo force-pushed the hamorillo/14-manual-depency-injector branch from 109f366 to d886841 Compare January 23, 2024 12:41
Copy link
Contributor

@maxme maxme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

I didn't investigate Koin, but I'm pretty sure we could use it if we wanted.

Honestly I'd like to remove some boiler code (maybe by testing/using Koin), but I'd also like to avoid an extra dependency. So I'm fine using manual injection first, we can revisit this later if it becomes a pain to manage. Good thing, it won't impact the end users (developers) if we swap it with Koin.

@hamorillo hamorillo merged commit f030678 into trunk Jan 23, 2024
1 check failed
@hamorillo hamorillo deleted the hamorillo/14-manual-depency-injector branch January 23, 2024 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants