Automapper 将 Dictionary<string, string> 和 List<string> 属性映射到视图模型

Automapper map Dictionary<string, string> and List<string> properties to view model

我有以下视图模型

public class PlanDetail
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string Description { get; set; }

        [DisplayFormat(DataFormatString = "{0:$#.##}")]
        public decimal Price { get; set; }

        public string FrequencyAbbreviatedName { get; set; }

        [Display(Name = "Frequency")]
        public string FrequencyName { get; set; }


        [Display(Name = "Events")]
        public int EventLimit { get; set; }

        [Display(Name = "Help Center Access")]
        public bool HelpCenterAccess { get; set; }

        [Display(Name = "Email Support")]
        public bool EmailSupport { get; set; }

        [Display(Name = "Priority Email Support")]
        public bool PriorityEmailSupport { get; set; }

        [Display(Name = "Phone Support")]
        public bool PhoneSupport { get; set; }

        public bool Active { get; set; }

        public string PictureUrl { get; set; }

        public bool BestValue { get; set; }
    }

我正在使用 stripe.com products and prices.

在我的映射配置文件 class 中,我能够映射到基本属性(例如 ID、名称、描述、活动)。

Mapper.Map();

我不确定如何将条带产品对象中的元数据 属性 (Dictionary<string,string>) 或图像 属性 (List <string>) 映射到某些PlanDetail 属性。

我在我的种子 class 中创建了条带产品,并向元数据和图像属性添加了值。

  public static async Task SeedStripeAsync(string stripeKey)
        {
            StripeConfiguration.ApiKey = stripeKey;

            var productService = new ProductService();
            var priceService = new PriceService();

            var products = await productService.ListAsync();

            var productsData = System.IO.File.ReadAllText("../Infrastructure/Data/SeedData/stripe_products.json");

            var productPlans = JsonSerializer.Deserialize<List<StripeProductSeed>>(productsData);

            foreach (var item in productPlans)
            {
                if (!products.Any(x=> x.Name.Equals(item.Name, StringComparison.InvariantCultureIgnoreCase)))
                {
                    var productOptions = new ProductCreateOptions
                    {
                        Name = item.Name,
                        Description = item.Description,
                        Active = item.Active,
                        Images = new List<string>(),
                        Metadata = new Dictionary<string, string>()
                    };

                    productOptions.Images.Add(item.PictureUrl);
                    productOptions.Metadata.Add("EventLimit", item.EventLimit.ToString());
                    productOptions.Metadata.Add("HelpCenterAccess", item.HelpCenterAccess.ToString());
                    productOptions.Metadata.Add("EmailSupport", item.EmailSupport.ToString());
                    productOptions.Metadata.Add("PriorityEmailSupport", item.PriorityEmailSupport.ToString());
                    productOptions.Metadata.Add("PhoneSupport", item.PhoneSupport.ToString());
                    productOptions.Metadata.Add("BestValue", item.BestValue.ToString());

                    var newProduct = await productService.CreateAsync(productOptions);

                    var priceOptions = new PriceCreateOptions
                    {
                        UnitAmountDecimal = item.Price,
                        Currency = "usd",
                        Recurring = new PriceRecurringOptions()
                        {
                            Interval = item.Interval,
                            IntervalCount = (long)item.IntervalCount
                        },
                        Product = newProduct.Id
                    };

                    await priceService.CreateAsync(priceOptions);

                }
            }
        }

我想将条带产品元数据属性(例如 EventLimit、HelpCenterAccess、EmailSupport、PriorityEmailSupport、PhoneSupport 和 BestValue)映射到它们在 PlanDetail 视图模型中的对应项。

此外,我想将条带产品图像 属性 映射到 PlanDetail 视图模型中的 PictureUrl 属性。

任何关于如何为这些属性使用自动映射器的想法或建议将不胜感激。

这是我的假设

// Just demo class
public class StripeProductSeed
    {
        public string PictureUrl { get; set; }
        public int EventLimit { get; set; }
        public bool HelpCenterAccess { get; set; }
        public bool EmailSupport { get; set; }
        public bool PriorityEmailSupport { get; set; }
        public bool PhoneSupport { get; set; }
        public bool BestValue { get; set; }

        public List<string> ExtractImages() => new() { PictureUrl };

        public Dictionary<string, string> ExtractMetaData() => new()
            {
                {nameof(EventLimit), EventLimit.ToString()},
                {nameof(HelpCenterAccess), HelpCenterAccess.ToString()},
                {nameof(EmailSupport), EmailSupport.ToString()},
                {nameof(PriorityEmailSupport), PriorityEmailSupport.ToString()},
                {nameof(PhoneSupport), PhoneSupport.ToString()},
                {nameof(BestValue), BestValue.ToString()}
            };
    }

地图应该是:

public class MappingProfile : Profile
    {
        public MappingProfile()
        {
            CreateMap<StripeProductSeed, PlanDetail>()
                .ForMember(dst => dst.Images, x => x.MapFrom(src => src.ExtractImages()))
                .ForMember(dst => dst.Metadata, x => x.MapFrom(src => src.ExtractMetaData()));
        }
    }