1

I'm new to Play framework and all the Scala stack. I'm trying to implement dependency injection using macwire and ReactiveMongo to handle my MongoDB database.

I have this classes to define repositories.

trait BaseRepository extends ReactiveMongoComponents {

  /**
    * collection to retrieve documents from when accessing the database
    */
  val collectionName: String

  /**
    *
    * @return collection future instance
    */
  def collection: Future[BSONCollection] = reactiveMongoApi.database.map{
    _.collection(collectionName)
  }(dbContext)
}

UserRepository Trait:

trait UserRepository extends BaseRepository {

  def findUserByUsername(username: String): User
}

Repository Implementation:

class UserRepositoryImpl(val reactiveMongoApi: ReactiveMongoApi) extends UserRepository {

  override val collectionName = "user"

  override def findUserByUsername(username: String): User = User("user", "password")
}

I'm trying to wire the dependencies through 'wire' method like this:

trait RepositoryModule {

  import com.softwaremill.macwire._

  lazy val reactiveMongoApi = wire[ReactiveMongoApi]
  lazy val userDAO = wire[UserRepositoryImpl]
}

And I have an application loader defined like this:

class MyApplicationLoader extends ApplicationLoader {
  def load(context: Context): Application = new AppComponents(context).application
}

class AppComponents(context: Context) extends ReactiveMongoApiFromContext(context)
  with AppModule
  with AssetsComponents
  with I18nComponents
  with play.filters.HttpFiltersComponents {

  // set up logger
  LoggerConfigurator(context.environment.classLoader).foreach {
    _.configure(context.environment, context.initialConfiguration, Map.empty)
  }

  lazy val router: Router = {
    // add the prefix string in local scope for the Routes constructor
    val prefix: String = "/"
    wire[Routes]
  }
}

But I get the error:

Cannot find a public constructor nor a companion object for [play.modules.reactivemongo.ReactiveMongoApi

Notice that I have extended ReactiveMongoApiFromContext in my ApplicationLoader.

I am also extending ReactiveMongoComponents in my repository to be able to access the reactiveMongoApi which gives me access to the database. Is that correct?

How can I wire the ReactiveMongoApi in my implementation of the repository so I can access the database and collections?

Marcos
  • 701
  • 1
  • 8
  • 25
  • The Play-ReactiveMongo module is intended for Play injection. If not using Play DI, not sure it's of any use. – cchantep Jan 15 '18 at 21:37
  • In that case, I have to manually create the connection when using another dependency injection framework instead of Guice? – Marcos Jan 15 '18 at 21:48
  • To manage specific DI of `MongoConnection` (connection pool, not a single connection: http://reactivemongo.org/releases/0.12/documentation/tutorial/connect-database.html ) – cchantep Jan 15 '18 at 21:58
  • The documentation states: The traits ReactiveMongoComponents and ReactiveMongoApiComponents can be used for compile-time dependency injection. – Marcos Jan 15 '18 at 23:54
  • For Play DI, as it's clearly states up its very own lib name – cchantep Jan 16 '18 at 00:02

0 Answers0