title: Kotlin Room withTransaction in Repository or @Transaction in Dao
body: <p>I have a question regarding Room and it’s <code>withTransaction { }</code> code block in combination with Koin.</p>
  <p>I have a repository where I need to access a couple of DAOs at the same time. I wanted to work with a <code>withTransaction { }</code> so I wouldn’t clutter 1 DAO with references to other DAOs.
  I’m not sure which object to inject in the constructor of my repository. The <code>withTransaction{ }  </code>can only be accessed by getting the RoomDatabase. But having the RoomDatabase in my Repository means that I have access to all the DAOs connected to that RoomDatabase. I’m not sure what the best practice around this use-case would be.</p>
  <p>Should I use the <code>withTransaction { }</code> and risk that all DAOs are accessible be that Repository or should I have the DAOs in my Repository's constructor and hand them to the `ReviewDao' to handle every insert?</p>
  <p>An example would be something like this with <code>withTransaction { }</code></p>
  <pre class="lang-kotlin prettyprint-override"><code>class ReviewRepository(
      private val roomDatabase: RoomDatabase
  ) {
      private val reviewDao = roomDatabase.reviewDao()
      private val userDao = roomDatabase.userDao()
  
      suspend fun saveReview(reviewResponse: ReviewResponse) {
          roomDatabase.withTransaction {
              reviewDao.insert(reviewResponse.getAsEntity())
              userDao.insert(reviewResponse.user.getAsEntity())
          }
      }
  }
  </code></pre>
  <p>And an example without <code>withTransaction { }</code> would be this</p>
  <pre class="lang-kotlin prettyprint-override"><code>class ReviewRepository(
      private val reviewDao : ReviewDao,
      private val userDao : UserDao
  ) {
      suspend fun saveReview(reviewResponse: ReviewResponse) {
          reviewDao.insertWithUser(reviewResponse.getAsEntity(), reviewResponse.user.getAsEntity(), userDao)
      }
  }
  
  @Dao
  interface ReviewDao {
      @Transaction
      suspend fun insertWithUser(review: Review, user: User, userDao: UserDao) {
          insert(review)
          userDao.insert(user)
      }
  }
  </code></pre>