Depedency injection framework of Play and Scala?

627 Views Asked by At

I am from spring background, where we use Dependency Injections in our projects. Now i am on Play-Framework, where i am select Scala for development. With Scala i want to use dependency injection and i find out there are so many Dependency Injections frameworks are available for Scala. Spring also provide the support for Scala dependency injection framework. But i only need IOC container, so there is no need for using spring. In the Play-Framework documentation they use Google-Guice for Dependency injection framework. But i found out SCALDI also a nice Dependency Injection Framework for Scala.

I am still confusing which Dependency Injection frameworks good for Scala and Play-Framework. There are also compile time type safety framework are available. Please suggest me, go for which Dependency Injection Framework?

2

There are 2 best solutions below

2
On BEST ANSWER

I would definitely suggest scaldi, but I'm also its creator, so my opinion is most probably a little bit biased :D

But seriously, it's hard to give a suggestion bested on your the description. I think it depends on the project you are working on and the team you are working with. Also whether you are ready to give up some flexibility in favor of static typing (in this case cake pattern, or MacWire would be a good choice). Since you are coming from Spring background, I think the concepts introduced by scaldi would be familiar to you.

You also need to keep in mind, that the next version of Play (2.4.0) will support DI out-of-the-box. Google Guice will be the default implementation (since they need some library that supports both - Scala and Java), but they keep it pretty open, so it's pretty easy for others to provide an alternative. For quite some time now I'm working on scaldi support for the new Play DI mechanism, so ideally it would be available around the time of Play 2.4.0 release to provide first-class Scaldi - Play 2.4.0 integration.

But in general I would suggest you to experiment with several libraries and see which you like (feel more comfortable with) the most (I would suggest scaldi, MacWire and cake pattern).

Recently similar question was asked at scaldi mailing list. Maybe you will also find my answer there helpful:

https://groups.google.com/forum/#!topic/scaldi/TYU36h7kGqk

0
On

If you need something really simple and lightweight take a look at Macwire: https://github.com/adamw/macwire

Example usage:

class DatabaseAccess()
class SecurityFilter()
class UserFinder(databaseAccess: DatabaseAccess, securityFilter: SecurityFilter)
class UserStatusReader(userFinder: UserFinder)

trait UserModule {
    import com.softwaremill.macwire._

    lazy val theDatabaseAccess   = wire[DatabaseAccess]
    lazy val theSecurityFilter   = wire[SecurityFilter]
    lazy val theUserFinder       = wire[UserFinder]
    lazy val theUserStatusReader = wire[UserStatusReader]
} 

will generate

trait UserModule {
    lazy val theDatabaseAccess   = new DatabaseAccess()
    lazy val theSecurityFilter   = new SecurityFilter()
    lazy val theUserFinder       = new UserFinder(theDatabaseAccess, theSecurityFilter)
    lazy val theUserStatusReader = new UserStatusReader(theUserFinder)
}

For testing, just extend the base module and override any dependencies with mocks/stubs etc, e.g.:

trait UserModuleForTests extends UserModule {
    override lazy val theDatabaseAccess = mockDatabaseAccess
    override lazy val theSecurityFilter = mockSecurityFilter
}