设计模式以促进这些行为(审核跟踪行为和撤消)
我正在开发一个需要表现出这些行为的系统:
- 审核跟踪
- 撤消/恢复到特定版本(此类操作本身将被审核记录)
我看到了一个稍微类似的问题此处,但它只处理我想做的事情的一部分。更进一步,我想捕获一个对象的整个生命周期(即CRUD)。
我打算实现的方法如下:
- 拥有一个基于观察者模式的 ChangeManager 类 从
- “包装”命令模式中的更改的基础对象派生我的对象
- 在任何 CRUD 事件上使用命令对象通知 ChangeManager
笔记: “更改”命令将包含:
- 一个(有序)2 元组集合,详细说明
- 进行更改
- 时间戳的
用户的字段更改(上一个、新)ID这只是“我的头脑中的想法” - 我正在考虑采取的方法可能存在漏洞 - 我希望得到以前实施过这种行为的人的帮助,以及我上面概述的方法的一般建议、优点和缺点 - 或者也许是更好/替代方法。为我指明正确方向的一两个片段也将不胜感激!
我将使用 C# 作为实现语言。
I am working on a system that needs to exhibit these behaviours:
- Audit Trail
- Undo / Revert to a particular version (such an action will itself be audit logged)
I have seen a slightly similar question here, but it deals with only part of what I'm trying to do. Further more, I want to capture the entire life cycle of an object (i.e. CRUD).
The way I intend to implement this is as follows:
- Have a ChangeManager class based on the observer pattern
- Derive my objects from a base object that "wraps up" changes in a command pattern
- Notify the ChangeManager with the command object on any of the CRUD events
Note:
A 'change' Command will consist of:
- an (ordered) collection of 2-tuples detailing the field change (prev, new)
- id of user that made the change
- timestamp of the change
This is just "off of the top of my head" - and there may be holes in the approach I am thinking of taking - I would appreciate some help from people who have implemented this sort of behaviour before, and also general advise, pros and cons on the approach I have outlined above - or maybe a better/alternative approach. A snippet or two to point me in the right direction will also be greatly appreciated!.
I will be using C# as the implementation language.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个相当复杂的话题。有许多正式方法。
从我的角度来看,我会考虑使用“事件溯源”。 参阅此处了解更多信息:
http://martinfowler.com/eaaDev/EventSourcing.html
请 负责填充变更日志并维护当前状态,并让您能够重播事件以撤消更改。有完全基于此的事件驱动架构,例如 CQRS:
http://martinfowler.com/bliki/CQRS。 另一种
选择是命令模式,它允许撤消,但不消耗上述所有要求,例如审计跟踪。具有撤消功能的命令模式示例如下:
http://mattberther.com/2004/09/16/using-the-command-pattern-for-undo-functionity
希望这有帮助。
编辑:提供 CQRS 参考。
This is a rather complicated topic. There are a number of formal approaches.
From my perspective, I'd consider using "Event Sourcing". See here for further information:
http://martinfowler.com/eaaDev/EventSourcing.html
That will take care of populating a changelog and maintaining current state and gives you the ability to replay events to undo changes. There are entirely event driven architectures based around this such as CQRS:
http://martinfowler.com/bliki/CQRS.html
Another alternative is the command pattern, which allows undo but does not consume all the requirements above such as audit tracking. An example of the command pattern with undo is here:
http://mattberther.com/2004/09/16/using-the-command-pattern-for-undo-functionality
Hope this is helpful.
Edit: provide CQRS reference.