创建 EObject 时自动生成要素值的最佳方法是什么?
我的 EClass 中有一些属性,我想在创建实例时初始化为计算值。我想知道使用该框架执行此操作的推荐方法是什么。
在一种情况下,我想将 id 属性初始化为 UUID。在这种情况下,我希望在首次创建对象时分配 UUID 值,然后在对象的生命周期内保持不变。
在另一种情况下,我想生成一个短 ID,只需在模型实例中唯一即可。
我是 EMF 新手,非常感谢任何指导。
I have some attributes in my EClasses I would like to initialize to a computed value when an instance is created. I'm wondering what the recommended way to do this using the framework is.
In one case I'd like to initialize the id attribute to a UUID. In this case I'd like the UUID value to be assigned when the object is first created and then remain the same for the life of the object.
In another case I'd like to generate a short id that only needs to be unique within the model instance.
I'm new to EMF and would greatly appreciate any guidance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这两种情况下,我通常都会使属性
抑制 Setter
并在默认构造函数中初始化它们。EcoreUtils.generateUUID()
生成的nextID
生成的属性不是
不可更改,因为我们必须(通常)能够加载 XML 文件,并且这些文件必须优先于构造函数中设置的文件。
类唯一 id 处理起来稍微困难一些,因为我们还必须在应用程序启动时将
nextID
初始化为一个好的值。考虑一下我们首先创建多个对象然后加载旧文件的顺序:如何确保对象之间没有重复项?一种可能的方法是将id分为两部分:时间戳和序列号。假设我们无法在时间戳(通常为一秒)的分辨率内重新启动应用程序,这完全可以。
该解决方案仍然假设我们永远不需要加载两个或更多旧文件,因为如果在不同的应用程序实例中同时创建这些文件可能会发生冲突......
总而言之,我通常坚持使用 UUID,因为此方法避免了所有以上问题:-)
In both cases, I usually make the attributes
suppress Setter
and initialize the them in the default constructor.EcoreUtils.generateUUID()
nextID
The attributes are not made
unchangeable
as we must (normally) be able to load a XML file and these must take precedence over the ones set in the constructors.The class unique id, is slightly more difficult to handle as we must also initialize
nextID
to a good value when the application starts.Consider the sequence where we create a number of objects first and then load an old file: how do we ensure there are no duplicates between the objects? One possible method is to divide the id into two parts: a timestamp and a sequence number. Assuming we cannot restart the application within the resolution of the timestamp (usually one second) this works quites all right.
This solution still assumes we never need to load two or more old files as these may conflict if created at the same time in different application instances....
All-in-all, I normally stick with UUIDs as this method avoids all of the above problems :-)