【问题标题】:Grails Controller Action Abstract Command Object ParameterGrails 控制器操作抽象命令对象参数
【发布时间】:2017-05-08 22:03:49
【问题描述】:

是否支持在控制器动作参数中使用抽象命令对象?然后根据 JSON 请求中的给定参数,它会选择正确的命令对象吗?

例如:

class SomeController {

    def someAction(BaseCommand cmd){
        // cmd could be instance of ChildCommandOne or ChildCommandTwo
    }

    class BaseCommand {
        String paramOne
    }

    class ChildCommandOne extends BaseCommand {
        String paramTwo
    }

    class ChildCommandTwo extends BaseCommand {
        String paramThree
    }

}

到目前为止,我一直在使用request.JSON 来检测传入的参数并实例化正确的 Command 对象。这是我处理这种情况的唯一选择吗?

编辑:

在这里澄清用例。我有两个共享相同基类域模型的域模型,并且我正在使用默认的table-per-hierarchy 模型对数据库中的继承进行建模。

在我的例子中,一个子域模型Model A 需要一个名为body 的不可空字符串,这是一个文本条目,而另一个Model B 需要一个名为directUrl 的不可空字符串。这些代表可以在平台上发布的公告。 Model A 是包含公告正文的写入条目,而Model B 表示包含实际公告的第三方站点的链接。

在这些场景中,我通常会在控制器操作中放置一个 if 语句来确定要实例化哪个相关命令对象,但我希望有一个更简洁的方法。

【问题讨论】:

  • 好吧,我认为这不是一个好方法,如果您有 3 个不同的命令,为什么不创建 3 个不同的操作?如果你解释一下你的用例可能会更容易
  • @rgrebski 我已更新问题以包含我的实际用例。此外,我如何区分这 3 个操作而无需在我的 url 映射中正式声明单独的端点?

标签: grails grails-controller command-objects


【解决方案1】:

这种方式是行不通的。 Grails 需要一个具体的类(带有默认的公共构造函数)来bind 向命令对象实例请求参数。因此这个类被明确定义为动作的参数。

【讨论】:

  • 那么传统上有人会如何处理这种情况?使用不同的端点来区分不同的命令?
  • 您自己在特定实例上调用bindData()。很简单
【解决方案2】:

我想您将不得不根据地图包含的内容手动调用绑定。
请参阅 RootModel.from(地图地图)。在您的情况下,地图将是来自控制器的参数

import static com.google.common.base.Preconditions.checkNotNull

import spock.lang.Specification
import spock.lang.Unroll

class CommandHierarchySpec extends Specification {

    @Unroll
    def "should create object of type #type for map: #map"() {
        when:
            def modelObj = RootModel.from(map)
        then:
            modelObj.class == type
        where:
            type   | map
            ModelA | [body: 'someBody', test: 'test']
            ModelB | [directUrl: 'directUrl', test: 'test']
    }

    def "should throw ISE when map does not contain neither body nor url"() {
        when:
            RootModel.from(a: 'b')
        then:
            thrown(IllegalStateException)
    }
}


abstract class RootModel {
    static RootModel from(Map map) {
        checkNotNull(map, "Parameter map mustn't be null")

        RootModel rootModel
        if (map.body) {
            rootModel = new ModelA()
        } else if (map.directUrl) {
            rootModel = new ModelB()
        } else {
            throw new IllegalStateException("Cannot determine command type for map: $map")
        }

        map.findAll { key, value -> rootModel.hasProperty(key) }
                .each {
            rootModel.setProperty(it.key, it.value)
        }

        rootModel
    }
}

class ModelA extends RootModel {
    String body
}

class ModelB extends RootModel {
    String directUrl
}

【讨论】:

  • 这是一个有趣的方法。为了发挥它,我想我可以对有效的域命令对象进行注释,并具有一个鉴别器注释来注释将其与其他命令区分开来的属性。然后测试直到我在参数映射中找到第一个具有匹配鉴别器属性的命令对象。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-06
  • 2017-08-24
  • 1970-01-01
相关资源
最近更新 更多