【问题标题】:Reliable Service Data Model Issue可靠服务数据模型问题
【发布时间】:2020-08-16 10:40:36
【问题描述】:

我是 Azure Service Fabric 的新手,在 Pluralsight 上观看了 Ivan Gavryliuk 的“了解 Azure Service Fabric 的编程模型”课程。我一直在关注,可靠服务和 API 中的基本数据模型按照课程中的说明工作。

但是,如果我增加所用数据模型的复杂性,我会遇到错误。

来自 ECommerce.ProductCatelog.Model 的 Product.cs

namespace ECommerce.ProductCatalog.Model
{
   public class Product
   {
      public Guid Id { get; set; }

      public string Name { get; set; }

      public string Description { get; set; }

      public double Price { get; set; }

      public int Availability { get; set; }
      public Supplier Suppliers { get; set; }
   }

   public class Supplier
   {
      public Guid Id { get; set; }
      public string Name { get; set; }

   }
}

来自 ECommerce.API.Model 的 ApiProduct.cs

   public class ApiProduct
   {
      [JsonProperty("id")]
      public Guid Id { get; set; }

      [JsonProperty("name")]
      public string Name { get; set; }

      [JsonProperty("description")]
      public string Description { get; set; }

      [JsonProperty("price")]
      public double Price { get; set; }

      [JsonProperty("isAvailable")]
      public bool IsAvailable { get; set; }

      [JsonProperty("suppliers")]
      public ApiSupplier suppliers { get; set; }

   }

   public class ApiSupplier
   {
      [JsonProperty("id")]
      public Guid Id { get; set; }

      [JsonProperty("name")]
      public string Name { get; set; }

   }

来自 Ecommerce.API.Controlers 的 ProductController.cs

[HttpGet]
      public async Task<IEnumerable<ApiProduct>> GetAsync()
      {
         IEnumerable<Product> allProducts = await _service.GetAllProductsAsync();

         return allProducts.Select(p => new ApiProduct
         {
            Id = p.Id,
            Name = p.Name,
            Description = p.Description,
            Price = p.Price,
            IsAvailable = p.Availability > 0,
            suppliers = p.Suppliers
         });
      }

上述块中的最后一行触发了一个智能感知错误: “无法将类型 'ECommerce.ProductCatelog.Model.Supplier' 隐式转换为 'ECommerce.API.Model.Supplier'

有关如何解决此问题的任何建议欢迎 :)

干杯, 亚当

【问题讨论】:

标签: azure-service-fabric


【解决方案1】:

您的问题并非特定于 Service Fabric,而是一般 C#。您正在尝试使用不同类型的值设置变量。

在这一行:

IEnumerable<Product> allProducts = await _service.GetAllProductsAsync();

您将获得ECommerce.ProductCatalog.Model.Product 类型的项目集合。在这个类中,你添加了ECommerce.ProductCatalog.Model.Supplier 类型的属性Suppliers(应该是Supplier,因为它不是一个集合)。

现在,使用以下行:

     return allProducts.Select(p => new ApiProduct
     {
        Id = p.Id,
        Name = p.Name,
        Description = p.Description,
        Price = p.Price,
        IsAvailable = p.Availability > 0,
        suppliers = p.Suppliers
     });

您正在将此集合转换为新类型 ECommerce.API.Model.Product 的集合,您向其中添加了类型为 ECommerce.API.Model.Supplier 的新属性 Suppliers,但您将此属性设置为原始值,而不进行转换。因此,将原来的Suppliers 属性转换为正确的类型:

     return allProducts.Select(p => new ApiProduct
     {
        Id = p.Id,
        Name = p.Name,
        Description = p.Description,
        Price = p.Price,
        IsAvailable = p.Availability > 0,
        suppliers = new ApiSupplier
        {
           Id = p.Suppliers.Id,
           Name = p.Suppliers.Name
        }    
     });

更新:将供应商设为集合

使您的 Suppliers 属性成为您的数据模型和 Api 模型中的集合:

 public Collection<ApiSupplier> suppliers { get; set; }

然后相应地转换集合:

     return allProducts.Select(p => new ApiProduct
     {
        Id = p.Id,
        Name = p.Name,
        Description = p.Description,
        Price = p.Price,
        IsAvailable = p.Availability > 0,
        suppliers = p.Suppliers.Select(s => new ApiSupplier
           {
             Id = s.Id,
             Name = s.Name
           }    
     });

【讨论】:

  • 嗨@Francesc,是的,我对 C# 也很陌生。感谢您的洞察力。您的解释是有道理的,但是我在您的解决方案中看到了同样的错误:无法隐式转换类型:ECommerce.ProductCatalog.Model.Supplier to ECommerce.API.Mode.Supplier。
  • 我使用了错误的类型名称。检查更新的答案。应该是 ApiSupplier
  • 是的,成功了。我需要更多地阅读数据模型方面的内容。此示例似乎将单个供应商映射到产品。映射多个供应商只是将 Product 模型定义为: Collection supply { get;放; } ?您将如何在控制器中反映这一点?顺便说一句,请随意将我指向文档/文章。欣赏洞察力。
  • 我添加了一个关于如何使供应商成为集合的示例。关于文档和文章,我没有链接,但我会从任何通用 C# 教程和 LINQ 开始。
  • 圣人建议。也许到时候我会继续努力。我遇到了一些新问题 :) 所以无论哪种方式,我都在学习新的“东西”,因为我现在只是在 SF 环境中完成典型的 CRUD 活动。感谢您在此问题上的帮助!
猜你喜欢
  • 2013-05-20
  • 2010-12-02
  • 1970-01-01
  • 1970-01-01
  • 2017-10-21
  • 1970-01-01
  • 2017-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多