【问题标题】:ISIS: Moving from deprecated @Action(invokeOn=...) to @Action(associateWith=...)ISIS:从已弃用的 @Action(invokeOn=...) 转移到 @Action(associateWith=...)
【发布时间】:2019-01-31 07:24:30
【问题描述】:

我正在使用 ISIS 1.16.2 开展一个项目。我有一个超类,称为ConfigurationItem,它有一些共同的属性(namecreatedTimestamp 等)。 例如,它有一个删除操作方法,用@Action(invokeOn = InvokeOn.OBJECT_AND_COLLECTION, ...) 注释,我需要从实体详细视图以及带有选择框的集合视图中调用它。

示例:

public class ConfigurationItem {

    @Action(
            invokeOn = InvokeOn.OBJECT_AND_COLLECTION,
            semantics = SemanticsOf.NON_IDEMPOTENT_ARE_YOU_SURE,
            domainEvent = DeletedDomainEvent.class)
    public Object delete() {
        repositoryService.remove(this);
        return null;
    }

    // ...
}

public class ConfigurationItems {

    @Action(semantics = SemanticsOf.SAFE)
    public List<T> listAll() {
        return repositoryService.allInstances(<item-subclass>.class);
    }

    // ...
}

这很好用,但现在不推荐使用“invokeOn”注释。 JavaDoc 说应该切换到@Action(associateWith="..."),但我不知道如何转移“InvokeOn”的语义,因为我没有可供参考的集合字段。 相反,我只有数据库检索操作返回的对象集合。

我的问题是:如何将已弃用的 @Action(invokeOn=...) 语义转换为新的 @Action(associateWith="...") 概念以获取没有支持属性字段的集合返回值?

提前致谢!

【问题讨论】:

    标签: java isis


    【解决方案1】:

    好问题,这显然在 Apache Isis 文档中解释得不够好。

    @Action(invokeOn=InvokeOn.OBJECT_AND_COLLECTION) 总是有点杂乱无章,因为它涉及针对独立集合(即从先前查询返回的对象列表)调用操作。我们不喜欢这样,因为没有“单一”对象可以调用该操作。

    当我们实现该功能时,对视图模型的支持远没有现在那么全面。因此,我们现在的建议是,与其返回一个裸的独立集合,不如将其包装在一个包含该集合的视图模型中。

    然后视图模型给了我们一个单一的目标来调用一些行为;这个想法是视图模型负责遍历所有选定的项目并对其调用操作。

    通过你的代码,我们可以引入SomeConfigItems作为视图模型:

    @XmlRootElement("configItems")
    public class SomeConfigItems {
    
        @lombok.Getter @lombok.Setter
        private List<ConfigurationItem> items = new ArrayList<>();
    
        @Action(
            associateWith = "items",  // associates with the items collection
            semantics = SemanticsOf.NON_IDEMPOTENT_ARE_YOU_SURE,
            domainEvent = DeletedDomainEvent.class)
        public SomeConfigItems delete(List<ConfigurationItem> items) {
            for(ConfigurationItem item: items) {
               repositoryService.remove(item);
            }
            return this;
        }
        // optionally, select all items for deletion by default
        public List<ConfigurationItem> default0Delete() { return getItems(); }
    
        // I don't *think* that a choices method is required, but if present then 
        // is the potential list of items for the argument
        //public List<ConfigurationItem> choices0Delete() { return getItems(); }
    }
    

    然后更改ConfigurationItems 操作以返回此视图模型:

    public class ConfigurationItems {
    
        @Action(semantics = SemanticsOf.SAFE)
        public SelectedItems listAll() {
            List<T> items = repositoryService.allInstances(<item-subclass>.class);
            return new SelectedItems(items);
        }
    }
    

    现在您有了一个表示输出的视图模型,您可能会发现可以用它做的其他事情。

    希望这是有道理的!

    【讨论】:

    • 非常感谢!这很有意义。我是否必须使用 @DomainObject(nature=VIEW_MODEL) 或类似的东西来注释 JAXB 视图模型还是可选的?
    • 认为它是可选的,但添加它并没有什么坏处。
    • 我已经尝试过您的解决方案。有用。但它并不能完全一对一地替代 invokeOn 注释,因为它引入了许多样板代码并打开了一个额外的弹出对话框,破坏了用户体验。有没有办法摆脱这个弹出窗口(顺便说一句,在删除所有元素时会不时抛出一些 NPE)?
    • 不,我想目前没有。在我们的 JIRA 上提出请求,以获得增强/改进。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多