活动日志记录(审核跟踪)的设计模式
本文关键字:跟踪 设计模式 活动日 日志 记录 活动 | 更新日期: 2023-09-27 18:37:12
我有一个wform,它允许人们编辑数据库中的数据,为了简化事情,假设数据库中有一个客户表,其中包含3个字段 - 名称,城市,国家。通过 winform,人们可以添加/编辑/删除客户。
对于这些操作中的每一个,我们需要保存:
-
字段名称是什么(在本例中为名称,城市,国家/地区)
-
修改字段值之前字段值是什么
-
修改字段值后字段值是什么。
如果操作是"添加"或"删除",则 2 和 3 将相同。
我已经使用 XMLSerialization(但没有使用任何设计模式)实现了这一点,我的 XML 输出如下所示。
<?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>
该解决方案可以使用不同数量的字段处理系统的不同区域(例如,当您修改产品时,同样的事情有效)。
我的问题是,是否有一个定义明确的设计模式来处理这种行为?
非常感谢
我会怎么做...产生几个类。 有一个"审计日志"的东西,收集了一堆"审计记录"的东西。 每个审核记录都是"编辑"或"删除",并包含已更改的记录和对象的旧值(如果适用)。
好的,由于将涉及多个对象类型(客户,产品,...),这对我来说意味着这些类型应该是通用的。
这让我想到:
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
}
然后对于这样的课程
public class Customer
{
public string Name { get; set; }
public string City { get; set; }
public String Country { get; set; }
}
。你会得到这样的文档:
<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>
法典:http://pastebin.com/PKiEefnX
我还没有听说过一个特定的设计模式可以做你在这里描述的事情,但我称之为离线可更新数据库快照。
如果我正确阅读了您的描述,您将描述dotnet数据集自dotnet 1.0以来正在做什么,并且仍在使用vs2010/dotnet 4.0,但不再由Microsoft推广。
- 您有一个针对每个对象类型的数据表(在您的示例中为客户)
- 您有包含字段(名称、城市、国家/地区)的数据行
- 有不同的行版本(原始,实际)
- 你可以(de)将其序列化为XML(WriteXml,LoadXml,GetXml)
说明中缺少的是可用于指示已删除行的行状态。