【问题标题】:Automapper map Dictionary<string, string> and List<string> properties to view modelAutomapper 将 Dictionary<string, string> 和 List<string> 属性映射到视图模型
【发布时间】:2021-08-15 23:11:07
【问题描述】:

我有以下视图模型

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 productsprices

在我的映射配置文件类中,我能够映射到基本属性(例如 Id、Name、Description、Active)。

Mapper.Map();

我不确定如何将条带产品对象中的 Metadata 属性 (Dictionary&lt;string,string&gt;) 或 Images 属性 (List &lt;string&gt;) 映射到某些 PlanDetail 属性。

我在我的种子类中创建了条带产品,并向 Metadata 和 Image 属性添加了值。

  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 属性。

任何关于如何将 automapper 用于这些属性的想法或建议将不胜感激。

【问题讨论】:

    标签: asp.net-mvc asp.net-core automapper


    【解决方案1】:

    这是我的假设

    // 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()));
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2017-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多