簡體   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