2011-08-18 115 views
1

有沒有一種方法使用實體框架的泛型方法?使用實體框架和ADO.NET模型的泛型類型

例如,我有表格:TblPlanTblTier。我可以通過一個通用的方法來查找類型嗎?每個表都有稍微不同的值,我想爲每個值進行不同的比較。我試過了:

public static Dictionary<T, List<T>> checkDuplicates<T>(T something) 
    { 
     try 
     { 
     //TblCommissionPlan plan = (TblCommissionPlan)something; 
     //var type = typeof(something); 
     } 
    } 

這些沒有工作......任何想法?

+0

當你的意思是檢查不同的值,你試圖檢查什麼值? – Jethro

+0

例如,如果我有'Plan.nm == Plan2.nm' – Cody

+0

所以你想檢查EF生成的計劃的屬性? – Jethro

回答

2

這不是通用方法的目的。如果你寫的方法有簽名:

public Dictionary<T, List<T>> CheckDuplicates<T>(T something) 

你正在定義「共享邏輯」。這意味着相同的邏輯必須適用於傳遞給該方法的每個T。如果沒有,你必須使用某種約束約束的T

public Dictionary<T, List<T>> CheckDuplicates<T>(T something) where T : ISomeInterface 

現在你知道,每一個T傳遞給方法必須實現ISomeInterface,你可以在你的方法使用任何屬性或方法聲明上該界面。

對於不同類型的T,該方法的內容不應該是不同的,但邏輯可能因爲您可以調用可能有不同實現的方法和屬性。如果它還不夠,你可以傳遞另一個參數 - 泛型委託或者其他一些基於T的泛型類,它將爲你增加一些額外的邏輯。

在你的場景中,你想比較不同的傳遞類=>比較不能是你的方法的一部分,但它必須是你的實體的一部分,或者你必須傳遞額外的類/方法來做比較的方法。

直接在你的類實現比較可以實現IComparable<T>接口,並聲明你的方法爲:

public Dictionary<T, List<T>> CheckDuplicates<T>(T something) where T : IComparable<T> 

爲了實現你的類以外的比較,你可以簡單地使用Func<T, T, int>或實施IComparer<T>

public Dictionary<T, List<T>> CheckDuplicates<T>(T something, IComparer<T> comparer) 

在這兩種情況下,我不確定這與實體框架有什麼關係,因爲您的方法的簽名與EF無關。

1

這是我用來顯示EF生成的單個實體的屬性的類。

你可以建立這個比較兩個不同的實體,它應該是一個很好的起點。

namespace Solutions.Data.Entities 
{ 
    using System; 
    using System.Collections.Concurrent; 
    using System.Reflection; 
    using System.Text; 
    using System.Linq; 

    public static class EntityExtensions 
    { 
     #region Fields 

     private static readonly ConcurrentDictionary<string, PropertyInfo[]> PropertyInfoCache = new ConcurrentDictionary<string, PropertyInfo[]>(); 

     #endregion 

     #region Extension Methods 

     /// <summary> 
     /// This method will find all the Properties of Entity and display them in formatted way. 
     /// </summary> 
     /// <typeparam name="T">Entity Type</typeparam> 
     /// <param name="value">Entity value</param> 
     /// <returns>Formatted string of the Entity Properties</returns> 
     public static string PropertiesToString<T>(this T value) where T : IObjectWithChangeTracker 
     { 
      var type = typeof(T).FullName; 
      if (String.IsNullOrEmpty(type)) return String.Empty; 

      CachePropertyInfo<T>(type); 

      StringBuilder stringBuilder = new StringBuilder(); 

      foreach (var propertyInfo in PropertyInfoCache[type]) 
      { 
       stringBuilder.AppendLine(String.Format("{0} : {1}", propertyInfo.Name, propertyInfo.GetValue(value, null))); 
      } 

      return stringBuilder.ToString(); 
     } 

     /// <summary> 
     /// Use reflection to find all propertied if key is not found in list. 
     /// </summary> 
     /// <typeparam name="T">Entity Type</typeparam> 
     /// <param name="type">property fullname</param> 
     private static void CachePropertyInfo<T>(string type) 
     { 
      if (!PropertyInfoCache.ContainsKey(type)) 
      { 
       // Get all public properties of T type where T inherits interface IObjectWithChangeTracker 
       var properties = 
        typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.Name != "ChangeTracker"); 
       PropertyInfoCache[type] = properties.ToArray(); 
      } 
     } 

     #endregion 
    } 
}