簡體   English   中英

如何對具有多個XmlElementAttributes的對象使用automapper?

[英]How do I use automapper for objects with multiple XmlElementAttributes?

我在從.xsd為.cs文件中生成的對象設置automapper時遇到問題。

當對象具有多個屬性時,不確定如何解決問題,如下所示:

一直在看TypeConverters等,但不確定如何正確設置它。 一直在使用automapper並且沒有任何問題,只要沒有多個屬性連接到一個成員。

public partial class customerInfo {

    private object itemField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("customerInfoBasic", typeof(customerInfoBasic))]
    [System.Xml.Serialization.XmlElementAttribute("customerInfoSimple", typeof(customerInfoSimple))]
    [System.Xml.Serialization.XmlElementAttribute("customerInfoEnhanced", typeof(customerInfoEnhanced))]
    public object Item {
        get {
            return this.itemField;
        }
        set {
            this.itemField = value;
        }
    }
}

public partial class customerInfoBasic{

    private string nameField;

    /// <remarks/>
    public string name {
        get {
            return this.nameField;
        }
        set {
            this.nameField= value;
        }
    }
}

public partial class customerInfoSimple{

    private string nameField;
    private string idField;


    /// <remarks/>
    public string name {
        get {
            return this.nameField;
        }
        set {
            this.nameField= value;
        }
    }

    public string id {
        get {
            return this.idField;
        }
        set {
            this.idField= value;
        }
    }
}

public partial class customerInfoEnhanced{

    private string nameField;
    private string idField;
    private string ageField;

    /// <remarks/>
    public string name {
        get {
            return this.nameField;
        }
        set {
            this.nameField= value;
        }
    }

    public string id {
        get {
            return this.idField;
        }
        set {
            this.idField= value;
        }
    }

    public string age {
        get {
            return this.ageField;
        }
        set {
            this.ageField= value;
        }
    }
}

我遇到的問題是我不知道如何設置它,以便根據“信息”中的某些值正確映射customerInfo。

例如,如果“Info”包含“age”和“id”,則應將其映射到customerInfoEnhanced等。

public static void AddSessionTransformationMappings(IMapperConfiguration cfg)
{
    cfg.AllowNullCollections = true;

    cfg.CreateMap<IEnumerable<Info>, customerInfoList>()
        .ForMember(x => x.customerInfo, x => x.MapFrom(y => y));

    cfg.CreateMap<Info, customerInfo>()
        .ForMember(x => x.Item, x => x.MapFrom(y => y));

    cfg.CreateMap<Info, customerInfoBasic>()
        .ForMember(x => x.Name, x => x.MapFrom(y => y.name));

    cfg.CreateMap<Info, customerInfoSimple>()
        .ForMember(x => x.Name, x => x.MapFrom(y => y.name))
        .ForMember(x => x.Id, x => x.MapFrom(y => y.id));

    cfg.CreateMap<Info, customerInfoEnhanced>()
        .ForMember(x => x.Name, x => x.MapFrom(y => y))
        .ForMember(x => x.Id, x => x.MapFrom(y => y.id))
        .ForMember(x => x.Age, x => x.MapFrom(y => y.age));
}

這是序列化器的代碼:

var output = provider.Transform(new List<Info> { input });

customerInfoList actual = null;
XmlSerializer serializer = new XmlSerializer(typeof(customerInfoList));
using (MemoryStream ms = new MemoryStream())
{
    serializer.Serialize(ms, output);
    ms.Position = 0;
    actual = (customerInfoList)serializer.Deserialize(ms);
}

如果我設置.ForMember(x => x.customerInfo, x => x.MapFrom(y => (Object)null)); 代碼工作,“實際”給我一個item = null的列表,所以我知道問題是在customerInfo中映射“Item”。

我希望映射器映射到正確的類,現在我得到Missing類型映射或“Info is not expected。使用XmlInclude或SoapInclude屬性指定靜態未知的類型。

真的很感激如何解決這個問題的一些指示!

解決它是為了我自己的需要,如果其他人遇到同樣的問題解決方案。

對我來說,解決方案是使用ResolveUsing而不是MapFrom來獲取具有多個標記名稱的特定屬性,並使用Mapper.Map以及針對不同情況的正確類。

完整代碼如下所示:

public static void AddSessionTransformationMappings(IMapperConfiguration cfg)
{
    cfg.AllowNullCollections = true;

    cfg.CreateMap<IEnumerable<Info>, customerInfoList>()
        .ForMember(x => x.customerInfo, x => x.MapFrom(y => y));

    cfg.CreateMap<Info, customerInfo>()
        .ForMember(x => x.Item, x => x.ResolveUsing(y => CustomerInfoLevel(y)));

    cfg.CreateMap<Info, customerInfoBasic>()
        .ForMember(x => x.Name, x => x.MapFrom(y => y.name));

    cfg.CreateMap<Info, customerInfoSimple>()
        .ForMember(x => x.Name, x => x.MapFrom(y => y.name))
        .ForMember(x => x.Id, x => x.MapFrom(y => y.id));

    cfg.CreateMap<Info, customerInfoEnhanced>()
        .ForMember(x => x.Name, x => x.MapFrom(y => y.name))
        .ForMember(x => x.Id, x => x.MapFrom(y => y.id))
        .ForMember(x => x.Age, x => x.MapFrom(y => y.age));
}

private static Object CustomerInfoLevel(Info info)
{
    if (info.age != null)
    {
        return Mapper.Map<customerInfoEnhanced>(info);
    }
    else if (info.id != null)
    {
        return Mapper.Map<customerInfoSimple>(info);
    }
    else
    {
        return Mapper.Map<customerInfoBasic>(info);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM