以通用方式更新实体属性

时间:2016-10-27 16:56:04

标签: c# .net automapper

我有以下内容:

 public class Car : ITransportRelatedEntity
 {
    public void UpdateFrom(ITransportRelatedEntity otherEntity)
    {
        var typedEntity = otherEntity as Car; // From HERE      *
        if (typedEntity == null)              //                *
        {                                     //                *
            throw new ArgumentException(nameof(otherEntity));// *
        }                                     //                *
                                              //                *
        Name = typedEntity.Name;              //                *
        Make = typedEntity.Make;              //                *
        Model= typedEntity.Model;             // To HERE        *
    }

 }

 public class Plane : ITransportRelatedEntity
 {
    public void UpdateFrom(ITransportRelatedEntity otherEntity)
    {
        var typedEntity = otherEntity as Plane; // From HERE      *
        if (typedEntity == null)                //                *
        {                                       //                *
            throw new ArgumentException(nameof(otherEntity));  // *
        }                                       //                *
                                                //                *
        Name = typedEntity.Name;                //                *
        Brand = typedEntity.Brand;              //                *
        Passengers = typedEntity.Passengers;    // To HERE        *
    }

 }

public interface ITransportRelatedEntity
{
    void UpdateFrom(ITransportRelatedEntity otherEntity);
}

我觉得所有这一切都可以被抽象出来,但我不知道如何。我应该以某种方式试图强制ITransportRelatedEntity检测调用类型并相应地自动推断/映射属性?如果是这样,我该怎么做?

0 个答案:

没有答案