[英]Extending (inherit) entity framwork class (not using partial)
它是如何開始的?
我想將不在業務對象集合中的兩列添加到 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; }
}
最后的問題 - 我不明白什么基本
如何從 DownloadLink 制作 dlExtended? (我知道這是錯誤的命名約定,只是舉例:))我如何從 DownloadLink 的集合中創建 dlExtended 列表? 一定有比使用 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);
}
}
}
好吧,這很愚蠢。 那么我對擴展 class 並為其添加新屬性沒有得到什么?
我知道 EF4 類是部分的,我可以簡單地通過部分 class 向它們添加屬性,但我只希望這些用於網格而不是其他任何地方。
如果這些是 EF 類(不是 T4 模板中的 POCO),那么您可能無論如何都不想從它們繼承 - 因為您最終會得到 EF 包袱。 除此之外,我認為還有許多潛在的解決方案。
如果僅在一個地方使用,您可以在 Linq 中使用投影改進 for 循環
var newthings = oldlinks.Select(old => new dlExtended{ NewUrl =old.NewUrl , NewIdOnFilehost =old.NewIdOnFilehost });
您還可以為 dlExtended 編寫一個構造函數,該構造函數接受一個 DownloadLink 然后執行
var newthings = oldlinks.Select(old => new dlExtended(old));
它將屬性復制放在一個地方。
您還可以構建一個通用擴展方法來在兩個對象之間復制具有相同名稱的屬性,並以多種方式使用它。
我的“淺”object 復印機與您的非常相似,但 null 測試略有不同。 它還有一個方便的擴展方法包裝器——因此它需要位於 static class 中。
/// <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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.