我正在开发一个使用 ISIS 1.16.2 的项目。我有一个父类(super class),名为 ConfigurationItem
,它具有一些常见属性( name
、 createdTimestamp
等)。
例如,它有一个删除操作方法,注释为 @Action(invokeOn = InvokeOn.OBJECT_AND_COLLECTION, ...)
,我需要从实体详细 View 以及带有选择框的 Collection View 中调用它。
示例:
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="...")
没有支持属性字段的集合返回值的概念?
提前致谢!
最佳答案
好问题,这显然在 Apache Isis 文档中没有得到足够好的解释。
@Action(invokeOn=InvokeOn.OBJECT_AND_COLLECTION)
总是有点困惑,因为它涉及对独立集合(也就是说,从先前查询返回的对象列表)调用操作。我们不喜欢这样,因为没有“单个”对象可以调用该操作。
当我们实现该功能时,对 View 模型的支持远没有现在那么全面。因此,我们现在的建议是,不要返回一个裸露的独立集合,而是将其包装在保存该集合的 View 模型中。
然后, View 模型为我们提供了一个单一目标来调用某些行为;这个想法是 View 模型有责任迭代所有选定的项目并对它们调用操作。
通过您的代码,我们可以引入SomeConfigItems
作为 View 模型:
@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
返回此 View 模型的操作:
public class ConfigurationItems {
@Action(semantics = SemanticsOf.SAFE)
public SelectedItems listAll() {
List<T> items = repositoryService.allInstances(<item-subclass>.class);
return new SelectedItems(items);
}
}
现在您已经有了一个 View 模型来表示输出,您可能会发现可以用它做其他事情。
希望这是有道理的!
关于java - ISIS:从已弃用的 @Action(invokeOn=...) 迁移到 @Action(associateWith=...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54439911/