2016-05-23 41 views
2

除了像Automapper這樣的第三方映射器應用程序,將數據從一個對象複製到另一個對象具有有限屬性(不完全是克隆任務)的最佳方法是什麼? 。將數據從一個對象複製到另一個具有有限屬性的對象

Customer 
{ 
    string Name { get; set; } 
    string SSN { get; set; } 
    object Addresses { get; set; } 
} 

to 

CustomerData 
{ 
    string Name { get; set; } 
    object Addresses { get; set; } 
} 

例如,我想複製CustomerCustomerData(Addressses對象可能是嵌套的對象,並且對象可以具有多個屬性)。當然,這是爲了演示目的而縮短的。 Customer中有更多的字段,我不想複製到CustomerData。

+2

定義「最佳」。最快的?最少的代碼?最靈活?你顯然可以明確地複製屬性,但我認爲你需要不同的東西。 –

+0

最低代碼,最快分別爲 – Rod

+2

我認爲你正在尋找一個特殊的演員操作員,或者甚至是一個通用的擴展程序,爲你做到這一點。看看[這個]的答案之一(http://stackoverflow.com/questions/3672742/cast-class-into-another-class-or-convert-class-to-another)問題。 –

回答

1

有一個ToOtherType方法是很短的customer.ToCustomerData(),應該是快速的(相比反思奇怪)。

//ToOtherType method 
public CustomerData ToCustomerData(){ 
    var customerData = new CustomerData(); 
    customerData.Name = Name; 
    customerData.Addresses = Addresses; 
    return customerData; 
} 

使用明示或暗示的運營商,您可以編寫更短代碼:CustomerData cd = customer(隱含的)或CustomerData cd = (CustomerData)customer(明確)。但是,要小心隱式運算符,它們可以創建有趣的調試頭痛。

//explicit operator 
public static explicit operator CustomerData(Customer c){ 
    var customerData = new CustomerData(); 
    customerData.Name = c.Name; 
    customerData.Addresses = c.Addresses; 
    return customerData; 
} 

我會去與ToOtherType模式,這是比較明顯的是怎麼回事了一點,特別是如果你有任何人對誰也不會承認它的代碼工作。另外,操作員模式在某些情況下不起作用(密封對象等)。

在線例如:https://dotnetfiddle.net/56p0Lh

+1

借調靜態顯式運算符。它使代碼簡潔,但不會混淆太多的內容。它還具有將映射代碼保存在一個地方的好處,所以當您修改映射對象時,您不必遍歷代碼庫以尋找要更新的內容。 – DVK

0

哪些屬性複製和忽視是非常主觀。它必須是一個定製的任務。如果我要做到這一點,你所描述的情況下,我會做到以下幾點:

public interface ICustomCloning{ 
    void CopyFrom(ICustomCloning other); 
} 

class Customer: ICustomCloning 
{ 
    string Name { get; set; } 
    string SSN { get; set; } 
    object Addresses { get; set; } 

    public virtual void CopyFrom(ICustomCloning other){ 
     if(other is Customer) 
     { 
      var o = other as Customer; 
      //copy values from o. 
      //this.Name = o.Name; 
     } 
     //else if(other is Addresses) 
     //this.Addresses.CopyFrom(other); 
    } 
} 
class CustomerData: ICustomCloning 
{ 
    string Name { get; set; } 
    object Addresses { get; set; } 

    public virtual void CopyFrom(ICustomCloning other){ 
     if(other is CustomerDa) 
     { 
      var o = other as CustomerData; 
      //copy values from o. 
      //this.Name = o.Name; 
     } 
     //else if(other is Addresses) 
     //this.Addresses.CopyFrom(other); 
    } 
} 
public class Addresses: ICustomCloning{ 
    public virtual void CopyFrom(ICustomCloning other){ 
     if(other is Addresses){ 
      var o as Addresses; 
      //Do your copy from o. 
     } 
     else if(other is Customer) 
     //var o = (other as Customer). Addresses; 
     //Do your copy from o 
    } 
} 
-1

@Chakrava曾與static explicit operator正確的答案,因爲這些讓你像對待轉換內置的轉換(如。CustomerData data = (CustomerData)myCustomer;)。如果你不喜歡這樣做的想法,一些其他的選擇將是(除了寫ToSomeType()方法):

構造函數重載

public CustomerData 
{ 
    public CustomerData(ICustomer customer) 
    { 
     // put your mapping code here 
    } 
} 

var data = new CustomerData(myCustomer); 

擴展方法

類似以ToSomeType()方法...

public static CustomerData ToCustomerData(this Customer customer) 
{ 
    // mapping code here 

    return myCustomerData; 
} 

繼承

CustomerData 
{ 
    string Name { get; set; } 
    object Addresses { get; set; } 
} 

Customer : CustomerData 
{ 
    string SSN { get; set; } 
} 

反思

,所以最好使用自動映射器或類似。很多使用反射的例子,所以我不會進入它。

對象初始化語法

不打折此,可以是一次性的(認爲YAGNI)或補充一些本文所描述的其他機制。儘管如此,不要亂丟你的代碼。

var data = new CustomerData(){ Name = myCustomer.Name, Addresses = myCustomer.Addresses}; 
+0

如果你想downvote一個有效的答案,你可以解釋爲什麼。 – DVK

相關問題