已经提出了类似的问题,但无法找到效果很好的解决方案。 我所拥有的是一个相当大的对象(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; }
}
答案 0 :(得分:0)
我会像你提到的那样使用Auto Mapper或Value Injector。特别是如果您的属性名称与对象类似,则几乎不需要编写任何代码。对于自定义映射,编写自定义函数以传递给映射方法非常容易。很有用。快速伪造的代码看起来像:
AutoMapper.Mapper.CreateMap<Viper, ViperDto>()
.ForMember(dest => dest.Property, opt => opt.NullSubstitute("N/A"));
var merged = AutoMapper.Mapper.Map(viper, viperDTO);