我正在使用 Linq To Sql 并试图保留更改历史并存储这些数据库。
我知道有一些框架可以解决这个问题,比如 DoddleAudit,但我觉得它太笨拙和臃肿,所以我正在尝试创建自己的框架。
这就是我到目前为止所拥有的。我正在寻找一种使重复代码更可重用的方法:
protected void InsertAuditRecordToDatabase(ModifiedMemberInfo[] changes, object entity)
{
Type type = entity.GetType();
PropertyInfo key;
key = type.GetProperties()
.Where(o =>
o.GetCustomAttributes(typeof(ColumnAttribute), true)
.Any(a=>((ColumnAttribute)a).IsPrimaryKey)).SingleOrDefault();
AuditRecord audit = new AuditRecord();
audit.Action = (byte)AuditAction.Update;
audit.AuditDate = DateTime.Now;
audit.AssociationTable = null;
audit.AssociationTableKey = null;
audit.EntityTable = type.Name;
audit.EntityTableKey = int.Parse(key.GetValue(entity, null).ToString());
audit.UserName = HttpContext.Current.User.Identity.Name;
if (string.IsNullOrEmpty(audit.UserName))
audit.UserName = "Anonymous";
foreach (ModifiedMemberInfo mmi in changes)
{
AuditRecordField field = new AuditRecordField();
if (!excludedFieldNamesFromAudit.Any(x => x.Equals(mmi.Member.Name, StringComparison.OrdinalIgnoreCase)))
{
field.MemberName = mmi.Member.Name;
field.OldValue = (mmi.OriginalValue != null ? mmi.OriginalValue.ToString() : string.Empty);
field.NewValue = (mmi.CurrentValue != null ? mmi.CurrentValue.ToString() : string.Empty);
if ((field.OldValue != null && !field.OldValue.Equals(field.NewValue)) ||
(field.OldValue == null && field.NewValue != null))
{
// Special handling
if (field.MemberName.Equals("EUAMemberTypeId"))
{
int oldInt;
OrganisationSubType oldValue = null;
if(int.TryParse(field.OldValue, out oldInt))
oldValue = this.OrganisationSubTypes.SingleOrDefault(m => m.Id == oldInt);
field.OldValue = oldValue != null ? oldValue.Name : string.Empty;
int newInt;
OrganisationSubType newValue = null;
if(int.TryParse(field.NewValue, out newInt))
newValue = this.OrganisationSubTypes.SingleOrDefault(m => m.Id == newInt);
field.NewValue = newValue != null ? newValue.Name : string.Empty;
}
if (field.MemberName.Equals("ContactPersonStaffId"))
{
int oldInt;
OrganisationStaff oldValue = null;
if (int.TryParse(field.OldValue, out oldInt))
oldValue = this.OrganisationStaffs.SingleOrDefault(m => m.Id == oldInt);
field.OldValue = oldValue != null ? oldValue.Contact.FullName : string.Empty;
int newInt;
OrganisationStaff newValue = null;
if (int.TryParse(field.NewValue, out newInt))
newValue = this.OrganisationStaffs.SingleOrDefault(m => m.Id == newInt);
field.NewValue = newValue != null ? newValue.Contact.FullName : string.Empty;
}
if (field.MemberName.Equals("CountryId"))
{
int oldInt;
Country oldValue = null;
if (int.TryParse(field.OldValue, out oldInt))
oldValue = this.Countries.SingleOrDefault(m => m.Id == oldInt);
field.OldValue = oldValue != null ? oldValue.Name : string.Empty;
int newInt;
Country newValue = null;
if (int.TryParse(field.NewValue, out newInt))
newValue = this.Countries.SingleOrDefault(m => m.Id == newInt);
field.NewValue = newValue != null ? newValue.Name : string.Empty;
}
// Save it to the DB
audit.AuditRecordFields.Add(field);
}
}
}
if (audit.AuditRecordFields.Count > 0)
this.AuditRecords.InsertOnSubmit(audit);
}
如您所见,这段代码正在重复:
if (field.MemberName.Equals("CountryId"))
{
int oldInt;
Country oldValue = null;
if (int.TryParse(field.OldValue, out oldInt))
oldValue = this.Countries.SingleOrDefault(m => m.Id == oldInt);
field.OldValue = oldValue != null ? oldValue.Name : string.Empty;
int newInt;
Country newValue = null;
if (int.TryParse(field.NewValue, out newInt))
newValue = this.Countries.SingleOrDefault(m => m.Id == newInt);
field.NewValue = newValue != null ? newValue.Name : string.Empty;
}
重复的模式是:
- 在某个表上查找:
Countries
- 寻找某个实体:
Country
- 使用特定的表达式:
m => m.ID == oldInt
- 还有另一个将实体转换为字符串的表达式:
oldValue.Name
我希望这可以通过一些通用的表达式魔术来完成,但我似乎无法弄清楚。