如何在Mapper模型中编写可以混合CRUDify成员(需要访问用户模型)覆盖的特征?

时间:2014-09-14 12:46:52

标签: scala lift traits

我正在尝试消除使用Mapper和CRUDify的电梯应用程序模型中的一些冗余(参见下面的模型)。我反复覆盖对象定义中的一些类成员,如下所示:

override def showAllMenuLocParams: List[Loc.AnyLocParam] = List(If(User.loggedIn_? _, "Not logged in"))
override def createMenuLocParams: List[Loc.AnyLocParam] = List(If(User.loggedIn_? _, "Not logged in"))

我想制作一个能够解决这个问题的特性,以便我可以按照以下方式混合这个特性:

object Test1 extends Test1 with LongKeyedMetaMapper[Test1] with CRUDify[Long, Test1] with MyNewTrait {

其中MyNewTrait的定义如下:

trait MyNewTrait {
  override def showAllMenuLocParams: List[Loc.AnyLocParam] = List(If(User.loggedIn_? _, "Not logged in"))
  override def createMenuLocParams: List[Loc.AnyLocParam] = List(If(User.loggedIn_? _, "Not logged in"))  
}

我想这个特性必须能够访问用户模型。任何人都可以建议如何写出特质?如上所述,特征是不完整的。我无法弄清楚在trait MyNewTrait

之后要放什么

谢谢,

/* ********************************************************************************************* */
object Test1 extends Test1 with LongKeyedMetaMapper[Test1] with CRUDify[Long, Test1] {
  override def dbTableName = "test1"
  override def fieldOrder = List(c1, c2, c3)
  override def showAllMenuLocParams: List[Loc.AnyLocParam] = List(If(User.loggedIn_? _, "Not logged in"))
  override def createMenuLocParams: List[Loc.AnyLocParam] = List(If(User.loggedIn_? _, "Not logged in"))
}
class Test1 extends LongKeyedMapper[Test1] with IdPK {
  def getSingleton = Test1 
  object c1 extends MappedString(this, 50)
  object c2 extends MappedString(this, 50)
  object c3 extends MappedString(this, 50)
}
/* ********************************************************************************************* */
object Test1 extends Test1 with LongKeyedMetaMapper[Test1] with CRUDify[Long, Test1] {
  override def dbTableName = "test2"
  override def fieldOrder = List(c1, c2, c3)
  override def showAllMenuLocParams: List[Loc.AnyLocParam] = List(If(User.loggedIn_? _, "Not logged in"))
  override def createMenuLocParams: List[Loc.AnyLocParam] = List(If(User.loggedIn_? _, "Not logged in"))
}
class Test1 extends LongKeyedMapper[Test1] with IdPK {
  def getSingleton = Test1 
  object c1 extends MappedString(this, 50)
  object c2 extends MappedString(this, 50)
  object c3 extends MappedString(this, 50)
}

1 个答案:

答案 0 :(得分:3)

我没有方便检查代码的项目,但这应该有效:

trait MyNewTrait[KeyType, CrudType <: KeyedMapper[KeyType, CrudType]]
extends CRUDify[KeyType, CrudType] {
  self: CrudType with KeyedMetaMapper[KeyType, CrudType] =>

    override def showAllMenuLocParams: List[Loc.AnyLocParam] =
      List(If(User.loggedIn_? _, "Not logged in"))

    override def createMenuLocParams: List[Loc.AnyLocParam] =
      List(If(User.loggedIn_? _, "Not logged in"))  
}

之后你可以混合它而不是CRUDify特征。

由于您使用的是Long密钥类型,因此您也可以使用类似参数较少的类似方式扩展LongCRUDify