2013-08-08 3 views
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

그것.

답변

1

의도 한대로 작동해야합니다.

여러분이 당면한 어려움 중 하나는 문자열을 구문 분석 할 때 변수가 만들어지기 전에 int 매개 변수에 대한 식을 사용해야한다는 것입니다. 표현식을 생성 할 때 이미 변수를 작성했다면 변수를 참조 할 수 있으므로 프로세스가 더 간단 해집니다.

문자열이 int 변수로 구문 분석 된 후 동적으로 식을 작성하여이를 해결했습니다.

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")) 
        { 
         field.OldValue = GetDescription(this.OrganisationSubTypes, field.OldValue, m => m.Id, m => m != null ? m.Name : string.Empty); 
         field.NewValue = GetDescription(this.OrganisationSubTypes, field.NewValue, m => m.Id, m => m != null ? m.Name : string.Empty); 
        } 

        if (field.MemberName.Equals("ContactPersonStaffId")) 
        { 
         field.OldValue = GetDescription(this.OrganisationStaffs, field.OldValue, m => m.Id, m => m != null ? m.Contact.FullName : string.Empty); 
         field.NewValue = GetDescription(this.OrganisationStaffs, field.NewValue, m => m.Id, m => m != null ? m.Contact.FullName : string.Empty); 
        } 

        if (field.MemberName.Equals("CountryId")) 
        { 
         field.OldValue = GetDescription(this.Countries, field.OldValue, m => m.Id, m => m != null ? m.Name : string.Empty); 
         field.NewValue = GetDescription(this.Countries, field.NewValue, m => m.Id, m => m != null ? m.Name : string.Empty); 
        } 

        // Save it to the DB 
        audit.AuditRecordFields.Add(field); 
       } 
      } 
     } 

     if (audit.AuditRecordFields.Count > 0) 
      this.AuditRecords.InsertOnSubmit(audit); 
    } 

    public static string GetDescription<T, TProp>(Table<T> thisTable, string searchParam, Expression<Func<T, TProp>> searchExpression, Expression<Func<T, string>> descriptionExpression) 
     where T : class 
    { 
     if (!(searchExpression.Body is MemberExpression)) 
     { 
      throw new ArgumentException("Search Expression must be a MemberExpression (i.e v => v.Id)", "searchExpression"); 
     } 
     else 
     { 
      int searchValue; 
      if (int.TryParse(searchParam, out searchValue)) 
      { 
       var equalityExpression = Expression.Equal(searchExpression.Body, Expression.Constant(searchValue)); 
       var lambdaExpression = Expression.Lambda<Func<T, bool>>(equalityExpression, searchExpression.Parameters); 

       // the passed-in expression must resemble v => v.Id 
       // the generated expression will resemble v => v.Id == 5 

       var value = thisTable.SingleOrDefault(lambdaExpression); 
       return descriptionExpression.Compile()(value); 
      } 

      return string.Empty; 
     } 
    } 
관련 문제