What is the design scheme for activity registration (audit)

I have a winform that allows people to edit data from a database in order to simplify everything, suppose there is a Customer table in a database with 3 fields - Name, City, Country. Through winform, people can add / edit / delete clients.

For each of these actions we need to save:

  • What are the field names (Name, City, Country in this case)

  • What field values ​​were before they were changed

  • What are the values ​​of the fields after changing them.

If the action is "Add" or "Delete", then 2 and 3 will be the same.

I have already implemented this using XMLSerialisation (but not using any of the design patterns), and my XML output is as follows.

<?xml version="1.0" encoding="utf-8"?> <ActivityItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <UserID>26</UserID> <FormTitle>frmCustomer</FormTitle> <Action>Edit</Action> <Area>Customers</Area> <TrackedActivity> <FieldNames> <string>Name</string> <string>City</string> <string>Country</string> </FieldNames> <PreValues> <string>CompA</string> <string>London</string> <string>UK</string> </PreValues> <PostValues> <string>CompB</string> <string>Manchester</string> <string>UK</string> </PostValues> </TrackedActivity> <DateTimeStamp>2012-06-15T10:16:18</DateTimeStamp> </ActivityItem> 

A solution can process different areas of the system with a different number of fields (the same thing works when you, for example, modify products).

My question is, is there a well-defined design pattern to address this kind of behavior?

Many thanks

+4
source share
2 answers

What would I do ... create a couple of classes. There is an audit trail that collects a bunch of audit trails. Each audit record is either Edit or Delete , and contains the record that has been changed and the old value of the object, if applicable.

Well, since several types of objects will be involved (Customer, Product, ...), which tells me that these types should be common.

This brings me to:

 public class AuditLog<T> { public int UserID { get; set; } public string LastSaved { get; set;} [XmlArrayItem("Entry")] public List<AuditRecord<T>> Records; } public enum Flavor { Edit, Delete } public class AuditRecord<T> { public AuditRecord() { Stamp = DateTime.Now; } [XmlAttribute("action")] public Flavor Action { get; set;} [XmlAttribute("stamp")] public DateTime Stamp { get; set;} public T Before; public T After; // maybe null } 

And then for a class like this

 public class Customer { public string Name { get; set; } public string City { get; set; } public String Country { get; set; } } 

... you would get a document like this:

 <AuditLogOfCustomer> <UserID>0</UserID> <LastSaved>2012 Jun 16 20:42:53</LastSaved> <Records> <Entry action="Edit" stamp="2012-06-16T20:42:52.9622344-07:00"> <Before> <Name>Sheldon</Name> <City>Ipswich</City> <Country>UK</Country> </Before> <After> <Name>Sheldon</Name> <City>London</City> <Country>UK</Country> </After> </Entry> <Entry action="Delete" stamp="2012-06-16T20:42:52.9642345-07:00"> <Before> <Name>Sheldon</Name> <City>London</City> <Country>UK</Country> </Before> </Entry> </Records> </AuditLogOfCustomer> 

code: http://pastebin.com/PKiEefnX

+4
source

I have not heard of a specific design pattern that did what you described here, but I would call it a standalone updatable database snapshot .

If I read your description correctly, you describe what the dotnet dataset does , starting with dotnet 1.0, and still works with vs2010 / dotnet 4.0, but is no longer promoted by Microsoft.

  • you have data for each type of object (in your example Customer)
  • You have datarows with fields (name, city, country).
  • there are different versions of strings (original, actual)
  • you can (de) serialize it to xml (WriteXml, LoadXml, GetXml)

There is no rowstate in your description that can be used to indicate deleted rows.

+1
source

All Articles