ITypeConverter <pizzavm,pizza =“”>。转换(PizzaVm,Pizza,ResolutionContext)

时间:2016-08-02 09:13:32

标签: c# asp.net-web-api2 automapper

using System.Linq;
using AutoMapper;
using ShopDx3.DomainModels;
using ShopDx3.DomainModels.Enums;
using ShopDx3.SharedKernel;
using ShopDx3.ViewModels;

namespace ShopDx3.Api.Factories
{
    public class OrderVmToOrderDmConverter : ITypeConverter<OrderVm, Order>
    {
        public Order Convert(ResolutionContext context)
        {
            var src = (OrderVm)context.SourceValue;
            var servType = Enumeration.FromDisplayName<ServiceType>(src.ServiceType.Replace(" ", ""));
            var pizzas = src.Pizzas.Select(x => Mapper.Map<PizzaVm, Pizza>(x)).ToList();
            var result = new Order(servType, pizzas, src.Name);
            return result;
        }
    }
}

当我构建时,我得到了错误消息

  

错误CS0535&#39; PizzaVmToPizzaDmConverter&#39;没有实现接口成员&#39; ITypeConverter.Convert(PizzaVm,   比萨,   ResolutionContext)&#39; ShopDx3.Api D:\ Projects \ ShopDx3 \ src \ ShopDx3.Api \ Factories \ PizzaVmToPizzaDmConverter.cs

现在我正在尝试

using System.Linq;
using AutoMapper;
using ShopDx3.DomainModels;
using ShopDx3.DomainModels.Enums;
using ShopDx3.SharedKernel;
using ShopDx3.ViewModels;

namespace ShopDx3.Api.Factories
{
    public class OrderVmToOrderDmConverter : ITypeConverter<OrderVm, Order>
    {
        public Order Convert(OrderVm pvm, Order p, ResolutionContext context)
        {
            var src = (OrderVm)context.SourceValue;
            var servType = Enumeration.FromDisplayName<ServiceType>(src.ServiceType.Replace(" ", ""));
            var pizzas = src.Pizzas.Select(x => Mapper.Map<PizzaVm, Pizza>(x)).ToList();
            var result = new Order(servType, pizzas, src.Name);
            return result;
        }
    }
}

但是我得到了这个代码 Severity Code Description Project File Line Suppression State Error CS1061 'ResolutionContext' does not contain a definition for 'SourceValue' and no extension method 'SourceValue' accepting a first argument of type 'ResolutionContext' could be found (are you missing a using directive or an assembly reference?) ShopDx3.Api D:\Projects\ShopDx3\src\ShopDx3.Api\Factories\OrderVmToOrderDmConverter.cs 14 Active

已更新

我将AutoMapper降级到版本3.3.1它解决了我的问题, 谢谢大家。

2 个答案:

答案 0 :(得分:1)

您的方法Convert只有一个参数。该错误意味着它需要3个参数。

因此...

public Order Convert(ResolutionContext context)
{
   ...
}

应该是:

public Order Convert(PizzaVm pvm, Pizza p, ResolutionContext context)
{
   ...
}

或......应添加具有所需签名的第二种方法。

答案 1 :(得分:1)

AutoMapper 5.0从ResolutionContext中删除了源值属性,转而直接传入值。事实证明,将源值放在ResolutionContext对象中是非常慢的。将它们直接传递到您的类型转换器是允许AutoMapper 5.0比AutoMapper 3.x快20-50倍。