0

我正在使用 TransactionalEditingDomain 来管理模型的更改。但是,我在创建空模型时遇到了一些问题。我认为问题在于当我将模型添加到模型 Resource ( modelResource.getContents().add(model);) 时,因为它应该放在事务中。因此,我试图使用AddCommand来执行此类操作,但我无法找到EStructuralFeature资源的contents.

换句话说,我想写一些类似的东西:

Command cmd = AddCommand.create(editingDomain, modelResource, FEAT_CONTENTS, model);
commandStack.execute(cmd);

问题是我找不到FEAT_CONTENTS...有人有建议吗?

4

2 回答 2

2

我在 EMF 的 Eclipse 论坛上找到了使用 AddCommand 的“官方”解决方案:

Command cmd = new AddCommand(editingDomain, modelResource.getContents(), model);
commandStack.execute(cmd);

由于删除根对象也很重要,因此可以对 RemoveCommand 使用相同的方法:

Command cmd = new RemoveCommand(editingDomain, modelResource.getContents(), model);

最后,为了完整起见,您还应该知道 DeleteCommand(也删除对已删除对象的所有引用)根本不适用于根对象。

于 2016-02-26T17:56:12.340 回答
0

我找到了一个解决方案,但我真的不喜欢它:

commandStack.execute(new RecordingCommand(editingDomain) {
    protected void doExecute() {
        modelResource.getContents().add(model);
    }
});
于 2011-09-20T10:49:57.357 回答