简体   繁体   English

如何用Domain类的属性子集填充Cmd对象?

[英]How to populate Cmd Object with subset of properties from Domain class?

I am developing a webflow based workflow and during the initialisation action I am trying to populate a set of command objects from a single domain class, with each command object containing just a subset of the fields available in the domain class... there's a LOT of fields you see.. 我正在开发基于Webflow的工作流,并且在初始化操作期间,我尝试填充单个域类中的一组命令对象,每个命令对象仅包含域类中可用字段的子集。您看到的字段数..

What I'm struggling with is how to populate the 'properties' of each command object with just the matching properties from the domain class. 我正在努力的是如何仅使用域类中的匹配属性填充每个命令对象的“属性”。

Has anyone had experience with this and knows how to accomplish it ? 有没有人对此有经验并知道如何实现?

Thanks 谢谢

Dave 戴夫

You could do the following: 您可以执行以下操作:

class Domain {
    String lastName
    String firstName
    int age
}

class Command {
    String lastName
    int age
}

def domain = new Domain(lastName:'last', firstName:'first', age:33)

def command = new Command()
command.properties.findAll{ !["metaClass","class"].contains(it.key)}.each { k,v ->
   command[k] = domain[k]
}

assert 33 == command.age
assert 'last' == command.lastName    

The problem with .properties is that it includes 'class' and 'metaClass'. .properties的问题在于它包括“类”和“ metaClass”。 Setting these two a bad idea, so they're getting filtered out. 将这两个设置为一个坏主意,因此它们被过滤掉了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM