Как добавить черту ReactiveMongoApi в репозиторий с помощью macwire?

Я новичок в Play Framework и всем стеке Scala. Я пытаюсь реализовать внедрение зависимостей, используя macwire и ReactiveMongo для обработки моей базы данных MongoDB.

У меня есть эти классы для определения хранилищ.

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 UserRepository extends BaseRepository {

  def findUserByUsername(username: String): User
}

Реализация репозитория:

class UserRepositoryImpl(val reactiveMongoApi: ReactiveMongoApi) extends UserRepository {

  override val collectionName = "user"

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

Я пытаюсь связать зависимости с помощью метода "wire" следующим образом:

trait RepositoryModule {

  import com.softwaremill.macwire._

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

И у меня есть загрузчик приложения, определенный следующим образом:

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]
  }
}

Но я получаю ошибку:

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

Обратите внимание, что я продлил ReactiveMongoApiFromContext в моем ApplicationLoader,

Я также расширяю ReactiveMongoComponents в моем хранилище, чтобы иметь возможность доступа к reactiveMongoApi который дает мне доступ к базе данных. Это верно?

Как я могу подключить ReactiveMongoApi в моей реализации репозитория, чтобы я мог получить доступ к базе данных и коллекциям?

0 ответов

Другие вопросы по тегам