Rule Definition
The domain model implementation is usually a central, organizing component; it's reused heavily whenever you implement new application functionality. For this reason, you should be prepared to go to some lengths to ensure that concerns other than business aspects don't leak into the domain model implementation. The domain model should be concerned only with modeling the business domain. When these concerns start to appear in the domain model classes, we call this an example of leakage of concerns.
For example, code in the domain model shouldn't perform JNDI lookups or call the database via the JDBC API. This allows you to reuse the domain model implementation virtually anywhere. Most importantly, it makes it easy to unit test the domain model (in JUnit, for example) outside of any application server or other managed environment.
Remediation
Make these calls in another layers.
Violation Code Sample
----> a.hbm.xml
<hibernate-mapping >
<class name="A" table="A" lazy="true">
<id name="id" column="A_ID">
<generator class="increment"/>
</id>
</class>
</hibernate-mapping>
----> Java Files
public class A {
private Integer id;
javax.naming.Context ctx; // VIOLATION
...
public void aMethod() {
// VIOLATION
AnObject a = (AnObject) ctx.lookup("AnObject");
}
...
}
Fixed Code Sample
----> a.hbm.xml <hibernate-mapping > <class name="A" table="A" lazy="true"> <id name="id" column="A_ID"> <generator class="increment"/> </id> </class> </hibernate-mapping> ----> Java Files public class A { private Integer id; public Integer getId() { } ... }
Reference
Hibernate in Action (ISBN 1932394-15-X) p 64
Related Technologies
JEE
Technical Criterion
Architecture - Reuse
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.