2011-06-13 54 views
0

它是如何開始的?擴展(繼承)實體框架類(不使用部分)

我想添加兩列,不在業務對象集合到radGridView。特別是NewUrl和NewIdOnFilehost。 :)

那麼我試圖做什麼?

我要把它放到網格

radGridViewReuploadStatus.ItemsSource = FileHostings.Filesonic.getdAllDeletedLinks(); 

然後我加入他們的新列

<telerik:GridViewColumn Header="New F.H.Id" UniqueName="NewFilehostId" Width="*"></telerik:GridViewColumn> 
       <telerik:GridViewColumn Header="New URL" UniqueName="NewUrl" Width="*"></telerik:GridViewColumn> 

那麼,什麼是問題呢?

radGridViewReuploadStatus.Rows不存在。 我不知道他們爲什麼沒有將它添加到wpf radGridView,它是在它的aspx版本。我能夠使用getChildsOfType獲取行,但這顯然不是理想的方式。

接下來我做了什麼?

class dlExtended : DownloadLink { 
     public string NewUrl { get; set; } 
     public string NewIdOnFilehost { get; set; } 
    } 

最後的問題 - 什麼基本我不明白

如何使dlExtended從DownloadLink? (我知道這是錯誤的名稱慣例,它只是例如:)) 我如何使dlExtended從收集的DownloadLink名單?使用foreach必須有更好的方法!

現在我可能做錯了

所以現在我應該根據一個傳入DownloadLink通過做構造並設置dlExneded的每個屬性?

嗯,也許它是可行的反射像這樣

public DownloadLinkExtended(DownloadLink origDl){ 
     PropertyInfo[] myObjectProperties = origDl.GetType().GetProperties(); //BindingFlags.Public | BindingFlags.NonPublic 
     foreach (PropertyInfo pi in myObjectProperties) 
     { 
      if (pi.GetValue(origDl, null) != null) 
      { 
       pi.SetValue(this, pi.GetValue(origDl, null), null); 
      } 
     } 
    } 

嗯,這是愚蠢的。 那麼我沒有得到關於擴展類和添加新的屬性?

我知道,EF4類是部分的,我可以添加屬性給他們只是通過部分類,但我想這些只爲網格,而不是其他地方。

+0

的一個可能的解決方案配方: 如何繼承類,所以與父類的參數實例構造函數設置的所有繼承屬性? – evlo 2011-06-13 20:57:28

回答

0

我的「淺」對象複製器與您的非常相似,但空測試略有不同。它也有一個方便的擴展方法包裝 - 因此它需要處於靜態類中。

/// <summary> 
    /// Copy an object to destination object, only matching fields will be copied 
    /// </summary> 
    /// <typeparam name="T"></typeparam> 
    /// <param name="sourceObject">An object with matching fields of the destination object</param> 
    /// <param name="destObject">Destination object, must already be created</param> 
    public static void ShallowCopyTo<T>(this object sourceObject, ref T destObject) 
    { 
     Copy<T>(sourceObject,ref destObject); 
    } 
    /// <summary> 
    /// Copy an object to destination object, only matching fields will be copied 
    /// </summary> 
    /// <typeparam name="T"></typeparam> 
    /// <param name="sourceObject">An object with matching fields of the destination object</param> 
    /// <param name="destObject">Destination object, must already be created</param> 
    public static void Copy<T>(object sourceObject, ref T destObject) 
    { 
     // If either the source, or destination is null, return 
     if (sourceObject == null || destObject == null) 
      return; 

     // Get the type of each object 
     Type sourceType = sourceObject.GetType(); 
     Type targetType = destObject.GetType(); 

     // Loop through the source properties 
     foreach (PropertyInfo p in sourceType.GetProperties()) 
     { 
      // Get the matching property in the destination object 
      PropertyInfo targetObj = targetType.GetProperty(p.Name); 
      // If there is none, skip 
      if (targetObj == null) 
       continue; 

      // Set the value in the destination 
      targetObj.SetValue(destObject, p.GetValue(sourceObject, null), null); 
     } 
    } 

不過,我也有很深的複印機,但這只是與序列化對象的作品,所以看你,從EDMX使用的代碼生成,我不認爲它會與EF類直接工作,但與POCO生成的類有關。

/// <summary> 
/// Reference Article http://www.codeproject.com/KB/tips/SerializedObjectCloner.aspx 
/// 
/// Provides a method for performing a deep copy of an object. 
/// Binary Serialization is used to perform the copy. 
/// </summary> 

public static class ObjectCopier 
{ 
    /// <summary> 
    /// Perform a deep Copy of the object. 
    /// </summary> 
    /// <typeparam name="T">The type of object being copied.</typeparam> 
    /// <param name="source">The object instance to copy.</param> 
    /// <returns>The copied object.</returns> 
    public static T Clone<T>(this T source) 
    { 
     if (!typeof(T).IsSerializable) 
     { 
      throw new ArgumentException("The type must be serializable.", "source"); 
     } 

     // Don't serialize a null object, simply return the default for that object 
     if (Object.ReferenceEquals(source, null)) 
     { 
      return default(T); 
     } 

     IFormatter formatter = new BinaryFormatter(); 
     Stream stream = new MemoryStream(); 
     using (stream) 
     { 
      formatter.Serialize(stream, source); 
      stream.Seek(0, SeekOrigin.Begin); 
      return (T)formatter.Deserialize(stream); 
     } 
    } 
0

如果這些是EF類(不是來自T4模板的POCO),那麼您可能不希望從它們繼承 - 因爲您最終得到EF行李。除此之外,我認爲有一些潛在的解決方案。

如果只在一個地方使用,您可以在LINQ的提高上與投影循環

var newthings = oldlinks.Select(old => new dlExtended{ NewUrl =old.NewUrl , NewIdOnFilehost =old.NewIdOnFilehost }); 

你也可以寫dlExtended一個構造函數一個DownloadLink,然後做

var newthings = oldlinks.Select(old => new dlExtended(old)); 

它把財產複製在一個地方。

您還可以構建一個通用擴展方法,以便以各種方式在兩個對象之間複製具有相同名稱的屬性。

+0

謝謝你的回答。 你的拳頭方法很好,但我想在多於一個地方使用這個新班級。 Uith你的第二個方法,我仍然需要指定一個像 每個屬性 \t \t \t \t \t this.property1 = orig.property1; this.property2 = orig.property2; this.property3 = orig.property3; \t \t \t \t \t 是對的嗎?這正是我不想要的。 「在兩個對象之間複製具有相同名稱的屬性的擴展方法」實際上是我遇到的問題,因爲我的嘗試使用不處理其他EF類的屬性。 – evlo 2011-06-15 07:48:39

+0

無論如何,快速谷歌後,我認爲你提到的POCO,更具體地說,這個http://visualstudiogallery.msdn.microsoft.com/23df0450-5677-4926-96cc-173d02752313,並使用它作爲MVVM中ModelView的東西可以是溶劑我我正在尋找,但是很好 - 我第一次聽說poco,所以需要時間研究:/我還想知道如何正確反思。 – evlo 2011-06-15 07:48:53

+0

我認爲它很少會將EF課程傳遞到視圖中。它的工作原理,所有的例子都是這樣做的,但實際上幾乎所有真實世界的場景都需要一個POCO Viewmodel - 一個代表視圖所需數據的類。數據庫更改時保持這些同步是一個問題,但實例化大數字的性能是另一個問題。重新複製,我會發布另一個答案與我的對象複製代碼只是在它幫助。 – Andiih 2011-06-15 08:41:06