2016-05-23 68 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

相关问题