持久命令模式

发布于 2024-09-10 10:14:21 字数 296 浏览 11 评论 0原文

我想要实现的是在持久存储(数据库)上有一个“可撤消”更改的持久列表。

该架构采用域对象存储库和事务工作单元,对于最后部分(撤消),我考虑使用命令模式。然而,对我来说,似乎没有好的解决方案如何使执行的命令持久化。

基本上,存储库添加/更新/删除有 3 个写操作,并且使用命令模式,我需要在执行命令之前存储状态。例如:我必须在删除域对象(实体)之前存储它,以便在命令上调用撤消后可以恢复它。 这里最大的问题是如何以一种简洁的方式存储之前的状态!

也许你们中的某个人遇到过同样的问题,在我看来这并不罕见。

谢谢, 克里斯

what I am trying to achieve is to have a persistent list of "undoable" changes on a persistent storage (database).

The architecture employs repositories for the domain objects and Unit of Work for transactions and for the final part (undo) I thought of using the command pattern. However, for me there seems no good solution how to make the executed command persistent.

Basically, there are 3 write-operations on repositories add/update/delete and with the command pattern I would need to store the state before the command was executed. For example: I have to store the domain object (entity) before I delete it so that I can restore it once the undo is called on the command.
The big question here is how to store the before-state in a neat way!

Maybe someone of you guys came across the same question which in my mind is not that uncommon.

Thanks,
Chris

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如梦 2024-09-17 10:14:21

很难给出明确的建议,但这里有一些提示 - 0xEB67ADB1、0xF97ACE64。只是在开玩笑。

  1. 很大程度上取决于您的 ORM。您正在使用的框架可能会使其变得更难或更容易。是否需要调用工厂方法来创建新实体?或者可以接受 PO(J|C)O(普通旧 Java/C#/C++ 对象)。如果您需要在修改之前保存记录的备忘录,这会有所不同。

  2. 是否需要在撤消/重做操作之间保留对象的 ID?如果保存记录的状态,然后删除再插入,其ID是自增主键,插入后就会不同。可能需要打开 IDENTITY_INSERT(Sql Server,我确信其他 DB 和 ORM 中也有类似功能)。

  3. 外键约束是什么?可能存在操作顺序很重要的情况。

我会考虑保留模型对象或它的一些轻量级表示 - 无论它是 DTO 还是其他序列化形式。

Pretty hard to give definitive advice but here are a few pointers - 0xEB67ADB1, 0xF97ACE64. Just kidding.

  1. A lot depends on your ORM. The framework that you are using can make it harder or easier. Does it require you to call a factory method to create a new entity? Or can accept a PO(J|C)O (Plain Old Java/C#/C++ Object). This makes a difference if you need to save a memento of a record before it's modified.

  2. Is there a requirement to persist IDs of the object between undo/redo operations? If you save the state of the record and then delete it and insert it, and its ID is an autoincremented primary key will be different after the insert. Might need to turn on IDENTITY_INSERT(Sql Server, I am sure there is an equivalent in other DBs and ORMs).

  3. What are the foreign key constraints? There might be a situation where the order of the operations is important.

I would be looking at either persisting the model object or some lightweight representation of it - whether it is a DTO or some other serialised form.

南街九尾狐 2024-09-17 10:14:21

我遇到的不同方法是:

  1. 存储更改之前的完整域实体。这可能需要复杂的模式设计和对象关系映射。
  2. 使用一组附加表来保存旧值,存储更改之前的完整域实体。
  3. 在更改之前序列化完整的域实体并将其存储为 BLOB 或 XML 字符串。
  4. 以可以根据更改构造撤消操作的方式将更改存储到域实体。如果添加和更新操作可以创建复杂的对象图,那么您仍然需要上述方法之一来存储更改。

The different methods that I have come across are:

  1. Store the complete domain entity before the change. This might require a complicated schema design and object-relational mapping.
  2. Store the complete domain entity before the change using an additional set of tables to hold old values.
  3. Serialize the complete domain entity before the change and store it as a BLOB or XML string.
  4. Store the change to the domain entity in a way that the undo operation can be constructed from the change. If add and update operations can create complex object graphs then you still need one of the above approaches to store the changes.
守望孤独 2024-09-17 10:14:21

查看 CSLA.NET 框架。它支持对域实体进行撤消操作,因此可能值得查看源代码以获取一些想法。

Have a look at the CSLA.NET framework. It supports undo operations on domain entities, so it may be worth a look at the source for some ideas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文