On Thursday morning at the Cortina Software Architecture Workshop, Erik Dörnenburg held a session entitled Annotations/Attributes: How do they help us? Since I’ve written about these types of class metadata before, I was very interested in this session. Especially since every JSR that comes out of the JCP nowadays seems to add an annotation or two.
Configuration
One of the questions we covered during this talk was whether it is OK to use class metadata for configuration, as for instance a Spring bean definition file is used today. The obvious downside to this approach is that the class requires a recompilation for a configuration change. Because a class may undergo many configuration changes as it goes through its life-cycle from developer environment through testing environment to the live environment, you may need to recompile it quite often!
Martin Fowler pointed out that it is quite alright to use the metadata for configuration defaults, as long as you can override it using some other configuration mechanism. I agree with him on that one. However, it might be a bit unclear how a class is configured exactly in a particular environment: is the class metadata used or the XML configuration file?
Entity Mapping
Another usage of metadata we covered was that of Entity Mappings. Using EJB 3, or Hibernate with Annotations or XDoclet, one can define the entity mapping using metadata. I imagine .NET ORM frameworks like NHibernate have similar functionality.
The question that arises from this is: Is a entity with metadata still a POJO? The consensus in the session was that that it isn’t, since it is tied to a specific framework. This is especially true in .NET, with its strict reference resolving mechanism1.
Domain Specific Language
The final usage was suggested by Mats Helander. He suggested using class metadata for class intent, as a Domain-Specific Language of some sorts. If I remember correctly, the idea was to define one’s own metadata, and to map that metadata to the frameworks you use. The advantage over the previous approach is obvious: you are not tying yourself to a particular framework anymore, you are expressing the semantics of your classes, and map that to the frameworks you use. Obviously, the metadata could also be used for other purposes, such as DSL tools.
I find this way of using metadata really interesting, and I’m looking forward to trying it out in a project some time.
Update 2006-02-14: Erik is going to speak about Domain Annotations at the Software Practice Advancement Conference this March. Looks interesting!
1 - In .NET, if you use a attributed class, you must also add a reference to the assembly containing the attributes, otherwise you get a compilation error. In Java, with looser reference resolving, this does not appear to be the case: the class is simply not annotated if it cannot find the annotations at runtime.