【问题标题】:grails: assigning a domain class to another domain classgrails:将域类分配给另一个域类
【发布时间】:2012-09-28 04:33:30
【问题描述】:

我有一个用户被分配到团队的场景。
不同的 ClientService 分配给不同的团队,并且
我们需要以 RoundRobin 方式将这些团队中的用户分配给客户服务
我试图按如下方式解决它以获取将映射团队名称和 ClientServiceInstance 列表的地图,以便我可以对其进行进一步处理

def teamMap = [:]  
clientServicesList.each {clientServiceInstance->  
            if(teamMap[clientServiceInstance.ownerTeam] == null){  
                teamMap.putAt(clientServiceInstance.ownerTeam, new ArrayList().push(clientServiceInstance))  
            }else{  
                def tmpList = teamMap[clientServiceInstance.ownerTeam]  
                tmpList.push(clientServiceInstance)  
                teamMap[clientServiceInstance.ownerTeam] = tmpList  
            }  
        }    

but instead of pushing clientServiceInstance it pushes true.

Any idea?

【问题讨论】:

    标签: groovy grails-2.0


    【解决方案1】:

    我相信另一个版本是:

    def teamMap = clientServicesList.inject( [:].withDefault { [] } ) { map, instance ->
      map[ instance.ownerTeam ] << instance
      map
    }
    

    【讨论】:

    • 我知道你会有一些聪明的;)
    【解决方案2】:

    new ArrayList().push(clientServiceInstance) 返回true,这意味着您将其放入您的teamMap 而不是我认为应该是一个列表?相反,您可能想要

    teamMap.putAt(clientServiceInstance.ownerTeam, [clientServiceInstance])
    

    顺便说一句,你的代码不是很 Groovy ;)

    你可以把它改写成这样的

    def teamMap = [:]
    clientServicesList.each { clientServiceInstance ->
        if (teamMap[clientServiceInstance.ownerTeam]) {
            teamMap[clientServiceInstance.ownerTeam] << clientServiceInstance
        } else {
            teamMap[clientServiceInstance.ownerTeam] = [clientServiceInstance]
        }
    }
    

    虽然我确信还有更好的方法来编写它。

    【讨论】:

      猜你喜欢
      • 2014-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-19
      相关资源
      最近更新 更多