AutoMapper解析器未传递预期类型

时间:2012-06-11 22:44:43

标签: automapper

鉴于以下类和解析器,为什么我会收到此错误?我不明白为什么要传递ProductAddModel。

  

AutoMapper.AutoMapperMappingException未被用户代码
处理   Message =提供的值类型为System.Decimal但预期   AuctionCMS.Framework.Models.Admin.ProductAddModel。更改值   解析器源类型,或重定向提供的源值   使用FromMember的值解析器。

类型:

    public class Currency
    {
        public Int64 Value { get; set; }
        // Spot saved for currency type and any other extra properties
    }

   public class Product
    {
        public Currency Price { get; set; }
        public Currency ReservePrice { get; set; }
          }

    public class ProductAddModel
    {
        public Decimal Price { get; set; }
        public Decimal ReservePrice { get; set; }
    }

解析器代码:

public class DecimalToCurrencyValueResolver : ValueResolver<decimal, Currency>
{
    #region Overrides of ValueResolver<decimal,Currency>

    protected override Currency ResolveCore(decimal source)
    {
        return new Currency() { Value = (Int64)((decimal)source) * 1000 };
    }

    #endregion
}

public class CurrencyToDecimalValueResolver : ValueResolver<Currency, decimal>
{
    #region Overrides of ValueResolver<decimal,Currency>

    protected override decimal ResolveCore(Currency source)
    {
        return (decimal)source.Value * 1000;
    }

映射代码:

            Mapper.CreateMap<ProductAddModel, Product>()
                .ForMember(x => x.Price, opt => opt.ResolveUsing<DecimalToCurrencyValueResolver>())
                .ForMember(x => x.ReservePrice, opt => opt.ResolveUsing<DecimalToCurrencyValueResolver>());
            Mapper.CreateMap<Product, ProductAddModel>()
                .ForMember(x => x.Price, opt => opt.ResolveUsing<CurrencyToDecimalValueResolver>())
                .ForMember(x => x.ReservePrice, opt => opt.ResolveUsing<CurrencyToDecimalValueResolver>());

var model = new ProductAddModel();
var product = new Product();
Mapper.Map<ProductAddModel, Product>(model, product);

我做错了什么,这种方法是在映射过程中处理简单变换的最佳方法吗?

谢谢!

2 个答案:

答案 0 :(得分:3)

改为使用TypeConverters:

    public  class CurrencyToDecimalTypeConverter : ITypeConverter<Currency, Decimal>
    {
        public decimal Convert(ResolutionContext context)
        {
            return ((Currency)context.SourceValue).Value * 1000;
        }
    }

    public class DecimalToCurrencyTypeConverter : ITypeConverter<Decimal, Currency>
    {
        public Currency Convert(ResolutionContext context)
        {
            return new Currency() { Value = (Int64)((decimal)context.SourceValue) * 1000 };
        }
    }

这是配置:

Mapper.CreateMap<ProductAddModel, Product>();
Mapper.CreateMap<Product, ProductAddModel>();
Mapper.CreateMap<Decimal, Currency>().ConvertUsing<DecimalToCurrencyTypeConverter>();
Mapper.CreateMap<Currency, Decimal>().ConvertUsing<CurrencyToDecimalTypeConverter>();

var model = new ProductAddModel();
var product = new Product();
Mapper.Map<ProductAddModel, Product>(model, product);

答案 1 :(得分:0)

在解析器中使用十进制并且在映射中传递productAddModel。所以映射应该是这样的

Mapper.CreateMap<ProductAddModel, Product>()
                .ForMember(x => x.Price, opt => opt.ResolveUsing<DecimalToCurrencyValueResolver>().FromMember(e => e.Value ));