我在喷雾,akka,scala,reactivemongo项目中工作,我有这个特点
trait PersistenceManager {
val driver = new MongoDriver
val connection = driver.connection(List("localhost"))
def loan[A](collectionName: String)(f: BSONCollection => Future[A]): Future[A] = {
val db = connection("flujo_caja_db")
val collection = db.collection(collectionName)
f(collection)
}
}
我也有Dao的对象使用这样的特性:
object Dao extends PersistenceManager {
def find = loan("users"){
collection =>
collection.find(BsonDocument())....
}
}
在我的持久性管理器特征中实现这些数据库val是正确的吗? 它的效果非常好。
谢谢!
答案 0 :(得分:5)
我认为你想要这样定义的东西,以防止创建多个MongoDriver
和连接池:
object PersistenceManager{
val driver = new MongoDriver
val connection = driver.connection(List("localhost"))
}
trait PersistenceManager {
import PersistenceManager._
def loan[A](collectionName: String)(f: BSONCollection => Future[A]): Future[A] = {
val db = connection("flujo_caja_db")
val collection = db.collection(collectionName)
f(collection)
}
}