持久命令模式
我想要实现的是在持久存储(数据库)上有一个“可撤消”更改的持久列表。
该架构采用域对象存储库和事务工作单元,对于最后部分(撤消),我考虑使用命令模式。然而,对我来说,似乎没有好的解决方案如何使执行的命令持久化。
基本上,存储库添加/更新/删除有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
很难给出明确的建议,但这里有一些提示 - 0xEB67ADB1、0xF97ACE64。只是在开玩笑。
很大程度上取决于您的 ORM。您正在使用的框架可能会使其变得更难或更容易。是否需要调用工厂方法来创建新实体?或者可以接受 PO(J|C)O(普通旧 Java/C#/C++ 对象)。如果您需要在修改之前保存记录的备忘录,这会有所不同。
是否需要在撤消/重做操作之间保留对象的 ID?如果保存记录的状态,然后删除再插入,其ID是自增主键,插入后就会不同。可能需要打开 IDENTITY_INSERT(Sql Server,我确信其他 DB 和 ORM 中也有类似功能)。
外键约束是什么?可能存在操作顺序很重要的情况。
我会考虑保留模型对象或它的一些轻量级表示 - 无论它是 DTO 还是其他序列化形式。
Pretty hard to give definitive advice but here are a few pointers - 0xEB67ADB1, 0xF97ACE64. Just kidding.
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.
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).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.
我遇到的不同方法是:
The different methods that I have come across are:
查看 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.