可靠服务数据模型问题

Reliable Service Data Model Issue

我是 Azure Service Fabric 的新手,看过 Ivan Gavryliuk 的 "Understanding the Programming Models of Azure Service Fabric" 关于 Pluralsight 的课程。我一直在关注可靠服务中的基本数据模型,API 按照课程中的说明工作。

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

Product.cs 来自 ECommerce.ProductCatelog.Model

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; }

   }
}

ApiProduct.cs 来自 ECommerce.API.Model

   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; }

   }

ProductController.cs 来自 Ecommerce.API.Controlers

[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
         });
      }

上面块中的最后一行触发了智能感知错误: "Cannot implicitly convert type 'ECommerce.ProductCatelog.Model.Supplier' to 'ECommerce.API.Model.Supplier'"

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

干杯, 亚当

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

这一行:

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

您获得了 ECommerce.ProductCatalog.Model.Product 类型的项目集合。在此 class 中,您添加了类型为 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,但您设置this 属性 为原始值,不进行转换。所以,把原来的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 设为集合

使您的 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
           }    
     });