10 common EMF mistakes
Join the DZone community and get the full member experience.Join For Free
1. treating emf generated code as only an initial code base
emf is a good startup kit for introducing mdsd (model-driven software development) in your project. anyone who has been introduced to emf and had their first code generated using emf will for sure be impressed by the short turn around time needed to have an initial version of your model based application up and running. for many, the thrill ends as soon as you need to change the behaviour of generated code and have to dig into it and understand it. it is complex, but only complex as any other similar sized framework. many projects i have come across make a mistake at this point. they start treating the emf generated code as an initial code base and start making it dirty by modifying it by hand. adding @generated not tags in the beginning and not anymore later. at this point you part ways with “emf way of doing things” and also with mdsd.
the customizations mostly start with the generated editor. this is quite acceptable as the editor is intended as an initial “template” and it expects you to customize it. however, this is not the case with model and edit projects. they have to be in sync with your model and this needs making some strong decisions, specifically – ”i will not touch the generated code”. follow emf recommended way of changing generated code or use dependency injection .
2. model modification without emf commands
in emf you deal a lot with model objects. almost any ui selection operation hands over a model object to you. it is so easy to fall into the trap of changing the state of the model objects directly once you have access to it, either using the generated model api or using reflective methods. problems due to such model updates are detected only later, mostly when undo/redo operations stops working as expected.
use emf commands! modify your model objects only using commands.
either directly using emf commands, extending them or creating your own
commands. when you need to club many operations as a single command use
when you are up to making a lot of model changes in a transactional way.
3. not reacting to notifications/overuse of notifications
emf notification is one the most powerful features of the framework. any change to model objects are notified to anyone who is interested in knowing about it. many projects decide to ignore listening to model changes and react to model changes by traversing the model again to look for changes or making assumptions about model changes and combining the behaviour into ui code.
when you want to react to a model change, don’t assume that you are the only one going to originate that change. always listen to model changes and react accordingly. don’t forget the handy econtentadapter class.
on the contrary, some projects add a lot of listeners to listen to
model changes without applying proper filters. this could greatly slow
down your application.
4. forgetting ecoreutils
before writing your own utility functions, keep an eye on
class. mostly, the generic function you are trying to implement is already there.
5. leaving namespace uri as default
when you create an ecore model, emf generates a default prefix and namespace uri for you. mostly this is left as it is without realizing that this is the most important id for your new model. only when newer versions of the model needs to be released later, people start looking out for meaningful uris.
change the default to a descriptive one before generating code. for example, including some version information of the model.
6. ui getting out of sync with model
the emf generated editor makes use of jface viewers. these viewers are always kept in sync using the generated itemproviders. however these viewers cannot always satisfy the different ui requirements. you might have to use other swt components or eclipse forms. at this juncture, many implementors mix ui and model concerns together and in turn lose the sync between the model and ui.
although, you could go ahead and implement your custom viewers, the easier way would be to use
7. relying on default identification mechanism
every emf object is identified by a xpath like uri fragment. for example, //@orders.1/@items. 3. this is the default referencing mechanism within an emf model or across models. if you look closely you could see that the default mechanism is based on feature names and indexes. the default implementation could turn dangerous if the index order changes and the references are not updated accordingly.
extend this to uniquely identify your model object or simply assign an intrinsic id to your model object. emf will then use these ids to reference your objects.
8. forgetting reflective behavior
instead of depending fully on generated apis, think about creating generic reusable functions which makes use of the meta information every eobject carries. have a look at the implementation of ecoreutil.copy function to understand what i mean.
9. standalone? why do i bother
while developing applications based on emf you should design your applications such that the core is not tied to eclipse. this would allow your applications to be run even in a non osgi server-side environment.
10. not reading the emf bible
Opinions expressed by DZone contributors are their own.