Scala dependency injection when using case class/companion object pattern
<p>In migrating to Play 2.5, I am adopting the dependency injection design patterns, including for (JDBC) database access.</p>

<p>At a class level, I understand the concept:</p>

<pre><code>class Users @Inject() (db: Database)
</code></pre>

<p>But I have not yet seen a discussion of how this might apply when you require database access within methods of a case class and companion object pattern. An example basic model being:</p>

<pre><code>package models

import anorm._
import anorm.SqlParser._
import javax.inject._

import play.api.db._
import play.api.libs.functional.syntax._
import play.api.libs.json._


case class User @Inject() (db: Database) (
    id: Option[Long] = None,
    email: String
) {
    def save = {
        id.map { id =&gt; User.findById(id) } match {
            case None =&gt; create
            case _ =&gt; update
        }
    }

    def create = db.withConnection { implicit conn =&gt;
        SQL(
            """INSERT INTO users (email) VALUES ({email})"""
        ).on(
            'email -&gt; email
        ).executeUpdate()

        this
    }

    def update = ...
}

object User {    
    val simple = {
        get[Option[Long]]("id") ~
        get[String]("email") map {
            case id ~ email =&gt;
                User(id, email)
        }
    }

    def findById(id: Long) = db.withConnection { implicit conn =&gt;
        SQL("""SELECT * FROM users WHERE id = {id}""").on('id -&gt; id).as(User.simple.singleOpt)
    }
}
</code></pre>

<p>This changes the signature of the case class (making it unusable within <code>val simple = { ... }</code>), and I can't figure out how to inject/access the db in the companion object. Trying <code>@Inject() var db: Database _</code> within the object results in a world of NullPointerExceptions that I'd like to avoid.</p>

<p>What is the recommended design pattern for this common use case in a world of dependency injection?</p>