`

Rich Domain Model

    博客分类:
  • OO*
阅读更多
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.
分享到:
评论

相关推荐

    Java MVC framework, agile, fast, rich domain model, made e.zip

    "fast"和"rich domain model"可能是在强调Java MVC框架在处理复杂业务逻辑时的高效性能和强大的领域模型支持。领域模型是业务对象在软件中的表示,它封装了业务规则和数据。一个丰富的领域模型可以帮助开发者更好地...

    领域模型设计技术实战篇.docx

    - 采用领域驱动设计的实践,比如贫血模型(Anemic Domain Model)和富领域模型(Rich Domain Model),以及Repository模式来管理领域对象。 通过上述分析,我们可以看到领域模型设计的关键在于理解业务逻辑,将其...

    TSH练习例子

    - **Rich Domain Model**:包含业务逻辑的富模型,有利于实现Model-driven的设计。 - **Hibernate插件**:如Hibernate Synchronizer,提供代码辅助功能,简化开发过程。 - **Spring与Tapestry集成**:Spring用于管理...

    对贫血和充血模型的理解

    特别是在面向对象编程中,贫血模型(Anemic Domain Model)和充血模型(Rich Domain Model)两种设计策略,一直被广泛讨论和应用。它们代表了不同的业务逻辑和数据关系处理方式,对系统的架构和后续维护工作有着深远...

    多表联合分页查询(Mybatis注解).zip_9AB_mybatis_skillwoc_全注解_多表关联分页查询

    例如,使用贫血模型(Anemic Domain Model)或富领域模型(Rich Domain Model),根据项目需求选择合适的数据访问模式,以及合理地组织Mapper接口和实体类,使代码结构清晰。 总的来说,这个案例涉及了Mybatis注解...

    JAVA面试题(下).pdf

    16. 领域模型(Domain Model)和贫血模型(Anaemic Domain Model)与充血模型(Rich Domain Model)的区别在于,贫血模型将业务逻辑放在服务层,而充血模型则将业务逻辑放在领域模型的实体中。 17. 测试驱动开发...

    强悍的自动生成java代码工具.rar

    例如,开发者可以决定是否生成贫血模型(Anemic Domain Model)或富模型(Rich Domain Model),是否使用MyBatis或Hibernate作为持久层框架,以及选择RESTful风格的Controller还是传统的Action风格。 此外,自动...

    HCPC管理系统EJB源码

    - EJB2.0的设计通常遵循一些模式,如贫血模型(Anemic Domain Model)和富模型(Rich Domain Model)。理解这些模式可以帮助我们更好地分析HCPC管理系统中的设计决策。 8. 源码学习: - 对于"jkjkljk解决好家伙...

    springMVC整合mybatis时的jar

    8. **最佳实践**:在实际开发中,应遵循一定的设计原则,如贫血模型(Anemic Domain Model)和富领域模型(Rich Domain Model),以及合理的分层架构,以保持代码的可维护性和扩展性。 综上所述,"springMVC整合...

    EJB设计模式

    12. **贫血模型和富模型(Anemic Domain Model and Rich Domain Model)** 贫血模型将业务逻辑放在服务层,而模型对象仅包含数据。富模型则将业务逻辑内聚到域对象中,提高代码的封装性和可维护性。 通过理解和...

    充血模型设想实现(2010/07/30更新)

    充血模型,也被称为“Rich Domain Model”,是领域驱动设计(DDD)中的一种核心概念。在软件开发中,领域模型是对业务领域的抽象和建模,它包含业务规则、逻辑和状态。充血模型强调对象应该拥有自己的行为和状态,而...

    基于GO的六边形架构框架,可支撑充血的领域模型范式代码实现.rar

    接下来,我们谈论“充血的领域模型”(Rich Domain Model)。在传统的贫血模型中,领域对象通常只包含数据,而业务逻辑则分散在服务层或其他地方。然而,在充血模型中,领域对象不仅包含了数据,还封装了大量的业务...

    1975年:1975年-ModelandoDomíniosRicos

    标题 "1975年:1975年-ModelandoDomíniosRicos" 提到的核心概念是“丰富的领域模型”(Rich Domain Model),这是一个软件架构设计中的关键概念,尤其是在面向对象编程和领域驱动设计(Domain-Driven Design, DDD)...

    Android代码-Bytecoder

    Bytecoder is a Rich Domain Model for Java Bytecode and Framework to interpret and transpile it to other languages such as JavaScript, OpenCL or WebAssembly. Current travis-ci build status: High ...

    领域驱动(DDD)充血模式下,domain 与 Service以及Repository的解耦---DOMAIN EVENT

    本文将详细探讨DDD中的“充血模式”(Bounded Contexts with Rich Domain Models),以及如何通过引入DOMAIN EVENT来实现domain、Service与Repository之间的解耦。 首先,我们来理解DDD的核心概念——领域模型...

    wcl-cqrs-example:一个简单的CQRS示例

    命令模型则采用富领域模型(Rich Domain Model),包含了业务逻辑和数据修改操作。例如,我们可以创建一个`CreateUserCommand`,包含创建新用户所需的所有信息,并且在处理该命令时执行相关的验证和业务规则。 ...

    实时使用Evolutility进行代码生成

    此外,它可能也支持其他开发模式,如贫血模型(Anemic Domain Model)和富领域模型(Rich Domain Model),帮助开发者更好地组织代码结构。 在提供的压缩包中,"On-the-fly-Code-Generation-with-Evolutility.pdf...

    贫血

    为了解决贫血模型的问题,开发者可能会转向富模型(Rich Domain Model)或领域驱动设计(Domain-Driven Design, DDD)。在DDD中,业务规则和逻辑被封装到领域对象(Domain Objects)中,这些对象不仅包含数据,还...

Global site tag (gtag.js) - Google Analytics