Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Framework PathBindable with Dependency Injection

I'm migrating a Scala Play application to 2.5 and am currently moving my components to dependency injection. There's one place left where I'm at a loss how to do it though. I have a PathBindable implicit conversion defined in the companion object:

object Task {
  implicit def pathBindable(implicit stringBinder: PathBindable[String]) =
    new PathBindable[Task] {
       ...
    }
}

The implementation of the PathBindable needs to look up the object from a repository, but I haven't found a way to dependency-inject the repository here. As a workaround I'm using the now deprecated Play object:

val tasks = Play.application(Play.current).injector.instanceOf[TasksRepository]

Any ideas how to solve this properly?

like image 400
Mirko Stocker Avatar asked Apr 05 '16 09:04

Mirko Stocker


People also ask

What is inject in play framework?

Dependency injection is a widely used design pattern that helps to separate your components' behaviour from dependency resolution. Components declare their dependencies, usually as constructor parameters, and a dependency injection framework helps you wire together those components so you don't have to do so manually.

What is dependency injection stack overflow?

Dependency injection is a pattern to allow your application to inject objects on the fly to classes that need them, without forcing those classes to be responsible for those objects. It allows your code to be more loosely coupled, and Entity Framework Core plugs in to this same system of services.

Why play framework is used?

Play Framework makes it easy to build web applications with Java & Scala. Play is based on a lightweight, stateless, web-friendly architecture. Built on Akka, Play provides predictable and minimal resource consumption (CPU, memory, threads) for highly-scalable applications.

What is dependency injection in Scala?

Dependency injection is a widely used design pattern that helps separate your components' behaviour from dependency resolution. Play supports both runtime dependency injection based on JSR 330 (described in this page) and compile time dependency injection in Scala.


1 Answers

According to Lightbend Engineer Greg Methvin, PathBindables should only depend on the state in the path. The reason is that the code runs on the IO thread and should therefore be fast and not block.

like image 82
Mirko Stocker Avatar answered Sep 19 '22 08:09

Mirko Stocker