在.Net中用另一个相同类型的对象更新对象的最佳方式

本文关键字:对象 更新 最佳 方式 同类型 Net 另一个 | 更新日期: 2023-09-27 18:29:57

有人问过类似的问题,但找不到有效的解决方案。我有一个相当大的对象(Viper对象),它有几个子对象和列表。我每秒会收到几次Viper对象的新实例,我需要将这些数据合并到一个"主"Viper对象中。如果属性为null,如果不替换,请跳过它。一些列表属性将被新属性替换,一些列表属性只会将新列表附加到现有列表。

重点是,有些属性需要一些自定义逻辑来确定如何执行合并。问题是,我应该使用AutoMapper/Value Injector之类的东西,还是只在数据模型中编写一个自定义Merge命令,以获取另一个Viper对象?这需要尽可能高效,因为它会被调用很多。

以下是数据模型:

public class Viper
{
    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
    #endregion
    [DataMember(IsRequired = false, EmitDefaultValue = false)]
    public int ViperId { get; set; }
    [DataMember]
    public int CaseId { get; set; }
    [DataMember]
    public int RecordCount { get; set; }
    [DataMember(IsRequired = false, EmitDefaultValue = false)]
    public string SerialNumber { get; set; }
    [DataMember(IsRequired = false, EmitDefaultValue = false)]
    public string UserName { get; set; }
    [DataMember(IsRequired = false, EmitDefaultValue = false)]
    public string GroupName { get; set; }
    [DataMember]
    public Byte Status { get; set; }
    [DataMember]
    public int CaseStage { get; set; }
    [DataMember(IsRequired = false,EmitDefaultValue = false)]
    public CaseDetail CaseDetail { get; set; }
    private ViperMetaData _metaData;
    [DataMember(IsRequired = false, EmitDefaultValue = false)]
    public ViperMetaData MetaData {
        get { return _metaData; }
        set
        {
            _metaData = value;
            OnPropertyChanged("MetaData");
        }
    }
    [DataMember(IsRequired = false, EmitDefaultValue = false)]
    public List<CasePatientData> PatientDetailsTable1 { get; set; }
    [DataMember(IsRequired = false, EmitDefaultValue = false)]
    public List<CasePatientData> PatientDetailsTable2 { get; set; }
    [DataMember(IsRequired = false, EmitDefaultValue = false)]
    public List<CasePatientData> PatientDetailsTable3 { get; set; }
    [DataMember(IsRequired = false, EmitDefaultValue = false)]
    public List<CasePatientData> PatientDetailsTable4 { get; set; }
    [DataMember(IsRequired = false, EmitDefaultValue = false)]
    public List<CaseChannel> CaseChannelDataList { get; set; } // list of all channels available in a specific case/viper
    [DataMember(IsRequired = false, EmitDefaultValue = false)]
    public List<EventsData> EventsDataList { get; set; }

    [DataMember(IsRequired = false, EmitDefaultValue = false)]
    public List<ChannelData> ChannelDataList { get; set; }
    [DataMember(IsRequired = false, EmitDefaultValue = false)]
    public CasePatientDetails CasePatientDetails { get; set; }
    [DataMember(IsRequired = false, EmitDefaultValue = false)]
    public List<CaseFluids> CaseFluidsList { get; set; }
    [DataMember(IsRequired = false, EmitDefaultValue = false)]
    public List<PatientTable> PatientTableNames { get; set; }
    [DataMember]
    public int? TimeOffset { get; set; }
    [DataMember(IsRequired = false,EmitDefaultValue = false)]
    public string Html { get; set; }
    [DataMember(IsRequired = false, EmitDefaultValue = false)]
    public List<CaseDocument> CaseDocuments { get; set; }
    [DataMember(IsRequired = false, EmitDefaultValue = false)]
    public Department Department { get; set; }
    [DataMember(IsRequired = false, EmitDefaultValue = false)]
    public List<Gauge> Gauges { get; set; }
    [DataMember(IsRequired = false, EmitDefaultValue = false)]
    public List<Region> Regions { get; set; }
    [DataMember(IsRequired = false, EmitDefaultValue = false)]
    public List<BloodGasData> BloodGasDataList { get; set; }
    [DataMember(IsRequired = false, EmitDefaultValue = false)]
    public List<CardioplegiaData> CardioplegiaDataList { get; set; }
    [DataMember(IsRequired = false, EmitDefaultValue = false)]
    public List<ComplianceDataUnit> ComplianceDataList { get; set; }
}

在.Net中用另一个相同类型的对象更新对象的最佳方式

我会使用您提到的自动映射器或值注入器。特别是如果您的属性名称在对象之间相似,则几乎不需要编写任何代码。对于自定义映射,编写自定义函数以传递给映射方法是非常容易的。非常有用。快速模拟代码的样子:

AutoMapper.Mapper.CreateMap<Viper, ViperDto>()
     .ForMember(dest => dest.Property, opt => opt.NullSubstitute("N/A"));
var merged = AutoMapper.Mapper.Map(viper, viperDTO);