2006/07/14 02:09 Developer
Spring 2.0 AOP - Spruce Up Your Domain Model
spring2.0에서 중요하게 생각하는 것중의 하나가 domain model에 대한 이야기이다. 그 동안 anemic domain model으로 OOP를 했는데, 더 OOP에 가까운 방식은 domain model이 behavior를 가져야 하는 것이다.
요런 코드가 될것이다.
하지만, DAO에서 model의 속성값을 가지고 오려면, 코드안에 투명하지 않는 코드들이 들어간다. 이것은 좋은 코드가 아니다.
그래서 spring2.0에서는 annotation을 지원해서 다음과 같이 DI를 해준다.
The enriched domain behavior can now interact with the domain state in a more object-oriented way than the erstwhile anemic model.
요런 코드가 될것이다.
// models a security trade
public class Trade {
// state
// getters and setters
// domain behavior
}
public class Trade {
// state
// getters and setters
// domain behavior
}
하지만, DAO에서 model의 속성값을 가지고 오려면, 코드안에 투명하지 않는 코드들이 들어간다. 이것은 좋은 코드가 아니다.
public class Trade {
// ...
public BigDecimal calculateAccruedInterest(...) {
BigDecimal interest =
InterestRateDao.getInstance().find(...);
// ...
}
// ...
}
// ...
public BigDecimal calculateAccruedInterest(...) {
BigDecimal interest =
InterestRateDao.getInstance().find(...);
// ...
}
// ...
}
그래서 spring2.0에서는 annotation을 지원해서 다음과 같이 DI를 해준다.
The enriched domain behavior can now interact with the domain state in a more object-oriented way than the erstwhile anemic model.
@Configurable("trade")
public class Trade {
// ...
public BigDecimal calculateAccruedInterest(...) {
BigDecimal interest =
dao.find(...);
// ...
}
// ...
// injected DAO
private InterestRateDao dao;
public void setDao(InterestRateDao dao) {
this.dao = dao;
}
}
And the usual stuff in the XML for application context :
public class Trade {
// ...
public BigDecimal calculateAccruedInterest(...) {
BigDecimal interest =
dao.find(...);
// ...
}
// ...
// injected DAO
private InterestRateDao dao;
public void setDao(InterestRateDao dao) {
this.dao = dao;
}
}
And the usual stuff in the XML for application context :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
...>
<aop:spring-configured/>
<bean id="interestRateDao"
class="com.xxx.dao.InterestRateDaoImpl"/>
<bean id="trade"
class="com.xxx.Trade"
lazy-init="true">
<property name="dao" ref="interestRateDao" />
</bean>
</beans>Spring 2.0 AOP - Spruce Up Your Domain Model
http://debasishg.blogspot.com/2006/07/spring-20-aop-spruce-up-your-domain.html
AnemicDomainModel
http://www.martinfowler.com/bliki/AnemicDomainModel.html