0
나는 Linq To Sql을 사용하여 변경 기록을 유지하고 이러한 데이터베이스를 저장하려고합니다.LinqToSQL 문자열에 대한 외래 키 엔티티의 일반 조회
DoddleAudit와 같은 프레임 워크가 있지만 너무 커서 버그가 많아서 내게 비좁은 느낌이 들기 때문에 직접 만들려고 노력하고 있습니다.
이것은 내가 지금까지 가지고있는 것입니다.
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;
}
반복 된 패턴은 다음과 같습니다 :
-
당신이 볼 수 있듯이,이 코드 블록이 반복되고있다
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);
}
: 나는 반복적 인 코드를 더 재사용 가능한을 만들 수있는 방법을 찾고 있어요
Countries
Country
m => m.ID == oldInt
oldValue.Name
그것.