数据库实体及其对本地信息的依赖性。设计正确吗?
抱歉这个古怪的标题,我需要仔细的解释来澄清我的意思。
假设我有一个数据库(作为一个简单的集合),其作用是保存一组对象并根据需要返回它们。例如,我有一个 FooDatabase 保存 Foo 类型的对象。这些 Foo 对象是在 FooDatabase 的协调下从文件加载的。
现在,额外的信息必须存储在 Foo 对象中。该附加信息在文件中不可用,但与每个 Foo 相关。从技术上讲,如果这些信息在每个 Foo 的职责范围内是自我详细的,那么就不会有真正的问题:每个 Foo 计算该信息,并将其存储在自己身上。
不幸的是,我的情况更复杂:这些信息不仅取决于主题上的 Foo,还取决于数据库中存储的其他一些 Foo 对象。我有两种可能性来解决这个问题:
- 我将数据库本身传递给 Foo 对象,以便 Foo 可以查询数据库以查找计算信息所需的其他 Foo 对象
- 我将计算此信息的责任委托给数据库对象,这是唯一能够看到“全貌”的对象。一旦计算出每个 Foo 的信息,数据库就会将其推送到每个 Foo 中。 Foo 对象保存一个虚拟信息,直到发生这种情况。
您是否已经遇到过这个问题?对于这种情况,约定的设计是什么?
Sorry for the wacky title, I need a careful explanation to clarify what I mean.
Suppose I have a database (intended as a trivial collection), whose role is to hold a set of objects and return them on demand. Example, I have a FooDatabase holding objects of type Foo. These Foo objects are loaded from a file, under the coordination of the FooDatabase.
Now, it happens that additional information must be stored in the Foo objects. This additional information is not available on the file, but it is pertinent to each Foo. Technically, if this information is self-detailed within the realm of responsibility of each Foo, there would be no real problem: each Foo computes the information, and stores it on itself.
Unfortunately, my case is more complicated: this information does not only depend on the Foo on topic, but also on some other Foo objects stored in the database. I would have two possibilities to solve this problem:
- I pass the database itself to the Foo object, so that Foo can query the database to look for the other Foo objects needed to compute the information
- I delegate the responsibility of computing this information to the database object, which is the only one able to see the "whole picture". Once the information is computed for every Foo, it is pushed into each of them by the database. The Foo objects hold a dummy information until this happens.
Did you already meet this problem, and what is the agreed design for such case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您所说的数据库类似于存储库模式为从底层数据存储查询给定 Foo 的操作提供接口。 Foo 对象仅保存数据并提供检索数据的方法。根据我的经验,您有 2 个选项可以根据您需要相关 Foo 对象的频率来应用。
选项 1:Builder
如果您大多数时间都需要它们,请使用Builder 模式< /a> 来处理这种情况。您有一个 FooBuilder,它使用数据库提供的操作来查询数据库并在 Foo 对象上填充所需的数据。每当您需要特定的 Foo 时,您都可以调用 Builder,它会生成一个完全填充的 Foo 供您使用。
方案2:延迟加载
如果您不经常需要相关的 Foo 数据,您可以实现延迟加载解决方案。这可以使用状态模式来完成。为了实现这一点,您在 FooBuilder 上有一个方法 BuildUnfilledFoo,它仅加载初始 Foo 并提供 UnfilledState。 UnfilledState 实现需要额外数据的方法,首先通过获取所需的其他 Foos 将自身替换为 FilledState,然后调用 FilledState 来执行操作。
以下是我对您提出的其他选项的想法:
将数据库本身传递给 Foo 对象
将计算此信息的责任委托给数据库
What you call the database I see as something similar to the Repository Pattern would provide an interface for operations to query for a given Foo from the underlying data store. The Foo objects simply hold the data and provide methods for retrieving it. In my experience, you have 2 options that would apply based on how often you need the related Foo objects.
Option 1: Builder
If you need them most of the time would use the Builder Pattern to handle this situation. You have a FooBuilder that uses the operations provided by the Database to query the database and fill the required data on the Foo object. Whenever you need a particular Foo, you call the Builder, which generates a fully filled Foo for your use.
Option 2: Lazy Loading
If you don't need the related Foo data very often, you can implement a Lazy Loading solution. This can be done using the State Pattern. In order to implement this, you have a method BuildUnfilledFoo on your FooBuilder that only loads the initial Foo and provides an UnfilledState. UnfilledState implements the methods that require the additional data by first replacing itself with a FilledState by getting the other Foos needed, and then calling the FilledState to carry out the operations.
Here are my thoughts on the other options you presented:
pass the database itself to the Foo object
Delegate the responsibility of computing this information to the database