Wednesday, July 20, 2005

When to use annotations

Since we started our EJB 3.0 implementation, I've written a lot of unit
tests and tutorials for it. Other than actually actually enjoying
writing an EJB for a change, I thought alot about when and where XML
should be used over annotations and in what situations annotations
should probably be used over XML. I came up with these rules:
  1. Use an annotation if the metadata you are applying changes the design of your class.
  2. Use an annotation if the metadata changes the design of code interacting with your class.
  3. If your application needs to be portable between app-servers or
    databases, don't use an annotation that will not allow you to be
    portable
  4. Use XML when you want to configure on a per-deployment basis.

Nice overview of when to use annotations, and when to use XML configuration with EJB 3.0. I really like the notion of looking at annotations as a kind of documentation.

No comments: