简体   繁体   中英

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..

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'. Setting these two a bad idea, so they're getting filtered out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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