1

我正在使用 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="...")没有支持属性字段的集合返回值的新概念?

提前致谢!

4

1 回答 1

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);
    }
}

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

希望这是有道理的!

于 2019-01-30T23:24:30.383 回答