Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自动映射-将上层属性作为参数传递给下层映射_C#_Automapper - Fatal编程技术网

C# 自动映射-将上层属性作为参数传递给下层映射

C# 自动映射-将上层属性作为参数传递给下层映射,c#,automapper,C#,Automapper,我希望能够访问注释映射部分中的ShipmentIdentifier属性,以便能够成功运行下面的assert语句。下面是我的映射器的简化版本。映射权重单位时,如何传递ShipmentIdentifier属性 using AutoMapper; using System.Collections.Generic; using Xunit; namespace ConsoleApp1 { class Program { static void Main(string[] args) {

我希望能够访问注释映射部分中的ShipmentIdentifier属性,以便能够成功运行下面的assert语句。下面是我的映射器的简化版本。映射权重单位时,如何传递ShipmentIdentifier属性

using AutoMapper;
using System.Collections.Generic;
using Xunit;

namespace ConsoleApp1
{
class Program
{
    static void Main(string[] args)
    {
        IMapper mapper = new MapperConfiguration(cfg =>
        {
            cfg.AddProfile(typeof(PrintLabelCommandMappingProfile));
        }).CreateMapper();

        CarrierLabelPrintedMessage message = new CarrierLabelPrintedMessage
        {
            ShipmentIdentifier = "AB1234",
            Cartons = new List<Carton>
            {
                new Carton
                {
                    TrackingNumber = "TestTrackingNumber",
                    LabelData =
                        new LabelData
                        {
                            Weight = 5,
                            WeightUnit = "none"
                        }
                }
            }
        };

        var command = mapper.Map<PrintLabelCommand>(message);
        Assert.Equal(command.ShipmentUnits[0].Weight.Unit, WeightUnit.None);
    }

    public class PrintLabelCommandMappingProfile : Profile
    {
        public PrintLabelCommandMappingProfile()
        {
            CreateMap<CarrierLabelPrintedMessage, PrintLabelCommand>()
                .ForMember(cmd => cmd.ShipmentId, exp => exp.MapFrom(msg => msg.ShipmentIdentifier))
                .ForMember(cmd => cmd.ShipmentUnits, exp => exp.MapFrom(msg => msg.Cartons));

            CreateMap<Carton, ShipmentUnit>()
                .ForMember(su => su.Id, exp => exp.MapFrom(c => c.TrackingNumber))
                .ForPath(su => su.Weight.Amount, exp => exp.MapFrom(c => c.LabelData.Weight))
                .ForPath(su => su.Weight.Unit, exp => exp.MapFrom(c => c.LabelData.WeightUnit));

            CreateMap<string, WeightUnit>().ConvertUsing((str, _) =>
            {
                // I want to have ShipmentIdentifier field to be accessible in the upper level to implement the mapping logic here as below
                //if (ShipmentIdentifier.StartsWith("AB")) // it's "AB1234" in the sample
                //  return WeightUnit.None;
                //else
                    return WeightUnit.Grams;
            });
        }
    }

    public class CarrierLabelPrintedMessage
    {
        public string ShipmentIdentifier { get; set; }
        public List<Carton> Cartons { get; set; }
    }

    public class PrintLabelCommand
    {
        public string ShipmentId { get; set; }
        public List<ShipmentUnit> ShipmentUnits { get; set; }
    }

    public class Carton
    {
        public string TrackingNumber { get; set; }
        public LabelData LabelData { get; set; }
    }

    public class LabelData
    {
        public double Weight { get; set; }
        public string WeightUnit { get; set; }
    }

    public class ShipmentUnit
    {
        public string Id { get; set; }
        public Weight Weight { get; set; }
    }

    public class Weight
    {
        public double Amount { get; set; }
        public WeightUnit Unit { get; set; }
    }

    public enum WeightUnit
    { 
        None,
        Grams,
        Kilograms
    }
}
}
使用AutoMapper;
使用System.Collections.Generic;
使用Xunit;
名称空间控制台EAPP1
{
班级计划
{
静态void Main(字符串[]参数)
{
IMapper映射器=新的映射配置(cfg=>
{
AddProfile(typeof(PrintLabelCommandMappingProfile));
}).CreateMapper();
CarrierLabelPrintedMessage=新的CarrierLabelPrintedMessage
{
ShipmentIdentifier=“AB1234”,
纸箱=新清单
{
新纸箱
{
TrackingNumber=“TestTrackingNumber”,
唇形目=
新唇形目
{
重量=5,
WeightUnit=“无”
}
}
}
};
var命令=mapper.Map(消息);
Assert.Equal(command.ShipmentUnits[0].Weight.Unit、weighunit.None);
}
公共类PrintLabelCommandMappingProfile:Profile
{
公共PrintLabelCommandMappingProfile()
{
CreateMap()
.ForMember(cmd=>cmd.ShipmentId,exp=>exp.MapFrom(msg=>msg.ShipmentIdentifier))
.ForMember(cmd=>cmd.ShipmentUnits,exp=>exp.MapFrom(msg=>msg.Cartons));
CreateMap()
.ForMember(su=>su.Id,exp=>exp.MapFrom(c=>c.TrackingNumber))
.ForPath(su=>su.Weight.Amount,exp=>exp.MapFrom(c=>c.LabelData.Weight))
.ForPath(su=>su.Weight.Unit,exp=>exp.MapFrom(c=>c.LabelData.WeightUnit));
CreateMap().ConvertUsing((str,))=>
{
//我想让ShipmentIdentifier字段在上层可以访问,以实现下面的映射逻辑
//if(ShipmentIdentifier.StartsWith(“AB”)//示例中为“AB1234”
//返回权重单位。无;
//否则
返回重量单位:克;
});
}
}
公共类载体标记打印消息
{
公共字符串ShipmentIdentifier{get;set;}
公共列表纸箱{get;set;}
}
公共类PrintLabelCommand
{
公共字符串ShipmentId{get;set;}
公共列表ShipmentUnits{get;set;}
}
公营纸箱
{
公共字符串跟踪编号{get;set;}
公共LabelData LabelData{get;set;}
}
公共类LabelData
{
公共双权重{get;set;}
公共字符串权重单元{get;set;}
}
公营船务组
{
公共字符串Id{get;set;}
公共权重{get;set;}
}
公共类权重
{
公共双倍金额{get;set;}
公共权重单位{get;set;}
}
公共枚举单元
{ 
没有一个
克,
公斤
}
}
}
有什么想法吗?

您可以使用
CreateMap()
映射根据
CarrierLabelPrintedMessage
中的可用信息“修复”映射。映射可能如下所示:

CreateMap<CarrierLabelPrintedMessage, PrintLabelCommand>()
    .ForMember(cmd => cmd.ShipmentId, exp => exp.MapFrom(msg => msg.ShipmentIdentifier))
    .ForMember(cmd => cmd.ShipmentUnits, exp => exp.MapFrom(msg => msg.Cartons))
    .AfterMap((src, target) => {
        WeightUnit targetValue = WeightUnit.Grams;
        if (src.ShipmentIdentifier.StartsWith("AB"))
        {
            targetValue = WeightUnit.None;
        }
        foreach (ShipmentUnit shipmentUnit in target.ShipmentUnits)
        {
            shipmentUnit.Weight.Unit = targetValue;
        }
    });
CreateMap()
.ForMember(cmd=>cmd.ShipmentId,exp=>exp.MapFrom(msg=>msg.ShipmentIdentifier))
.ForMember(cmd=>cmd.ShipmentUnits,exp=>exp.MapFrom(msg=>msg.Cartons))
.AfterMap((src,目标)=>{
WeightUnit targetValue=重量单位克;
if(src.ShipmentIdentifier.StartsWith(“AB”))
{
targetValue=权重单位。无;
}
foreach(目标中的ShipmentUnit ShipmentUnit.ShipmentUnits)
{
shipmentUnit.Weight.Unit=目标值;
}
});

请将您的问题包括作为一个完整的源代码,其他人可以编译和测试。@Progman我添加了整个类的详细信息,并且可以编译,这是您所要求的吗?@anilca请提供一些入口点,如
Main()
方法或单元测试,显示您拥有的输入值和您想要的映射结果。还显示如何从AutoMapper调用
Map()
方法。@Progman完成,谢谢