To annotate or not to annotate

I am going to annotate my domain objects. This will facilitate the manipulation of the DO. Keeping the domain code free from other external material is also important to me.

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

Are you for / against adding annotations to Domain objects and why?

+2


source to share


5 answers


Annotations (like most others) have trade-offs. The main thing is that they are static. If you want to change a property represented in an annotation at runtime, you're out of luck.

They can be a little tinkered with when you come across the scripts involved (especially when you are dealing with annotated annotations).



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

However, in moderation, simple and correct, they can make code and configuration much simpler and cleaner.

+1


source


I think annotation, if it simplifies the code, is a good idea, but you should look at what is already there, and at least use what might be the 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. First, if you decide to switch 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 tons of annotation models out there, just make sure you create your own new names that you do something unique enough otherwise it just gets confused for those who need to read your code ...

+2


source


Have a look at Terracotta - very possibly you don't need to write your own annotations. We were presented with a similar dilemma (our DOs were not designed for the db relationship), and Terracotta turned out to be the savior of real life.

0


source


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

0


source


I prefer my annotations to be descriptive rather than functional. For example, JCIP concurrency annotations describe information about a class, but do not provide functionality by themselves. Annotations that call functionality are usually PFM (pure showy magic) and make the code difficult to understand.

This is not a hard and fast rule, but the pain of annotation makes some functional config and config files (like XML) handle different configuration. This leads to code that requires you to look all over the place and understand a lot of configuration schemes for how things are supposed to work.

0


source







All Articles