簡體   English   中英

將繼承對象列表的類映射到類似的類

[英]Map a class which inherits a list of objects to a similar class

在我的項目中,我需要將對象從外部系統映射到DTO。 我要映射的對象是:

public class PriceLists : List<PriceList> { }

我有在類中映射屬性的想法,但是很難找到這種情況的解決方案。 我的DTO最好與此源類“相同”,以使其目前盡可能簡單:

public class PriceListsDTO : List<PriceListDTO> { }

有沒有簡單的解決方案,或者我需要重構我的DTO對象?

謝謝。

編輯:我已經嘗試為價目表列表創建映射,但未成功解決此問題。

Mapper.Initialize(cfg => { cfg.CreateMap<PriceList>, <PriceListDTO>(); });

Mapper.Initialize(cfg => { cfg.CreateMap<IList<PriceList>, IList<PriceListDTO>>(); });

編輯2:

public class PriceList
{

    public string Agreement { get; set; }

    public Currency Currency { get; set; }

    public string Description { get; set; }

    public Nullable<DateTime> EndDate { get; set; }

    public int Id { get; set; }

    public Nullable<Guid> ImageKey { get; set; }

    public bool IsBid { get; set; }

    public bool IsLimitedToStock { get; set; }

    public bool IsPrimary { get; set; }

    public bool IsPublic { get; set; }

    public string Name { get; set; }

    public Nullable<DateTime> StartDate { get; set; }

    public int Type { get; set; }
}

public class PriceListDTO
{
    public string Agreement { get; set; }

    public CurrencyViewModel Currency { get; set; }

    public string Description { get; set; }

    public DateTime? EndDate { get; set; }

    public int Id { get; set; }

    public Guid? ImageKey { get; set; }

    public bool IsBid { get; set; }

    public bool IsLimitedToStock { get; set; }

    public bool IsPrimary { get; set; }

    public bool IsPublic { get; set; }

    public string Name { get; set; }

    public DateTime? StartDate { get; set; }

    public int Type { get; set; }
}

而且Currency類和DTO僅包含字符串屬性。

從給出的代碼中,您實際上從未告訴過AutoMapper將DTO與模型類相關聯。 如果您調用兩次Initialize ,則第二個將刪除以前的所有映射。 嘗試更新您的配置以執行以下操作:

Mapper.Initialize( cfg => {
    cfg.CreateMap<PriceList, PriceListDTO>()
       .ReverseMap();
    // Not sure if this is required if you already have the model/dto map 
    cfg.CreateMap<IList<PriceList>, IList<PriceListDTO>>();
    cfg.AssertConfigurationIsValid();
});
public class PriceList

{

public string Agreement { get; set; }

public Currency Currency { get; set; }

public string Description { get; set; }

public Nullable<DateTime> EndDate { get; set; }

public int Id { get; set; }

public Nullable<Guid> ImageKey { get; set; }

public bool IsBid { get; set; }

public bool IsLimitedToStock { get; set; }

public bool IsPrimary { get; set; }

public bool IsPublic { get; set; }

public string Name { get; set; }

public Nullable<DateTime> StartDate { get; set; }

public int Type { get; set; }

}

公共類PriceListDTO {公共字符串協議{get; 組; }

public Currency Currency { get; set; }

public string Description { get; set; }

public DateTime? EndDate { get; set; }

public int Id { get; set; }

public Guid? ImageKey { get; set; }

public bool IsBid { get; set; }

public bool IsLimitedToStock { get; set; }

public bool IsPrimary { get; set; }

public bool IsPublic { get; set; }

public string Name { get; set; }

public DateTime? StartDate { get; set; }

public int Type { get; set; }

}

之后,嘗試automapper.mapper.createmap它將為您工作,否則您需要使用formember方法一個一個地映射具有currencyviewmodel的貨幣屬性,因為對象彼此不同,只需嘗試一下即可。 希望對你有幫助。 謝謝

暫無
暫無

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

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