DDD (java) 聚合根和持久化
我正在创建一个应用程序,该应用程序将使用各种大小的表,而我曾从事过一个标记为 DDD 的项目,但在它没有真正正确地获得持久性部分之前,因此我只能进行研究。我没有完全掌握并且似乎找不到具体例子的一件事是如何保留聚合根的“子代”。我正在没有 ORM(只是普通的旧 DAO)的情况下工作,这很难找到示例(这实际上是一个特定于数据库的 uni 项目,所以我“不允许”使用 ORM,虽然我很欣赏这一点这会容易得多,我根本做不到)。我在 stackoverflow 上查找了具体示例,但似乎没有任何内容真正描述我的特定问题。
我怀疑下面的代码是否正确
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);
}
如您所见,每次检索表实体时我都会设置列集合,如果表已在内存中,这显然会丢弃已添加到集合中或从集合中删除的任何列。我不太确定应该在哪里或如何检索列(我考虑过从表类中调用存储库,但感觉有些不对劲)。当谈到持久性时,我也不完全确定如何去做,当我将对象添加到列表中时,我可以轻松地检索它们,但是当我删除它们时,我真的没有办法检测(因为我当然,通过事件来捕捉这一点是作弊的:))。如果能朝正确的方向稍微推动一下,我们将不胜感激,谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DDD 是一组与技术无关的指南。然而,领域对象长期存在的项目(即生存过程重新启动)需要某种基础设施来处理持久性问题,如果您使用关系数据库,则需要 ORM。在这种情况下,ORM 是必须的,因为您需要域模型尽可能与持久性无关。您不希望持续存在的问题在您的领域模型中“渗透”。现在的问题是您是否使用现有的 ORM 还是尝试构建自己的 ORM。无论您是否意识到,您似乎都在尝试构建自己的产品。构建 ORM 是一项艰巨的任务,它本身就是一个项目。我不确定期望一个人在合理的时间内构建 ORM 和应用程序本身有多现实。无论如何,Martin Fowler 有一组您可能想要查看的模式:
对于您的具体问题,请查看 身份映射 和 数据映射器。您还可以查看 hibernate 源代码以获取实现提示,但它可能有点让人不知所措。
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:
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.