Joh*_*Doe 10 scala guice playframework
我有多个模型类,它们共享相同的属性.出于这个原因,我创造了一个特征,例如:
trait Player extends Temp {
val gameId: BSONObjectID
val personalDetails: abc.PersonalDetails // <- comes from shared library
}
case class FootballPlayer(var _id: Option[BSONObjectID] = None,
gameId: BSONObjectID,
personalDetails: abc.PersonalDetails,
var created: Option[DateTime] = None,
var updated: Option[DateTime] = None
) extends Player
case class VideogamePlayer(var _id: Option[BSONObjectID] = None,
gameId: BSONObjectID,
personalDetails: abc.PersonalDetails,
var created: Option[DateTime] = None,
var updated: Option[DateTime] = None
) extends Player
Run Code Online (Sandbox Code Playgroud)
所有这些型号的,有作为一个伴侣的对象play.api.libs.json.Reads和play.api.libs.json.OWrites定义.
例如:
object FootballPlayer {
import play.api.libs.functional.syntax._
import play.api.libs.json.Reads._
import play.api.libs.json._
import reactivemongo.play.json.BSONFormats.BSONObjectIDFormat
implicit val footballPlayerReads: Reads[FootballPlayer] = (
(__ \ "_id").readNullable[BSONObjectID].map(_.getOrElse(BSONObjectID.generate)).map(Some(_)) and
(__ \ "gameId").read[BSONObjectID] and
(__ \ "personalDetails").read[abc.PersonalDetails] and
(__ \ "created").readNullable[DateTime].map(_.getOrElse(new DateTime())).map(Some(_)) and
(__ \ "updated").readNullable[DateTime].map(_.getOrElse(new DateTime())).map(Some(_))
) (FootballPlayer.apply _)
implicit val sharedPersonalDetailsWrites: Writes[abc.PersonalDetails] = abc.PersonalDetails.sharedPersonalDetailsWrites
implicit val footballPlayerWrites: OWrites[FootballPlayer] = (
(__ \ "_id").writeNullable[BSONObjectID] and
(__ \ "gameId").write[BSONObjectID] and
(__ \ "personalDetails").write[abc.PersonalDetails] and
(__ \ "created").writeNullable[DateTime] and
(__ \ "updated").writeNullable[DateTime]
) (unlift(FootballPlayer.unapply))
}
Run Code Online (Sandbox Code Playgroud)
现在我想将它们存储在不同的集合中,但我想只有一个DAO,所以我实现了以下内容:
trait PlayerDAO[T <: Player] {
def findById(_id: BSONObjectID)(implicit reads: Reads[T]): Future[Option[T]]
def insert(t: T)(implicit writes: OWrites[T]): Future[T]
}
class MongoPlayerDAO[T <: Player] @Inject()(
playerRepository: PlayerRepository[T]
) extends PlayerDAO[T] {
def findById(_id: BSONObjectID)(implicit reads: Reads[T]): Future[Option[T]] = playerRepository.findById(_id)
def insert(t: T)(implicit writes: OWrites[T]): Future[T] = playerRepository.insert(t).map(_ => t)
}
Run Code Online (Sandbox Code Playgroud)
然后,我有以下存储库:
class PlayerService[T <: Player] @Inject()(playerDAO: PlayerDAO[T])(implicit reads: Reads[T], writes: OWrites[T]) {
def findById(_id: BSONObjectID): Future[Option[T]] = playerDAO.findById(_id)
def save(t: T): Future[T] = playerDAO.save(t)
}
Run Code Online (Sandbox Code Playgroud)
我的模块如下所示:
class PlayerModule extends AbstractModule with ScalaModule {
def configure() {
bind[PlayerDAO[FootballPlayer]].to[MongoPlayerDAO[FootballPlayer]]
bind[PlayerDAO[VideogamePlayer]].to[MongoPlayerDAO[VideogamePlayer]]
// ...
()
}
}
Run Code Online (Sandbox Code Playgroud)
在我的Play控制器中,我注入以下内容:
import models.FootballPlayer._
import models.VideogamePlayer._
class PlayerController @Inject()(
val messagesApi: MessagesApi,
footballPlayerService: PlayerService[FootballPlayer],
videogamePlayerService: PlayerService[VideogamePlayer]
) extends Controller with I18nSupport
Run Code Online (Sandbox Code Playgroud)
但是,不幸的是,我得到以下异常:
1)没有绑定play.api.libs.json.OWrites的实现.2)没有绑定play.api.libs.json.OWrites的实现.3)没有绑定play.api.libs.json.Reads的实现.4)没有绑定play.api.libs.json.Reads的实现.
我怎样才能解决这个问题?