score:0

From your second example, it's getDb() to be out of scope, as it's defined on the DbUtil class.

So if you want to access that method, you need to call it on the dbUtil instance:

class UserDao(dbUtil: DBUtil) {

    def getById(id: Int): User = {
        dbUtil.getDb().withSession { implicit session => ... }
    }   

}

score:0

The following code works fine for me.

class Database {
  def withSession() {}
}
object DatabaseAccess {
  var databases: Map[String, Database] = Map()
}

class DbUtil {
  private lazy val db = {
    DatabaseAccess.databases("db.default")
  }

  def getDb() = db
}

class UserDao(dbUtil: DbUtil) {

  def getById(id: Int) = {
    dbUtil.getDb().withSession()
  }
}

Maybe it's a problem with your IDE. Have you tested if the code compiles?


Related Query

More Query from same tag