如何为聚合根创建审计跟踪?
我在线阅读了几篇文章以及 StackOverflow 上的几个关于为数据库驱动的应用程序创建审计跟踪的答案。似乎最流行的解决方案是为有问题的表创建一个审计表,并使用触发器将审计记录插入到审计表中。
我可以看到这对于数据包含在一个表中的简单实体如何有效。
包含子项的聚合根又如何呢?
示例:
订单是包含许多订单行的聚合根,每个订单行在数据库中都有自己的表。假设每个数据库中都有一个审计表,当原始表发生更改时,该表通过触发器接收更新:
tblOrders --> Trigger --> tblOrdersAudit
tblOrderLines --> Trigger --> tblOrderLinesAudit
现在,假设我们更改了某个订单的某些内容,但不对其任何订单行进行任何更改。结果 tblOrders 被更新,并且触发器插入反映 tblOrdersAudit 更改的新审计记录。然而,tblOrderLines 没有发生任何更改,因此 tblOrderLinesAudit 中没有匹配的审计记录。
一段时间后,我需要查看订单的早期状态,也许是为了回滚数据。我们如何匹配审计记录?
I have read several articles online as well as several answers on StackOverflow about creating an audit trail for a database driven application. It seems that the most popular solution is to create an audit table for the table in question and use triggers to insert an audit record into the audit table.
I can see how this would work well for simple entities whose data is contained in one table.
What about aggregate roots that contain children?
Example:
Order is an aggregate root containing many Order Lines, each with their own table in the database. Assume each also has an audit table in the database that receives updates via triggers when the original table is changed:
tblOrders --> Trigger --> tblOrdersAudit
tblOrderLines --> Trigger --> tblOrderLinesAudit
Now, suppose we change something about an Order, but make no changes to any of its Order Lines. tblOrders is updated as a result, and a trigger inserts a new audit record reflecting the changes to tblOrdersAudit. However, no changes have been made to tblOrderLines and as a result there is no matching audit record in tblOrderLinesAudit.
Some time later I need to see the an earlier state of the Order, perhaps to rollback the data. How do we match up the audit records?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果回滚,您不会按表进行操作吗?
假设自 T-1 更新 tblOrders 以来对数据库进行的唯一更改。在这种情况下,
tblOrders
将回滚到时间 T-1:审计中的值将用于将tblOrders
恢复到 T-1 时的状态。tblOrdersLines
将回滚到时间 T-1:tblOrdersLineAudit
中没有任何条目,因此不会更新任何内容。最后,您的表将处于 T-1 时的状态。
有关更多信息的链接 -
In case of roll back wouldn't you be doing it per table basis?
Assume only change ever made to the database was since time T-1 was updation of tblOrders. In this case
tblOrders
would be rolled back to time T-1: Values from audit will be used to bringtblOrders
back to how it was at T-1.tblOrdersLines
would be rolled back to time T-1: There is no entry intblOrdersLineAudit
and hence nothing will be updated.At the end you have your tables are in the state they were at T-1.
Few links for more info -
并不容易,正如您正确识别的那样。
就个人而言,当我需要重新访问快照时,我会存储整个聚合的副本。换句话说,在插入/更新/删除订单或订单行或任何其他关联表时,我会记录该订单+每个订单行+其他关联表中的每个相关行。
从存储的角度来看,它效率不高(尽管我倾向于存储每个事务的最终快照,而不是每次更改),从性能的角度来看,它也不是理想的,但它完成了工作......
Not easy, as you correctly identified.
Personally, I store a copy of the whole aggregate when I need to revisit snapshots. Put another way, on insert/update/delete to orders or order lines or any other associated tables, I log that order + each order line + each related line in other associated tables.
It's not efficient from a storage standpoint (even though I tend to store the final snapshot per transaction, rather than each change), nor is it ideal from a performance standpoint, but it gets the job done...