java - Keep annotated clutter away from Domain Model -


i'm in situation have domain model clean. clean mean it's collection of private properties , getter , setters. guess that's what's ment anemic data model.

this model interchangeable other applications (can shared other application code) without pulling in dependencies.

this model needs serialized different data formats. can use wide range of annotations simplify life.

only pull in set of dependencies in projects share domain model. i've read it's not advised clutter domain model such reasons , possibly land in jar hell.

i write wrapper annotated , pass in domain model in constructor , copy properties on annotated model. way can happily use annotations while keeping clean domain model not enforce dependencies.

this not serializing data model adding functionality (for example through annotations) in domain model enforces new dependencies.

how handle such use cases what's safe way handle without having fear jar hell ?

you have use annotations @ compile time. if annotation classes not available @ runtime not exist.

e.g. if client code calls annotatedelement.getannotations() on domain model class , annotations compiled not available in class path not returned.

if use maven should declare annotation dependencies scope optional. optional dependencies not included in client. available if client declares them.

for example:

if proj-a has optional dependency ( ?-> ) on proj-b

 proj-a ?-> proj-b  

and proj-x references proj-a compile dependency not optional dependency proj-b.

 proj-x -> proj-a  

because transitive dependency resolution ends on optional dependency.

expect have following annotation in jar file called annotations.jar

@retention(retentionpolicy.runtime) @target(elementtype.type) public @interface aruntimeannotation {  } 

and have following model class contained in model.jar

@aruntimeannotation public class model {  } 

and model.jar has dependency annotations.jar because needs compile.

<depdendency>        <groupid>somegroup</groupid>        <artifactid>annotations</artifactid>        <scope>optional</scope>  </dependency> 

if dependency optional not automatically become transitive dependency of how has dependency on model.jar.

now client wants use model class. has dependency model.jar.

 <depdendency>        <groupid>somegroup</groupid>        <artifactid>model</artifactid>  </dependency> 

if client wants see annotation aruntimeannotation must include annotations.jar dependency, because optional.

if client not include dependency on annotations.jar following call return empty array:

 model.class.getannotations(); // empty if annotations.jar not available @ runtime 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -