繁体   English   中英

AutoMapper转换器“无参数构造函数”错误

[英]AutoMapper converter 'parameterless constructor' error

我想做单元测试检查自动映射器工作正常。 创建的地图

CreateMap<List<BaseProd.Product>, List<TL.StockQuantity>>()
            .ConvertUsing<ProductStockQuantityConverter>();

转换器代码:

public class ProductStockQuantityConverter : ITypeConverter<List<BaseProd.Product>, List<TL.StockQuantity>>
{
    private readonly IMapper mapper;
    private readonly ProductService productService;        

    public ProductStockQuantityConverter(IMapper mapper, ProductService productService)
    {
        this.mapper = mapper;
        this.productService = productService;
    }

    public List<TL.StockQuantity> Convert(List<BaseProd.Product> source, List<TL.StockQuantity> destination, ResolutionContext context)
    {
        if (source == null)
            throw new ArgumentNullException(nameof(source));

        destination = new List<TL.StockQuantity>();

        foreach (var item in source)
        {
            destination.Add(new TL.StockQuantity()
            {
                ProductOriginalId = item.ErplId,
                Quantity = productService.GetQuantity(item.Id, ignorePresale: true).Quantity
            });
        }

        return destination;
    }
}

我的单元测试看起来像

[Fact]
    public void TestB2BStockQuantityEqual()
    {
        List<BaseProd.Product> prodList = new List<BaseProd.Product>();
        List<TL.StockQuantity> stockQuantityList = new List<TL.StockQuantity>();

        BaseProd.Product firstProductItem = new BaseProd.Product()
        {
            ErplId = ...
            Quantity = ...
        };

        BaseProd.Product secondProductItem = new BaseProd.Product()
        {
            ErplId = ...
            Quantity = ...
        };

        TL.StockQuantity firstStockQuantityItem = new TL.StockQuantity()
        {
            ProductOriginalId = ...
            Quantity = ...
        };

        TL.StockQuantity secondStockQuantityItem = new TL.StockQuantity()
        {
            ProductOriginalId = ...
            Quantity = ...
        };

        prodList.Add(firstProductItem);
        prodList.Add(secondProductItem);

        stockQuantityList.Add(firstStockQuantityItem);
        stockQuantityList.Add(secondStockQuantityItem);

        List<TL.StockQuantity> expected = mapper.Map<List<TL.StockQuantity>>(prodList);
        Assert.Equal(expected, stockQuantityList);
    }

和等于方法

public partial class StockQuantity : IEquatable<StockQuantity>
{
    public bool Equals(StockQuantity other)
    {
        bool equals =
            int.Equals(this.ProductOriginalId, other.ProductOriginalId) &&
            decimal.Equals(this.Quantity, other.Quantity);

        return equals;
    }
}

现在的问题是错误“无参数构造函数”

我无法在转换器中执行无参数构造函数,即使我这样做(在我从db获取回购的另一个示例中进行了尝试)我也收到一个错误,回购为null。 我不知道该怎么做

编辑

第二部分类代码:

public partial class StockQuantity
{
    public int ProductOriginalId { get; set; }
    public decimal Quantity { get; set; }
}

我在每个Converter中都遇到了这个问题,如果我要创建地图并使用

.ForMember(...)

可以,但是使用转换器会失败

错误详情

在此处输入图片说明

编辑

我的BaseAutomapperTest类代码

public abstract class BaseAutomapperTest
{
    public virtual bool IsConfigurationValid()
    {
        try
        {
            Mapper.AssertConfigurationIsValid();
            return true;
        }
        catch
        {
            return false;
        }
    }
}

public abstract class BaseAutomapperTest<TProfile> : BaseAutomapperTest where TProfile : Profile, new()
{
    protected MapperConfiguration config;
    protected IMapper mapper;

    public override bool IsConfigurationValid()
    {
        try
        {
            config.AssertConfigurationIsValid();
            return true;
        }
        catch
        {
            return false;
        }
    }

    public BaseAutomapperTest()
    {
        config = new MapperConfiguration(c => c.AddProfile<TProfile>());
        mapper = new Mapper(config);

        //config.AssertConfigurationIsValid<TProfile>();

    }
}

从头开始进行此测试

public class AutoMapperTests : BaseAutomapperTest<AutoMapperProfile>
{
    public AutoMapperTests()
        : base()
    {

    }

...

    [Fact]
    public void TestB2BStockQuantityEqual()
    {
        List<BaseProd.Product> prodList = new List<BaseProd.Product>();
        List<TL.StockQuantity> stockQuantityList = new List<TL.StockQuantity>();

        BaseProd.Product firstProductItem = new BaseProd.Product()
        {
            ErplId = 1,
            Quantity = new[] { new ProductWarehouseQuantity() }
        };

        BaseProd.Product secondProductItem = new BaseProd.Product()
        {
            ErplId = 2,
            Quantity = new[] { new ProductWarehouseQuantity() }
        };

        TL.StockQuantity firstStockQuantityItem = new TL.StockQuantity()
        {
            ProductOriginalId = 1,
            Quantity = 1
        };

        TL.StockQuantity secondStockQuantityItem = new TL.StockQuantity()
        {
            ProductOriginalId = 2,
            Quantity = 1
        };

        prodList.Add(firstProductItem);
        prodList.Add(secondProductItem);

        stockQuantityList.Add(firstStockQuantityItem);
        stockQuantityList.Add(secondStockQuantityItem);

        List<TL.StockQuantity> expected = mapper.Map<List<TL.StockQuantity>>(prodList);
        Assert.Equal(expected, stockQuantityList);
    }

...

}

我想告诉您的是,如果您使用的是Autofac,则可以直接在ProductStockQuantityConveter中解决依赖项

例如

public class ProductStockQuantityConverter
{
    private readonly ProductService productservice = Container.Resolve<ProductService>();
    private readonly IMapper mapper = Container.Resolve<IMapper>();

    public ProductStockQuantityConverter()
    {
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM