C# 在构造函数中从 REST 客户端初始化 class 属性
C# Initialize class properties from REST client inside constructor
我搜索了很多,我认为这是可能的,但我觉得我不知道如何正确格式化它。
我有一个 class 代表一个产品,它是从我们的 CRM 到 Magento 的关系 class。
在构造函数中,我必须做一些这样的事情...
public Product(IBaseProduct netforumProduct, MagentoClient client)
{
Product existingMagentoProduct = client.GetProductBySku(netforumProduct.Code);
if (existingMagentoProduct != null)
{
this.id = existingMagentoProduct.id;
this.name = existingMagentoProduct.name;
... many of them ...
this.visibility = existingMagentoProduct.visibility;
this.extension_attributes.configurable_product_links = existingMagentoProduct.extension_attributes.configurable_product_links;
}
else
{
// its a new product, new up the objects
this.id = -1;
this.product_links = new List<ProductLink>();
this.options = new List<Option>();
this.custom_attributes = new List<CustomAttribute>();
this.media_gallery_entries = new List<MediaGalleryEntry>();
this.extension_attributes = new ExtensionAttributes();
this.status = 0; // Keep all new products disabled so they can be added to the site and released on a specific day (this is a feature, not an issue / problem).
this.attribute_set_id = netforumProduct.AttributeSetId;
this.visibility = 0;
}
}
像这样初始化所有属性似乎很愚蠢。我可以使用映射器,但这看起来像创可贴。我必须先查看该产品是否存在于 magento 中,然后填充其 ID 和值,否则每当我保存该产品时,它都会创建一个额外的产品。
我考虑过 class 构造函数调用静态方法,但语法不正确。
可能来不及了,我需要考虑一下其他事情。
如果您必须在构造函数中执行此操作,您可以通过首先将 'default' 值设置为 'Product' 属性来摆脱大量代码。这将消除在构造函数中执行它们的需要。接下来,如果你想自动设置class的属性,你可以使用反射。
public class Product
{
public int Id { get; set; } = -1;
public List<ProductLink> Product_Links { get; set; } = new List<ProductLink>();
....
public int Visibility { get; set; } = 0;
public Product(IBaseProduct netforumProduct, MagentoClient client)
{
var existingMagentoProduct = client.GetProductBySku(netforumProduct.Code);
if (existingMagentoProduct != null)
{
foreach (PropertyInfo property in typeof(Product).GetProperties().Where(p => p.CanWrite))
{
property.SetValue(this, property.GetValue(existingMagentoProduct, null), null);
}
}
}
}
不过,我想指出,您可能不应该在 class 构造函数中使用 REST 客户端,尤其是只填充其数据(另外,您正在执行同步操作)。让另一层负责使用客户端填充此 class,然后使用类似 AutoMapper 的东西将数据映射到它会更干净。
我搜索了很多,我认为这是可能的,但我觉得我不知道如何正确格式化它。
我有一个 class 代表一个产品,它是从我们的 CRM 到 Magento 的关系 class。
在构造函数中,我必须做一些这样的事情...
public Product(IBaseProduct netforumProduct, MagentoClient client)
{
Product existingMagentoProduct = client.GetProductBySku(netforumProduct.Code);
if (existingMagentoProduct != null)
{
this.id = existingMagentoProduct.id;
this.name = existingMagentoProduct.name;
... many of them ...
this.visibility = existingMagentoProduct.visibility;
this.extension_attributes.configurable_product_links = existingMagentoProduct.extension_attributes.configurable_product_links;
}
else
{
// its a new product, new up the objects
this.id = -1;
this.product_links = new List<ProductLink>();
this.options = new List<Option>();
this.custom_attributes = new List<CustomAttribute>();
this.media_gallery_entries = new List<MediaGalleryEntry>();
this.extension_attributes = new ExtensionAttributes();
this.status = 0; // Keep all new products disabled so they can be added to the site and released on a specific day (this is a feature, not an issue / problem).
this.attribute_set_id = netforumProduct.AttributeSetId;
this.visibility = 0;
}
}
像这样初始化所有属性似乎很愚蠢。我可以使用映射器,但这看起来像创可贴。我必须先查看该产品是否存在于 magento 中,然后填充其 ID 和值,否则每当我保存该产品时,它都会创建一个额外的产品。
我考虑过 class 构造函数调用静态方法,但语法不正确。
可能来不及了,我需要考虑一下其他事情。
如果您必须在构造函数中执行此操作,您可以通过首先将 'default' 值设置为 'Product' 属性来摆脱大量代码。这将消除在构造函数中执行它们的需要。接下来,如果你想自动设置class的属性,你可以使用反射。
public class Product
{
public int Id { get; set; } = -1;
public List<ProductLink> Product_Links { get; set; } = new List<ProductLink>();
....
public int Visibility { get; set; } = 0;
public Product(IBaseProduct netforumProduct, MagentoClient client)
{
var existingMagentoProduct = client.GetProductBySku(netforumProduct.Code);
if (existingMagentoProduct != null)
{
foreach (PropertyInfo property in typeof(Product).GetProperties().Where(p => p.CanWrite))
{
property.SetValue(this, property.GetValue(existingMagentoProduct, null), null);
}
}
}
}
不过,我想指出,您可能不应该在 class 构造函数中使用 REST 客户端,尤其是只填充其数据(另外,您正在执行同步操作)。让另一层负责使用客户端填充此 class,然后使用类似 AutoMapper 的东西将数据映射到它会更干净。