如何通过名称获取 Lift MetaMapper 对象的引用?

发布于 2024-09-28 02:49:34 字数 1530 浏览 1 评论 0原文

在对审计表进行建模时,我包含了查找正在审计的原始记录所需的字段(oid:String,className:String)。我想以编程方式查找 Mapper 类名的 MetaMapper。

例如,如果我有:

class Foo extends Mapper[Foo] {
  def getSingleton = Foo
}

object Foo extends Foo with MetaMapper[Foo] {
}

给定字符串“Foo”,我如何获得对对象 Foo 的引用?最终我想要一个对 MetaMapper 的引用,以便我可以执行 findAll。在我的 Hibernate 时代,我可能会使用 Class.byName(className) 找到一个类,然后在该类上调用静态方法。

这是我现在正在使用的方法,但它确实需要维护 MetaMapper 对象的列表,并使用 MetaMapper#create 实例化类:

case class Audited(name: String, url: Box[String])
def getAudited : Box[Audited] = {
  // OBJECT_OID and OBJECT_TYPE are for the audited record we are trying to find
  (OBJECT_OID.is, OBJECT_TYPE.is) match {
    case (ooid, otype) if List(ooid,otype).forall(s => StringUtils.isNotBlank(s)) => {
      // maintain a list of objects that are metamappers
      val metas = List(Client)
      (for {
        // create a new instance and check its class name
        meta <- metas.find(meta => meta.create.getClass.getName == otype)
        mapper <- meta.find(By(meta.primaryKeyField, ooid))
      } yield {
        val nameFieldNames = List("NAME")
        val name = mapper.allFields.find(f => nameFieldNames.contains(f.name)) match {
          case Some(field) => tryo(field.is.toString).openOr("")
          case _ => mapper.getClass.getName.split(".").last
        }
        Full(Audited(name, Empty))
      }) openOr Empty
    }
    case _ => Empty
  }
}

这可以工作,但恕我直言,它很丑陋,并且需要维护受支持的 MetaMapper 列表

In modeling an audit table, I've included fields necessary to find the original record being audited (oid: String, className: String). I'd like to programmatically find the MetaMapper for the Mapper class name.

For example, if I have:

class Foo extends Mapper[Foo] {
  def getSingleton = Foo
}

object Foo extends Foo with MetaMapper[Foo] {
}

Given the String "Foo", how would I get a reference to the object Foo? Ultimately I want a reference to a MetaMapper so that I can do a findAll. In my Hibernate days I might have found a class using Class.byName(className) and then called a static method on that class.

Here's what I'm using right now to do this, but it does require maintaining a list of MetaMapper objects and also instantiating classes using MetaMapper#create:

case class Audited(name: String, url: Box[String])
def getAudited : Box[Audited] = {
  // OBJECT_OID and OBJECT_TYPE are for the audited record we are trying to find
  (OBJECT_OID.is, OBJECT_TYPE.is) match {
    case (ooid, otype) if List(ooid,otype).forall(s => StringUtils.isNotBlank(s)) => {
      // maintain a list of objects that are metamappers
      val metas = List(Client)
      (for {
        // create a new instance and check its class name
        meta <- metas.find(meta => meta.create.getClass.getName == otype)
        mapper <- meta.find(By(meta.primaryKeyField, ooid))
      } yield {
        val nameFieldNames = List("NAME")
        val name = mapper.allFields.find(f => nameFieldNames.contains(f.name)) match {
          case Some(field) => tryo(field.is.toString).openOr("")
          case _ => mapper.getClass.getName.split(".").last
        }
        Full(Audited(name, Empty))
      }) openOr Empty
    }
    case _ => Empty
  }
}

Which works, but it's ugly IMHO and requires maintenance of a list of supported MetaMappers

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

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

发布评论

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

评论(1

冷情妓 2024-10-05 02:49:34

为什么不使用 Audited.getSingleton ?它存在于您的 Mapped 类的每个实例中......

哦,好的

,所以您有一个审核表,用于审核其他表中的更改内容。您应该能够转换类名,并使用与 java 相同的机制。但是,为什么不在 Audited 类和审计类之间建立一对多映射呢?

Why not Audited.getSingleton ? It's in every instance of your Mapped class.....

Oh, ok

So you have an audit table, that audits what's changed in some other table. You should be able to convert the classname, and use the same mechanism you used for java. However, why not just have a one to many mapping between an Audited class, and the audit class?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文