Lift:如何使这些关系具有多态性?

发布于 2024-10-18 14:59:00 字数 852 浏览 2 评论 0原文

我有一个条目 它应该与三个“列表”之一相关,称它们为 ListA ListB ListC 我不知道如何使用 LongMappedMapper 来做到这一点。 那么我该怎么做呢?

我想让列表有多个条目,而不必为每种列表指定 listX 关系。 所以

class Entry ...{
    object listA extends LongMappedMapper(this,ListA)
    object listB extends LongMappedMapper(this,ListB)
    ...
}

我想要这样的东西:

class Entry ...{
    object list extends PolyLongMappedMapper(this,ListA,ListB,ListC)
    //PolyLongMappedMapper i the example mapper what i want
    ...
}

或者:

class Entry ...{
    object list extends PolyLongMappedMapper(this,BaseListTrait)
    //where BaseListTrait is a Trait shared by all List classes
    //PolyLongMappedMapper i the example mapper what i want
    ...
}

Lift框架中的某些东西是我想要的吗?什么可以与 PolyLongMappedMapper 相媲美? 或者有一个优雅的方法来解决这个问题吗?

I got a Entry
it should be related to one of three "Lists" call them ListA ListB ListC
i couldnt figure out how to do this with LongMappedMapper.
So how could i do this?

I wanted to let the List have multiple entries without having to specify a listX relation for each kind of list.
So not:

class Entry ...{
    object listA extends LongMappedMapper(this,ListA)
    object listB extends LongMappedMapper(this,ListB)
    ...
}

I want something like:

class Entry ...{
    object list extends PolyLongMappedMapper(this,ListA,ListB,ListC)
    //PolyLongMappedMapper i the example mapper what i want
    ...
}

or:

class Entry ...{
    object list extends PolyLongMappedMapper(this,BaseListTrait)
    //where BaseListTrait is a Trait shared by all List classes
    //PolyLongMappedMapper i the example mapper what i want
    ...
}

Is somewere something in the Lift framework what does what i want? what is comparable to the PolyLongMappedMapper?
Or is there an elegant way to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み零 2024-10-25 14:59:00

您可以通过以下方式执行此操作:

class A extends LongKeyedMapper[A] with IdPK {
    object entry extends LongMappedMapper(this, Entry)
    ...

class B extends LongKeyedMapper[B] with IdPK {
    object entry extends LongMappedMapper(this, Entry)
    ...

class C extends LongKeyedMapper[C] with IdPK {
    object entry extends LongMappedMapper(this, Entry)
    ...

class Entry extends LongKeyedMapper[Entry] with IdPK {
    def aList = A.findAll(By(A.entry, this))
    def bList = B.findAll(By(B.entry, this))
    def cList = C.findAll(By(C.entry, this))
    ...

您的列表是:

e.aList, e.bList, e.cList

时间:

val e: Entry

Etam。

You can do this in such a way:

class A extends LongKeyedMapper[A] with IdPK {
    object entry extends LongMappedMapper(this, Entry)
    ...

class B extends LongKeyedMapper[B] with IdPK {
    object entry extends LongMappedMapper(this, Entry)
    ...

class C extends LongKeyedMapper[C] with IdPK {
    object entry extends LongMappedMapper(this, Entry)
    ...

class Entry extends LongKeyedMapper[Entry] with IdPK {
    def aList = A.findAll(By(A.entry, this))
    def bList = B.findAll(By(B.entry, this))
    def cList = C.findAll(By(C.entry, this))
    ...

And your lista are:

e.aList, e.bList, e.cList

when:

val e: Entry

Etam.

内心激荡 2024-10-25 14:59:00

它并不完美,但我创建了一个解决方法。
我找不到一种很好的方法来实现两个方向的多态性。

那么我们就从角色开始吧。角色在内部拥有角色玩家的权利,即。用户或进程或其他什么。
角色扮演者在应用程序中扮演角色。适用的是系统中的文件或文件夹或其他东西。
applialbe 有很多作用。角色扮演者有很多角色。由于不必为每个 appliable 定义关系,而只需使用 Appliable 特征进行扩展,我在 Role 类中创建了带有 appliableId 和 appliableClass 的 Workarout。

class Role extends BaseModel[Role] {
  def getSingleton = Role

  object appliableId extends MappedLong(this)

  object appliableClass extends MappedString(this, 300)

  def setAppliable (appliable: Appliable[_]) = {
    rolePlayerId(appliable.id.is)
    rolePlayerClass(validClassName(appliable))
  }

  def validClassName(appliable: Appliable[_]) = {
    appliable.getClass.getName
  }
  def appliable={
    Class.forName(appliableClass).asInstanceOf[Appliable[_]].findById(appliableId.is)
  }
   object rolePlayerId extends MappedLong(this)

  object rolePlayerClass extends MappedString(this, 300)

  def setRolePlayer (appliable: Appliable[_]) = {
    rolePlayerId(appliable.id.is)
    rolePlayerClass(validClassName(appliable))
  }


  def rolePlayer={
    Class.forName(rolePlayerClass).asInstanceOf[RolePlayer[_]].findById(rolePlayerId.is)
  }
}

object Role extends Role with BaseMetaModel[Role] {

}

这是适用的即。文件夹或博客条目。

trait Appliable[T <: BaseModel[T]] {
  this: T =>

  def findById(i: Long): Box[T]

  def id: MappedLong[T]

  def getRoles = {
    Role.findAll(By(Role.appliableId, id.is), By(Role.appliableClass, Role.validClassName(Appliable.this)))
  }

  def addRole(role: Role) = {
    role.setAppliable(Appliable.this)
  }
}

class App1 extends BaseModel[App1] with Appliable[App1] {
  def getSingleton = App1
}

object App1 extends App1 with BaseMetaModel[App1]

class App2 extends BaseModel[App2] with Appliable[App2] {
  def getSingleton = App2
}

object App2 extends App2 with BaseMetaModel[App2]

这里用 FunSuite 进行一个简短的测试:

test("appliables should have roles") {
    val a = App1.create
    val b = App2.create
    List(a, b).map(_.save)
    val ra1 = Role.create.setAppliable(a)
    val ra2 = Role.create.setAppliable(a)
    val rb1 = Role.create.setAppliable(b)
    val rb2 = Role.create.setAppliable(b)

    List(ra1, ra2, rb1, rb2).map(_.save)
    val ar = App1.find(By(a.id, a.id.is)).get.getRoles.toList
    assert(ar(0) == ra1, ar(0) + " was not " + ra1)
    assert(ar(1) == ra2, ar(1) + " was not " + ra2)
    val br = b.getRoles.toList
    assert(br(0) == rb1, br(0) + "  was not " + rb1)
    assert(br(1) == rb2, br(1) + " was not " + rb2)
  }

Its not perfect but i created a workaround.
I couldnt find a way to do the polymorphism in both directions in a nice way.

So lets start with the role. the Role holds internally the Rights of a RolePlayer ie. a User or a process or whatever.
The Roleplayer plays a Role in a Appliable. The appliable is a File or a folder or something else in the system.
the applialbe has many roles. a roleplayer has many roles. for not having to define for each appliable the relation and just having to extend with the trait Appliable i created the Workarout wit appliableId and appliableClass in the Role class.

class Role extends BaseModel[Role] {
  def getSingleton = Role

  object appliableId extends MappedLong(this)

  object appliableClass extends MappedString(this, 300)

  def setAppliable (appliable: Appliable[_]) = {
    rolePlayerId(appliable.id.is)
    rolePlayerClass(validClassName(appliable))
  }

  def validClassName(appliable: Appliable[_]) = {
    appliable.getClass.getName
  }
  def appliable={
    Class.forName(appliableClass).asInstanceOf[Appliable[_]].findById(appliableId.is)
  }
   object rolePlayerId extends MappedLong(this)

  object rolePlayerClass extends MappedString(this, 300)

  def setRolePlayer (appliable: Appliable[_]) = {
    rolePlayerId(appliable.id.is)
    rolePlayerClass(validClassName(appliable))
  }


  def rolePlayer={
    Class.forName(rolePlayerClass).asInstanceOf[RolePlayer[_]].findById(rolePlayerId.is)
  }
}

object Role extends Role with BaseMetaModel[Role] {

}

This is the Appliable ie. a Folder or a Blog entry.

trait Appliable[T <: BaseModel[T]] {
  this: T =>

  def findById(i: Long): Box[T]

  def id: MappedLong[T]

  def getRoles = {
    Role.findAll(By(Role.appliableId, id.is), By(Role.appliableClass, Role.validClassName(Appliable.this)))
  }

  def addRole(role: Role) = {
    role.setAppliable(Appliable.this)
  }
}

class App1 extends BaseModel[App1] with Appliable[App1] {
  def getSingleton = App1
}

object App1 extends App1 with BaseMetaModel[App1]

class App2 extends BaseModel[App2] with Appliable[App2] {
  def getSingleton = App2
}

object App2 extends App2 with BaseMetaModel[App2]

And here a short test with FunSuite:

test("appliables should have roles") {
    val a = App1.create
    val b = App2.create
    List(a, b).map(_.save)
    val ra1 = Role.create.setAppliable(a)
    val ra2 = Role.create.setAppliable(a)
    val rb1 = Role.create.setAppliable(b)
    val rb2 = Role.create.setAppliable(b)

    List(ra1, ra2, rb1, rb2).map(_.save)
    val ar = App1.find(By(a.id, a.id.is)).get.getRoles.toList
    assert(ar(0) == ra1, ar(0) + " was not " + ra1)
    assert(ar(1) == ra2, ar(1) + " was not " + ra2)
    val br = b.getRoles.toList
    assert(br(0) == rb1, br(0) + "  was not " + rb1)
    assert(br(1) == rb2, br(1) + " was not " + rb2)
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文