EzDevInfo.com

macwire

Lightweight and Nonintrusive Scala Dependency Injection Library SoftwareMill - Java, Scala, Cloud we develop custom software solutions: web applications, back-end systems and enterprise solutions. in short, we specialize in: java, scala and cloud.

Inject database connection pool

I have started using MacWire for the dependency injection of my Play app, and I am having problems trying to inject the database connection.

Before using DI, my code looked like this:

DB.withConnection { implicit connection =>
  ...
}

This is not working anymore after using DI. I get the following exception: java.lang.InstantiationException: play.api.db.DBApi.

My application loader:

class Loader extends ApplicationLoader {
  def load(context: Context) = {
    val components = new BuiltInComponentsFromContext(context) with Components
    components.application
  }
}

The main components of the app:

trait Components extends BuiltInComponents with I18nComponents
                 with RepositoryModule {

  lazy val assets: Assets = wire[Assets]
  lazy val router: Router = wire[Routes] withPrefix "/"
}

And the repository module:

trait RepositoryModule {
  lazy val userRepository = wire[UserRepository]
}

How can I get and use a database connection pool and inject it so it can be used within the repository?


Source: (StackOverflow)

Akka supervision and DI integration

I have seen the guice akka integration as well as the macwire akka integration TypeSafe tutorials

However, something that i could not see is actor that requires supervision. I wonder if DI framework, makes sense in the context of child actor. How would create a child actor from within an actor using let say a module from macwire.

I could not find such example. Any help ?


Source: (StackOverflow)

Advertisements

How to use macwire with traits?

I recently found the macwire, which can help us to inject dependencies our scala classes. I want to apply it to my existing scala code, but found some problems with traits.

My existing code uses trait a lot (like thin-cake patten), here are some example code:

trait InvokeLater {
    def invokeLater(f: => Any) = ...
}

trait DialogSupport with InvokeLater { this: JDialog =>
    def showOnCenter() = ...
}

class MyDialog extends JDialog with DialogSupport

But now, if I want to use macwire, everything should be classes (except the trait which assembles dependencies).

I still want the DialogSupport to be a trait, since I want my dialogs have the showOnCenter methods which extend from it.

But InvokeLater will be a class(with macwire), the DialogSupport can't have the injected one of it easily if it's a trait.

How to fix it?


Source: (StackOverflow)

Play 2.4 with static dependency injection without global lookup

I tried to play a bit with Play 2.4 and tried to use compile time dependency injection with MacWire.

I found a neat tutorial here which looks promising, however I only consider it a partial solution because of this line:

protected val dbConfig = DatabaseConfigProvider.get[JdbcProfile](Play.current)

That is actually a lookup of a dependency and is exactly the thing I want to turn away with dependency injection!

However is this a possible thing to do (with compile time dependency injection) ?

My current assumption is: Play.current is the fully configured Play Application which is constructed at runtime, thus it is not injectable with a compile time framework like MacWire. Is this correct?

Maybe what I am trying to do here is what the documentation refers to when it states:

Play 2 was designed with an assumption of global state. Play 3 will hopefully remove this global state, however that is a major breaking task. In the meantime, Play will be a bit of a hybrid state, with some parts not using global state, and other parts using global state.

If there is a way to remove the static dependency lookup, I would love to hear about it. However if it is not possible I would love some confirmation about that as well, so I can move on (for now).

Thanks in advance for any hints/insights/...!


Source: (StackOverflow)