更新:更改了示例以反映我当前的情况
相当新的解除,我正在尝试为我的应用程序创建一个模型。由于我想保持DRY的精神,我想使用trait mixins来指定我模型中的一些字段。例如,我有一个Person
特征,我将其混合到我的Employee
类:
trait Person[T <: LongKeyedMapper[T]] extends LongKeyedMapper[T]{
self: T =>
object firstName extends MappedString[T](this, 50)
object lastName extends MappedString[T](this, 50)
object civicRegNumber extends MappedString[T](this, 12)
}
class Employee extends IdPK with OneToMany[Long, Employee] with Person[Employee] {
def getSingleton = Employee
object contactInfos extends MappedOneToMany(EmployeeContactInfo, EmployeeContactInfo.person)
}
object Employee extends Employee with LongKeyedMetaMapper[Employee]
可以看出,我在Employee中有一个contactInfos多对一映射。它看起来像:
trait PersonContactInfo[T <: LongKeyedMapper[T],P <: Person[P]] extends LongKeyedMapper[T] {
self: T =>
object email extends MappedEmail[T](this, 80)
def personMeta:P with LongKeyedMetaMapper[P]
object person extends LongMappedMapper[T,P](this, personMeta)
}
class EmployeeContactInfo extends IdPK with PersonContactInfo[EmployeeContactInfo, Employee] {
def getSingleton = EmployeeContactInfo
val personMeta = Employee
}
object EmployeeContactInfo extends EmployeeContactInfo with LongKeyedMetaMapper[EmployeeContactInfo]
这似乎有效,但我想将contactInfos对象移动到我的Person
特征中。但是,我无法弄清楚如何实现这一点......是否可以从特征继承OneToMany映射?欢迎任何帮助!
答案 0 :(得分:1)
经过几次尝试后,我通过将执行Person的OneToMany映射的特征分离到PersonContactInfo来实现此功能。这就是它现在的样子
trait Person[T <: Person[T]] extends LongKeyedMapper[T]{
self: T =>
object firstName extends MappedString[T](this, 50)
object lastName extends MappedString[T](this, 50)
object civicRegNumber extends MappedString[T](this, 12)
}
trait PersonToPersonContacts[P <: Person[P], PCI <: PersonContactInfo[PCI, P]] extends OneToMany[Long,P] {
self: P =>
def contactInfoMeta:LongKeyedMetaMapper[PCI] with PCI
object contactInfos extends MappedOneToMany(contactInfoMeta, contactInfoMeta.person)
}
class Employee extends IdPK with Person[Employee] with PersonToPersonContacts[Employee, EmployeeContactInfo] {
def getSingleton = Employee
override def contactInfoMeta = EmployeeContactInfo
}
object Employee extends Employee with LongKeyedMetaMapper[Employee]
我的PersonContactInfo现在看起来像:
trait PersonContactInfo[T <: LongKeyedMapper[T],P <: Person[P]] extends LongKeyedMapper[T] {
self: T =>
object email extends MappedEmail[T](this, 80)
def personMeta:P with LongKeyedMetaMapper[P]
object person extends LongMappedMapper[T,P](this, personMeta)
}
class EmployeeContactInfo extends IdPK with PersonContactInfo[EmployeeContactInfo, Employee] {
def getSingleton = EmployeeContactInfo
val personMeta = Employee
}
object EmployeeContactInfo extends EmployeeContactInfo with LongKeyedMetaMapper[EmployeeContactInfo]
仍然不确定这是否是解决这个问题的方法,但是ity完成了这项工作:)