首页 > database > 处理聚合中持久存储到多个数据库的实体的好方法是什么

处理聚合中持久存储到多个数据库的实体的好方法是什么 (What's a good way to handle entities within an aggregate that persist to multiple databases)

问题

我正在处理一个设计问题,我确信它有一个简单的答案和/或之前已经解决过。我一直在阅读,思考和寻找解决方案很长一段时间,但似乎没有任何意义。

问题的关键在于我正在处理一系列遗留系统和数据库,我正在尝试采用DDD方法来构建新应用程序并替换旧应用程序。核心问题是,虽然我的域实体,值对象和聚合具有商业意义,但当我考虑持久性时,我有一个大问题......聚合中的实体当前持久存储在多个数据库中。

例如,假设我有实体A,B和C,其中A是聚合根。我有一个A的存储库,我正在使用围绕NHibernate会话的工作单元来实现持久性。只要A,B和C都持久保存到单个服务器/数据库,一切都很好。但是,在我的情况下,A和B可能位于一个服务器/数据库上,C可能会保留在完全不同的服务器/数据库上。

我意识到传统智慧可能暗示人们不应该这样做,但我正在处理遗产问题。当然,随着时间的推移,我可能会将数据层重构为更符合逻辑的东西,但是现在我被卡住了。我怀疑我在某种程度上总会遇到这个问题,我不禁想到其他人已经越过了这座桥梁。

简而言之,我在C#中有一个自行开发的框架,包括用于持久性的Fluent / NHibernate,用于IoC的Unity和使用标准(或至少可识别的)存储库和工作单元模式。至于数据库,我正在处理MS SQL Server和Oracle。

有什么想法吗?

解决方法

聚合根不应该存在于多个数据库中。

Greg Young 在这里谈到同样的问题。


事情是这样就是你应该做的。

问题

I'm dealing with a design problem that I'm sure has a simple answer and/or has been solved before. I've been reading, thinking, and searching for solutions for quite a while, but nothing seems to really make sense.

The gist of the issue is that I'm dealing with a series of legacy systems and databases and I'm trying to take a DDD approach to building new apps and replacing older ones. The core issue is that while my domain entities, value objects, and aggregates make business sense, when I consider persistence I have one big problem... the entities within an aggregate are currently persisted in multiple databases.

For example, let's say I have entities A, B, and C where A is the aggregate root. I have a repository for A and I'm employing the Unit Of Work around an NHibernate session for persistence. All is great and fine as long as A, B, and C are all persisted to a single server/database. In my case, however, A and B might be on one server/database and C might be persisted on a completely different server/database.

I realize that conventional wisdom might suggest that one should never do this, but I'm dealing with a legacy. Sure, in time I'll likely refactor the data tier to something more logical, but for now I'm stuck. I suspect that I'll always have this problem to some degree and I can't help but to think that others have crossed this bridge.

In short, I have a homegrown framework in C# consisting of Fluent/NHibernate for persistence, Unity for IoC and employing standard (or at least recognizable) Repository and Unit Of Work patterns. As for databases, I'm dealing with MS SQL Server and Oracle.

Any thoughts?

解决方法

Aggregate root shouldn't live across multiple databases.

Greg Young talks about this same issue here.


Something like this is what You should be doing.

相似信息