我有多个模型类,它们共享相同的属性。出于这个原因,我创造了一个特征,例如:
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
所有这些模型都定义了伴随对象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))
}
现在我想将它们存储在不同的集合中,但我想只有一个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)
}
然后,我有以下存储库:
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)
}
我的模块如下所示:
class PlayerModule extends AbstractModule with ScalaModule {
def configure() {
bind[PlayerDAO[FootballPlayer]].to[MongoPlayerDAO[FootballPlayer]]
bind[PlayerDAO[VideogamePlayer]].to[MongoPlayerDAO[VideogamePlayer]]
// ...
()
}
}
在我的Play控制器中,我注入了以下内容:
import models.FootballPlayer._
import models.VideogamePlayer._
class PlayerController @Inject()(
val messagesApi: MessagesApi,
footballPlayerService: PlayerService[FootballPlayer],
videogamePlayerService: PlayerService[VideogamePlayer]
) extends Controller with I18nSupport
然而,不幸的是,我得到以下例外:
1)没有实施 play.api.libs.json.OWrites被绑定了。 2)没有 play.api.libs.json.OWrites的实现 受到约束。 3)没有实施 play.api.libs.json.Reads被绑定了。 4)没有 play.api.libs.json.Reads的实现 被约束了。
我该如何解决这个问题?
答案 0 :(得分:1)
当进行dao注射时,可能无法找到隐含的上下文。尝试在您定义绑定的AbstractModule中包含implicits。
修改强>
在git上检查我的解决方案。 Here
我试图效仿你想做的事情和工作正常。
我不太确定您当前代码的问题是什么,因为我无法访问整个代码,但我认为它与搜索OWrites的应用程序和隐含Player
的读取有关。比FootballPlayer
或VideoGamePlayer