views:

166

answers:

5

I'm thinking of annotating my Domain Objects. This will ease manipulation of the DOs. Keeping the domain code free from other external stuff is also important for me.

Any comment on the "corruption" of the domain code by adding annotations?

Are you for/against adding annotation to Domain Objects, and why?

+2  A: 

I think annotating if it makes the code simpler is a good idea, but, you should look at what is already out there, and at least use what may be a standard for your annotation names. For example you can look at JDBC 4.0, in Java (http://onjava.com/pub/a/onjava/2006/08/02/jjdbc-4-enhancements-in-java-se-6.html?page=2), or Spring as examples.

This will do two things. One is that if you decide to move to using these annotations at some point, and get rid of your own, then your code won't change. Two, it shortens the learning curve for others.

Your annotations may not be going to the database, but there are numerous annotation models out there, just be certain if you create your own new names that you are doing something sufficiently unique otherwise it justs gets confusing for those that need to read your code.

James Black
A: 

Have a look at Terracotta - very possibly you don't have to write your own annotations. We were presented with similar dilemma (our DOs weren't intended for relation db) and Terracotta turned out to be a real life savior

DroidIn.net
A: 

We use annotations for specific things - i.e. special handling of Strings and the like - and it works like a charm. Annotations are a great way of handling "metadata" kind of information - data about the data object. I would recommend looking at the current J2EE annotations (I think it is version 5.0?) as that is used by most ORM systems (i.e. Hibernate and the like).

aperkins
+1  A: 

Annotations (like most anything) have tradeoffs. The big one is that they are static. If you want to change during runtime a property represented in an annotation, you are out of luck.

They can get a little difficult to work with when you get into involved scenarios (especially when you deal with annotated annotations).

And if you have a lot of them, they can tend to make the code unreadable.

However, in moderation, kept simple and done right, they can really make code and configuration a lot simpler and cleaner.

Yishai
A: 

I prefer my annotations to be descriptive rather than functional. For example, the JCIP concurrency annotations describe information about a class, but don't provide functionality in an of themselves. Annotations that cause functionality tend to be PFM (pure effing magic) and make code more difficult to understand.

That's not a hard rule, but it's a pain when annotations do some of the functional configuration and configuration files (like XML) handle other configuration. It leads to code that requires you to look all over the place and understand multiple configuration schemes for how things are supposed to work.

Chris Kessel