title: Equivalent to Guice + RequestScope in Play Framework 2.4 (Scala)
body: <p>I am currently injecting some Service classes into Controller classes in Play Framework 2.4.  Each service class has a reference to one or more Repository classes that access the database.  It looks something like this:</p>
  
  <pre class="lang-scala prettyprint-override"><code>class AccountRepositoryImpl @Inject() (db: DB) extends AccountRepository {
      def find(id: Long): Future[Option[Account]] = // ...
  
      def insert(account: Account): Future[Long] = // ...
  }
  
  class AccountServiceImpl @Inject() (
      val accountRepository: AccountRepository) extends AccountService {
  
      // ...   
  }
  
  
  class AccountController @Inject() (
      val accountService: AccountService,
      val someOtherService: SomeOtherService) extends Controller {
  
      def signup: Action[JsValue] = Action.async(parse.json) { implicit request =&gt;
          val jsonSignup: JsResult[SignUp] = request.body.validate[SignUp]
  
          for {
             userid &lt;- accountService.insert(signUp.Value)
             somethingElse &lt;- someOtherService.doMoreSetup(userid)
             // ...
          } yield {
             // ...
          }
  }
  
  class ApiModule extends AbstractModule with ScalaModule {
  
      def configure(): Unit = {
          bind[DB].to[DBImpl]
          bind[AccountRepository].to[AccountRepositoryImpl]
          bind[AccountService].to[AccountServiceImpl]
          bind[SomeOtherService].to[SomeOtherServiceImpl]
      }
  } 
  </code></pre>
  
  <p>This works fine, but now I would like to add the ability to handle database transactions across Service classes.  To do that I was planning to write an ActionBuilder that can wrap the request in a transaction:</p>
  
  <pre class="lang-scala prettyprint-override"><code>class Transaction @Inject()(
      val db: DB) {
  
      object TransactionAction extends ActionBuilder[Request] {
          def invokeBlock[A](request: Request[A], block: (Request[A]) =&gt; Future[Result]): Future[Result] = {
              db.startTransaction()
              val result = block(request)
              db.commit() // todo: put rollback logic here
              result
          }
      }
  }
  </code></pre>
  
  <p>To do this, I need to share the same DB object among all the other injected objects, and I don't see a way to do that easily in Guice---
  it doesn't seem like there's a way to inject a single object in <code>RequestScope</code>.</p>
  
  <p>So my plan was to use Guice's <code>@Assisted</code> notation and create some factories, e.g.:</p>
  
  <pre class="lang-scala prettyprint-override"><code>class AccountRepositoryImpl @Inject() (@Assisted db: DB) extends AccountRepository {
      // ...
  }
  
  trait AccountRepositoryFactory {
     def create(db: DB): AccountRepository
  }
  
  class ApiModule extends AbstractModule with ScalaModule {
  
     def configure(): Unit = {
         // ...
  
         install(new FactoryModuleBuilder()
             .implement(classOf[AccountRepository],classOf[AccountRepositoryImpl])
             .build(classOf[AccountRepositoryFactory])
  
         install(new FactoryModuleBuilder()
             .implement(classOf[AccountService],classOf[AccountServiceImpl])
             .build(classOf[AccountServiceFactory])
  
  
     )
  </code></pre>
  
  <p>... and then replace the injected Traits with Factories and inject the DB into the controller.</p>
  
  <p>I think this will allow me to share a single DB instance among injected objects, but it seems like a lot of boilerplate, and it's extra work to write tests.  Is there a smarter way of doing this in Play Framework?  </p>