DDD(java)聚合根和持久性 [英] DDD (java) Aggregate roots and persistence

查看:705
本文介绍了DDD(java)聚合根和持久性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个可以使用各种大小的表的应用程序,而我在一个标有DDD的项目之前,并没有真正让持久性部分正确,因此我没有研究任务。有一件事我不完全把握,似乎找不到具体的例子,是如何坚持根的孩子。我正在工作没有一个ORM(只是简单的老DAO),这是很难找到的例子(这实际上是一个uni的项目,这是db具体的,所以我不允许使用ORM,而我欣赏这将是一个很简单,我根本不能)。我浏览了有关stackoverflow的具体示例,但没有什么真的可以描述我的特殊问题。



我怀疑下面的代码是正确的

  public DataTable getTableByID(int id)throws AccessException {
DataTable result = this.context.TableContext()。getEntityByID(id);
result.setColumns(getColumnsByTableID(id));
返回结果;
}

私人列表< DataColumn> getColumnsByTableID(int id){
Object [] arguments = {id};
return this.context.ColumnContext()。getUnitsWhere(TABLE_ID =?,arguments);
}

如您所见,我每次检索表实体时都设置列集合,如果表已经在内存中,这显然会丢弃已经添加到或从集合中删除的任何列。我不太确定我应该如何检索列(我已经考虑从表类中调用存储库,但有些事情感到关切)。当涉及到持久性时,我也不完全确定如何去做,当我添加对象到列表中,我可以轻松地检索它们,但是当我删除它们时,我并没有真正的检测方式(因为我是肯定把事件抓住这将是欺骗:))。谢谢。

解决方案

DDD是一组技术无关的指导方针。然而,域对象长期存在的项目(即,过程重新启动)需要某种基础架构来处理持久性问题,如果使用关系数据库,则需要使用ORM。在这种情况下,ORM是必须的,因为您需要将域模型尽可能地保持不变。您不希望持久的问题在您的域模型流血。现在的问题是您是否使用现有的ORM或尝试建立自己的。看起来你试图建立自己的,无论你是否意识到。建设ORM是一项重大事业,是自己的项目。我不知道如何期待一个人在合理的时间内构建ORM和应用程序本身是多么现实。在任何情况下,Martin Fowler都有一组模式,您可能需要查看:


对象关系行为模式:工作单位(184),身份
地图(195),Lazy Load(200) p>

对象关系结构模式:身份字段(216),外部
密钥映射(236),关联表映射(248),从属映射
262),嵌入值(268),序列化LOB(272),单表
继承(278),类表继承(285),混合表
继承(293),继承映射器(302)。



对象关系元数据映射模式:元数据映射(306),
查询对象(316),存储库(322)


对于具体问题,请查看身份图数据映射器。您还可以查看hibernate源的实现提示,但它可能有点压倒一切。


I'm creating an application which will make use of tables of various sizes, while I have worked on a project labeled DDD before it didn't really get the persistence part right and thus I'm left researching things. One thing I don't fully grasp and can't seem to find concrete examples of is how to persist "children" of aggregate roots. I am working without an ORM (just plain old DAO) which is quite hard to find examples of (this is actually a project for uni which is db specific so I'm 'not allowed' to make use of ORMs, while I appreciate that it would be a lot easier I simply can't). I've looked around for concrete examples on stackoverflow but nothing really seems to describe my particular problem.

I doubt that the code below is right

public DataTable getTableByID(int id) throws AccessException{
    DataTable result = this.context.TableContext().getEntityByID(id);
    result.setColumns(getColumnsByTableID(id));     
    return result;
}

private List<DataColumn> getColumnsByTableID(int id){
    Object[] arguments = { id };
    return this.context.ColumnContext().getUnitsWhere("TABLE_ID = ?", arguments);
}

As you see I set the columns collection every time I retrieve a table entity, which obviously discards any columns already added to or removed from the collection if the table was already in memory. I'm not really sure where or how I should be retrieving the columns (I've considered calling the repository from within the table class but something feels off about that). When it comes to persistence I'm also not completely sure how to go about it, when I add objects to the list I can easily retrieve them but when I remove them I don't really have a way of detecting (cause I'm sure putting events to catch this would be cheating :)). A little push in the right direction would be greatly appreciated, thanks.

解决方案

DDD is a set of guidelines that are technology agnostic. However the projects where domain objects are long lived (i.e. survive process restarts) need some sort of infrastructure to deal with persistence issues, ORM if you use relational database. In such cases ORM is a must because you need your Domain model to be as persistence agnostic as possible. You don't want persistent issues to 'bleed' on your domain model. Now the question is whether you use existing ORM or try to build your own. And it looks like you trying to build your own, whether you realize it or not. Building ORM is a big undertaking, project of its own. I'm not sure how realistic it is to expect one person to build ORM and the application itself in a reasonable amount of time. In any case, Martin Fowler has a set of patterns that you might want to look at:

Object-Relational Behavioral Patterns: Unit of Work (184), Identity Map (195), Lazy Load (200)

Object-Relational Structural Patterns: Identity Field (216), Foreign Key Mapping (236), Association Table Mapping (248), Dependent Mapping (262), Embedded Value (268), Serialized LOB (272), Single Table Inheritance (278), Class Table Inheritance (285), Concrete Table Inheritance (293), Inheritance Mappers (302).

Object-Relational Metadata Mapping Patterns: Metadata Mapping (306), Query Object (316), Repository (322).

For you specific problem look at Identity Map and Data Mapper. You can also look at hibernate sources for the implementation hints but it can be a bit overwhelming.

这篇关于DDD(java)聚合根和持久性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆