2011-06-13 50 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