将继承对象列表的类映射到类似的类

时间:2016-08-02 09:04:03

标签: c# automapper

在我的项目中,我需要将对象从外部系统映射到DTO。我要映射的对象是:

public class PriceLists : List<PriceList> { }

我明白了在类中映射属性但很难找到解决此案例的方法。我的DTO最好是相同的&#34;到这个源类,使其尽可能简单:

public class PriceListsDTO : List<PriceListDTO> { }

是否有简单的解决方案,还是需要重构我的DTO对象?

感谢。

编辑:我尝试为价格列表创建映射,但没有成功解决此问题。

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

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

Edit2:

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只包含字符串属性。

2 个答案:

答案 0 :(得分:0)

从您提供的代码中,您实际上从未告诉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();
});

答案 1 :(得分:0)

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的货币属性一个接一个,因为   对象彼此不同只是尝试一下。希望它会有所帮助   为了你 。感谢