What is the purpose of scaldi Module

164 Views Asked by At

It seems like i am able to inject Services or Akka Actors without the use of Module. May i know what is the purpose of Module then?

class Hello(implicit inj:Injector) extends Controller with AkkaInjectable {
  val greetingService = inject[GreetingService]
  implicit val system = inject [ActorSystem]
  val greetingActor = injectActorRef[greetingActor]

  def greet(person:Person) = Action {
    Ok(greetingService.greet(person.name))
  }
}

Even without the below it works just fine

class MainModule extends Module {
  binding to new GreetingService
  bind [ActorSystem] to ActorSystem("ScaldiAkkaExample") destroyWith (_.terminate())
  binding toProvider new StatisticsProvider
}
1

There are 1 best solutions below

0
On

Module basically instantiates the services and when you inject them to your controllers, all controllers share the same instance (which is what we want). Without declaring the bindings in module you will have a new instance of service in every controller (which is not what we want).

However in Akka its the opposite

Quote from scaldi

I would like to point out how Actor are bound. It is important, that you bind them with toProvider function. It will make sure that Scaldi always creates new instances of the Actor classes when you inject them with injectActorRef or injectActorProps