【发布时间】:2015-12-30 01:45:40
【问题描述】:
我正在开发一个 Play! 2.2 应用程序在 Scala 中使用 Slick 2.0,我现在正在处理数据访问方面,尝试使用 Cake Pattern。 这看起来很有希望,但我真的觉得我需要编写一大堆类/特征/对象来实现一些非常简单的东西。所以我可以对此有所了解。
以User 概念举一个非常简单的例子,我的理解是我们应该有:
case class User(...) //model
class Users extends Table[User]... //Slick Table
object users extends TableQuery[Users] { //Slick Query
//custom queries
}
到目前为止,这是完全合理的。现在我们添加一个“Cake Patternable”UserRepository:
trait UserRepository {
val userRepo: UserRepository
class UserRepositoryImpl {
//Here I can do some stuff with slick
def findByName(name: String) = {
users.withFilter(_.name === name).list
}
}
}
然后我们有一个UserService:
trait UserService {
this: UserRepository =>
val userService: UserService
class UserServiceImpl { //
def findByName(name: String) = {
userRepo.findByName(name)
}
}
}
现在我们将所有这些混合在一个对象中:
object UserModule extends UserService with UserRepository {
val userRepo = new UserRepositoryImpl
val userService = new UserServiceImpl
}
UserRepository真的有用吗?我可以将findByName写为Users光滑对象中的自定义查询。假设我有另一组这样的
Customer类,我需要在其中使用一些UserService功能。
我应该这样做:
CustomerService {
this: UserService =>
...
}
或
CustomerService {
val userService = UserModule.userService
...
}
【问题讨论】:
-
9000 是从哪里来的?
-
@Blankman knowyourmeme.com/memes/its-over-9000
标签: scala slick cake-pattern