自定义复杂类型的AutoMapper

时间:2013-02-22 12:56:18

标签: c# automapper automapper-2

我有两种复杂类型:一种是服务层,它用作ViewModel,另一种用于存储库层。 它们的定义如下:

    //The Repository Layer 
public class ProductDetailsEntity
        {
            public Int64 StockNumber { get; set; }

            public String StockName { get; set; }

            public String Image { get; set; }

            public Decimal Price { get; set; }

            public String JewelleryName { get; set; }

            public String ShortDescription { get; set; }

            public Int64 ShippingDays { get; set; }

            public String DesignCode { get; set; }

            public List<SettingDetails> SettingsDetails { get; set; }

            public List<SideStoneDetails> SideStoneDetails { get; set; }
        }

 // The Service Layer 
public class ProductDetailsModel
    {
        public Int64 StockNumber { get; set; }

        public String StockName { get; set; }

        public String Image { get; set; }

        public Decimal Price { get; set; }

        public String JewelleryName { get; set; }

        public String ShortDescription { get; set; }

        public Int64 ShippingDays { get; set; }

        public String DesignCode { get; set; }

        public List<SettingDetailsModel> SettingsDetails { get; set; }

        public List<SideStoneDetailsModel> SideStoneDetails { get; set; }
    }

将SettingsDetailsModel和SettingDetails设为:

public class SettingDetails // same Structure with different Names
    {
        public Int64 AttributeId { get; set; }

        public String AttributeName { get; set; }

        public String AttributeValue { get; set; }

    }

SideStoneDetailsModel和SideStoneDetails:

public class SideStoneDetailsModel
    {
        public Int64 SideStoneSettingId { get; set; }

        public String SideStoneSettingName { get; set; }

        public String SideStoneSettingValue { get; set; }
    }

现在,在从实体映射到模型时, 它抛出一个AutoMapper异常声明:

The following property on Repository.Entities.SettingDetails cannot be mapped: 
SettingsDetails
Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type Service.Models.SettingDetailsModel.
Context:
Mapping to property SettingsDetails of type Repository.Entities.SettingDetails from source type Service.Models.SettingDetailsModel
Mapping to property SettingsDetails of type System.Collections.Generic.List`1[[Repository.Entities.SettingDetails, Repository, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] from source type System.Collections.Generic.List`1[[Service.Models.SettingDetailsModel, Service, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
Mapping to type Repository.Entities.ProductDetailsEntity from source type Service.Models.ProductDetailsModel
Exception of type 'AutoMapper.AutoMapperConfigurationException' was thrown.

现在,Mapper实现包含

Mapper.CreateMap<SettingDetails, SettingDetailsModel>();
Mapper.CreateMap<SideStoneDetails, SideStoneDetailsModel>();
Mapper.CreateMap<ProductDetailsModel, ProductDetailsEntity>();
Mapper.AssertConfigurationIsValid();

基本上它没有自定义类型的列表。我不明白哪里出错了: 现在提升我发现的是:

  • 为不同类型添加单独映射。检查!
  • 自定义映射器功能 - 但为什么?在这种情况下,我无法理解为什么要这样做?

我该如何解决这个问题?我想从REPOSITORY实体映射到我的VIEWMODEL

1 个答案:

答案 0 :(得分:1)

你的意思是这句话:

Mapper.CreateMap<ProductDetailsModel, ProductDetailsEntity>();

或者您想要反过来创建地图吗?

Mapper.CreateMap<ProductDetailsEntity, ProductDetailsModel>();

我不确定你想要映射到哪个方向,但如果确实你想要前者,那么你将不得不将SettingDetailsModel的地图定义回{{1} },即:

SettingDetails