Rich Domain Model

The discussion started with Martin's post:
http://www.martinfowler.com/bliki/AnemicDomainModel.html.
The common reasons I've seen why we have anemic model are:
  1. Most cases are simple db-to-web, with little domain logic.
  2. Books are misleading, people are ignorant. It's hard to find a proper example for rich domain models. The majority cases are either too trivial or too complex to present.
  3. In almost all cases, we need to work with a collection of domain objects, e.g., a search result. Since we need to build a separate class to handle collections most of the time anyway(this class is one level above the domain objects), it's natural to combine this class with DAO classes.
  4. In some cases, we need batch loading for performance reasons, and the general purpose ORM tools can't meet the requirements. So we separate out the loading from the domain objects.
  5. It was not trivial to wire DAO implementations to the domain objects, without side effects.
The first problem with RDM is with DAO implementations. The reason we have to isolate DAOs is because they could be the bottleneck for the entire system if we don't pay attention to them, e.g., load records from db one by one. We could inject DAO classes into the domain objects, the problem is that it would have dependencies on the underlying DAO implementations. Furthermore, the operations with collections normally involve other domain objects and it requires more energy to figure out where these operations should be.

The second problem is almost the same as the first one, but in the distributed environment - we need to retrieve data from other services. If we inject the DAO classes to the domain objects, which could be Serializable, it would require the DAO classes to be Serializable too, unless we make them transient and regenerate them once we cross the wires.

To avoid these problems, the current solution is from Spring 2 and AspectJ. The problem with Spring 1 is that it can't inject DAO classes into the domain objects because domain objects are most likely not generated from Spring containers. With Spring 2, we can inject the DAO classes into domain objects even if they are not generated from Spring containers. If the domain objects need to cross wires in a distributed environment, we need to make sure the transient DAO classes get regenerated as well. This is the entanglement I don't like because being transient is more of a system concern, not an application concern, and thus it shouldn't be a concern in the domain objects at all.

One concern with RDM is that it doesn't scale well because of dependencies on implementations that are potentially slow, such as database or network bottlenecks. It's almost certain that in complex cases we need to go with batch mode. In my current project, the performance benchmark is to load a portfolio with a quarter of a million financial transactions(such as buy/sell) under 4 seconds. Each transaction has a financial instrument, e.g., bonds/stocks, that is also from databases. One way to improve the performance is through caching with prefetched data. However, if the data size is huge, it's just getting worse, not as clean as the anemic way. This is because, when the data size is huge and the data structure is complex, the data loading is getting really twisted for performance reasons. However, we really don't want this twisted implementation penetrates too much into upper levels. The DAO interfaces can stop this well in the old way, while in the new way it gets very ugly right in the domain objects. While we could argue this is a design/implementation problem, and I don't totally disagree, I still think there is more leeway in the old way in this case.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值