Warm tip: This article is reproduced from serverfault.com, please click

c#-具有根属性的AutoMapper第三级子集合映射

(c# - AutoMapper third level subcollection map with root properties)

发布于 2020-11-28 06:41:27

我正在尝试将源子集合映射到有效的目标子集合。但是我还需要在子集合上映射一些基本属性。我尝试了IncludeMembers,但没有运气。我想找到一些将两种类型映射为一种的示例,但这似乎不适用于我的情况,因为当我的所有嵌套在同一对象中时,大多数示例都将两种不同类型的实例转换为一种。

我准备了一个单元测试来显示我的问题:

对象

public class FlatProduct
{
    public string Sku { get; set; }
    public int BrandId { get; set; }
    public int CategoryId { get; set; }
    public int ProductVersionId { get; set; }
    public int PriceLevelId { get; set; }
    public decimal Price { get; set; }
    public decimal RebatePrice { get; set; }
    public List<FlatProductInventory> FlatProductInventory { get; set; }


}

public class FlatProductInventory
{

    public int StockedLocationId { get; set; }
    public int StockedQty { get; set; }


}

public class Product
{
    public string Sku { get; set; }
    public int BrandId { get; set; }
    public int CategoryId { get; set; }
    public ProductVersion ProductVersion { get; set; }

}

public class ProductVersion
{

    public string Sku { get; set; }
    public int BrandId { get; set; }
    public int CategoryId { get; set; }
    public int ProductVersionId { get; set; }
    public ProductPrice ProductPrice { get; set; }
    public List<ProductInventory> ProductInventory { get; set; }

}

public class ProductPrice
{

    public string Sku { get; set; }
    public int BrandId { get; set; }
    public int CategoryId { get; set; }
    public int ProductVersionId { get; set; }
    public int PriceLevelId { get; set; }
    public decimal Price { get; set; }
    public decimal RebatePrice { get; set; }

}
public class ProductInventory
{

    public string Sku { get; set; }
    public int BrandId { get; set; }
    public int CategoryId { get; set; }
    public int ProductVersionId { get; set; }
    public int StockedLocationId { get; set; }
    public int StockedQty { get; set; }

}

映射/测试

public static void TestMapper()
    {
        var config = new MapperConfiguration(cfg => {
            cfg.CreateMap<FlatProduct, ProductPrice>();
            cfg.CreateMap<FlatProduct, ProductVersion>();
            cfg.CreateMap<FlatProduct, ProductInventory>();
            cfg.CreateMap<FlatProductInventory, ProductInventory>();
            cfg.CreateMap<FlatProduct, ProductVersion>()
                .ForMember(productVersion => productVersion.ProductPrice, flatProduct => flatProduct.MapFrom(fp => fp))
                .ForMember(productVersion => productVersion.ProductInventory, flatProduct => flatProduct.MapFrom(fp => fp))
                .ForMember(productVersion => productVersion.ProductInventory, flatProduct => flatProduct.MapFrom(fp => fp.FlatProductInventory));
            cfg.CreateMap<FlatProduct, Product>()
                .ForMember(product => product.ProductVersion, flatProduct => flatProduct.MapFrom(fp => fp));


        });

        IMapper mapper = config.CreateMapper();


        var flatProductExample = new FlatProduct
        {
            Sku = "ABC123",
            BrandId = 1,
            CategoryId = 1,
            ProductVersionId = 1,
            PriceLevelId = 1,
            Price = 12.99m,
            RebatePrice = 9.99m,
            FlatProductInventory = new List<FlatProductInventory>()
        {
            new FlatProductInventory()
            {
                StockedLocationId = 1,
                StockedQty = 33
            }
        }
        };

        var mappedProduct = mapper.Map<Product>(flatProductExample);

        Assert.Equal(flatProductExample.Sku, mappedProduct.ProductVersion.ProductInventory[0].Sku); //Fail
        Assert.Equal(flatProductExample.FlatProductInventory[0].StockedQty, mappedProduct.ProductVersion.ProductInventory[0].StockedQty); //Pass

    }

编辑

Xerillio确实接近我需要的答案,实际上他的答案适用于automapper低于8的版本。就我而言,我使用的是最新版本,因此我不得不对其代码进行一些修改才能使其正常工作。这里是:

var config = new MapperConfiguration(cfg => {
            cfg.CreateMap<FlatProduct, ProductPrice>();
            cfg.CreateMap<FlatProduct, ProductVersion>();
            cfg.CreateMap<FlatProduct, ProductInventory>();
            cfg.CreateMap<FlatProductInventory, ProductInventory>();
            cfg.CreateMap<FlatProduct, ProductVersion>()
                .ForMember(productVersion => productVersion.ProductPrice, flatProduct => flatProduct.MapFrom(fp => fp))
                .ForMember(productVersion => productVersion.ProductInventory, opt => opt.MapFrom((flatProduct, productVersion, i, context) => {
                    return flatProduct.FlatProductInventory

                        // Map using '<FlatProductInventory, ProductInventory>'
                        .Select(flatPInv => context.Mapper.Map<FlatProductInventory, ProductInventory>(flatPInv))
                        // Map (in-place by passing in the destination object)
                        // using '<FlatProduct, ProductInventory>'
                        .Select(destPInv => context.Mapper.Map<FlatProduct, ProductInventory>(flatProduct, destPInv))
                        // Now you've combined the mappings from FlatProduct and FlatProductInventory
                        .ToList();
                }));


            cfg.CreateMap<FlatProduct, Product>()
            .ForMember(product => product.ProductVersion, flatProduct => flatProduct.MapFrom(fp => fp));
                
            

        });
Questioner
user3907939
Viewed
11
Xerillio 2020-11-28 19:11:00

我的理解是,每一个FlatProductInventory你想要拥有一个ProductInventory但是,ProductInventory还需要填充来自的属性FlatProduct

你实际上已经在“将两种类型映射到一种”上了正确的轨道,因此解决该问题的一种方法是:

var config = new MapperConfiguration(cfg => {
    cfg.CreateMap<FlatProduct, ProductPrice>();
    cfg.CreateMap<FlatProduct, ProductVersion>();
    cfg.CreateMap<FlatProduct, ProductInventory>();
    cfg.CreateMap<FlatProductInventory, ProductInventory>();
    cfg.CreateMap<FlatProduct, ProductVersion>()
        .ForMember(productVersion => productVersion.ProductPrice, flatProduct => flatProduct.MapFrom(fp => fp))
        .ForMember(productVersion => productVersion.ProductInventory,
            opt => opt.ResolveUsing((flatProduct, productVersion, i, context) => {
                return flatProduct.FlatProductInventory
                    // Map using '<FlatProductInventory, ProductInventory>'
                    .Select(flatPInv => context.Mapper.Map<FlatProductInventory, ProductInventory>(flatPInv))
                    // Map (in-place by passing in the destination object)
                    // using '<FlatProduct, ProductInventory>'
                    .Select(destPInv => context.Mapper.Map<FlatProduct, ProductInventory>(flatProduct, destPInv))
                    // Now you've combined the mappings from FlatProduct and FlatProductInventory
                    .ToList();
            })
        );
    cfg.CreateMap<FlatProduct, Product>()
        .ForMember(product => product.ProductVersion, flatProduct => flatProduct.MapFrom(fp => fp));
});

(可能有语法错误,因为我没有通过IDE运行它。)