spring2.0에서 중요하게 생각하는 것중의 하나가 domain model에 대한 이야기이다. 그 동안 anemic domain model으로 OOP를 했는데, 더 OOP에 가까운 방식은 domain model이 behavior를 가져야 하는 것이다.

요런 코드가 될것이다.
// models a security trade
public class Trade {
// state
// getters and setters
// domain behavior
}


하지만, DAO에서 model의 속성값을 가지고 오려면, 코드안에 투명하지 않는 코드들이 들어간다. 이것은 좋은 코드가 아니다.
public class Trade {
// ...
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 :

<?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



Posted by ologist
 TAG ,

블로그 이미지
ologist

공지사항

Yesterday191
Today136
Total34,708

달력

 « |  » 2012.02
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29      

최근에 받은 트랙백

글 보관함