Implementing Dependency Injection in Room
As with other recipes, Dependency Injection is vital, and in this recipe, we will walk through how we can inject our DatabaseModule
and provide the Room database where it is needed.
Getting ready
You will need to have prior knowledge of how Hilt works to be able to follow this recipe step by step.
How to do it…
Open the RoomExample
project and add Hilt, which is what we will use for Dependency Injection. In Chapter 3, Handling the UI State in Jetpack Compose and Using Hilt, we covered Hilt, so we will not discuss it here but just show you how you can use it with Room:
- Open your project and add the necessary Hilt dependency. See Chapter 3, Handling the UI State in Jetpack Compose and Using Hilt, if you need help setting up Hilt or visit https://dagger.dev/hilt/.
- Next, let’s go ahead and add our
@HiltAndroidApp
class, and in theManifest
folder, add the name of ourHiltAndroidApp
, in our case,UserInformation...