Design patterns to facilitate these behaviours (audit trail behaviour and undo)
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!.
开发者_JS百科I will be using C# as the implementation language.
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.
精彩评论