0

I have the following project structure:

app/
    di/
        components/
        modules   
    JakcApplication.kt
    MainActivity.kt    
core/
    data/
        local/
            database/
                models/
                    ProjectEntity.kt
                    PartEntity.kt
                    CounterEntity.kt
                AppDatabase.kt
        remote/
feature/
    addproject/
        data/
        di/
        page/
        screen/
        viewmodel/
    home/
        data/
        di/
        page/
        screen
        viewmodel/

As you can see, this is how I figure clean architecture should look and I'm guessing specifically package by feature (however, please correct me if I'm wrong).

Now, ideally, I'd like to keep the features self-contained. However, what I've noticed is that the typical way of using Room is you would have the following:

@Database(
    // ...
)
abstract class AppDatabase: RoomDatabase() {
    abstract val projectDao: ProjectDao

    companion object {
        // ...
        
        fun getDatabase(context: Context): AppDatabase {
            // ...
        }
    }
}

Now, I'm also wanting to use Dagger 2 as my dependency injection. I also know that typically, in order to use Room, it needs to know each dao at runtime. However, the way it would be typically set up would create a circular dependency:

Core would depend on each feature to get sight of the dao, and each feature would depend on Core in order to get sight of the app database.

Is there a way to define each dao within each feature:

feature/
    addproject/
        data/
            local/
                database/
                    dao/
                        ProjectDao.kt
                        PartDao.kt
                        CounterDao.kt

And then somehow inject the dao's into AppDatabase, or alternatively register them?

2
  • Additioinally, how do you want to handle when different features need different entities? In contrast to daos, entities are structurally relevant to the database and cannot be separated. So I don't think that separating just the daos (if it would be possible) would be of any help. Commented Apr 1 at 9:35
  • @tyg At present, each feature is relying on the three entities provided above. Each feature would map them into their relevant models as needed. Commented Apr 1 at 9:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.