Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#从构造函数内的REST客户端初始化类属性_C#_Rest_Constructor_Instantiation - Fatal编程技术网

C#从构造函数内的REST客户端初始化类属性

C#从构造函数内的REST客户端初始化类属性,c#,rest,constructor,instantiation,C#,Rest,Constructor,Instantiation,我已经搜索了很多,我认为这是可能的,但我觉得我只是不知道如何正确格式化它 我有一个表示产品的类,它是从CRM到Magento的关系类 在构造函数中,我必须做一些像这样的事情 public Product(IBaseProduct netforumProduct, MagentoClient client) { Product existingMagentoProduct = client.GetProductBySku(netforumProduct.Code); if (ex

我已经搜索了很多,我认为这是可能的,但我觉得我只是不知道如何正确格式化它

我有一个表示产品的类,它是从CRM到Magento的关系类

在构造函数中,我必须做一些像这样的事情

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;

    }
}
公共产品(IBaseProduct netforumProduct、MagentoClient)
{
Product existingMagentoProduct=client.GetProductBySku(netforumProduct.Code);
if(existingMagentoProduct!=null)
{
this.id=existingMagentoProduct.id;
this.name=existingMagentoProduct.name;
…他们中的许多人。。。
this.visibility=existingMagentoProduct.visibility;
this.extension\u attributes.configurable\u product\u links=现有的magentoproduct.extension\u attributes.configurable\u product\u links;
}
其他的
{
//这是一个新产品,新的对象
这个.id=-1;
this.product_links=新列表();
this.options=新列表();
this.custom_attributes=新列表();
this.media_gallery_entries=新列表();
this.extension_attributes=新的ExtensionAttributes();
this.status=0;//禁用所有新产品,以便将其添加到站点并在特定日期发布(这是一项功能,不是问题)。
this.attribute\u set\u id=netforumProduct.attributestatid;
这是0.0;
}
}
像这样初始化所有属性似乎很愚蠢。我可以用绘图机,但那看起来像绷带。我必须先查看产品是否存在于magento中,并填充其ID和值,否则每当我保存产品时,它都会创建一个附加的ID和值

我考虑过使用类构造函数调用静态方法,但语法不正确


这可能太晚了,我需要考虑一下其他问题。

如果必须在构造函数中执行此操作,可以通过首先将“默认”值设置为“产品”属性来删除大量代码。这将消除在构造函数中执行这些操作的需要。接下来,如果要自动设置类的属性,可以使用反射

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);
            }
        }
    }   
}
公共类产品
{
公共int Id{get;set;}=-1;
公共列表产品链接{get;set;}=new List();
....
公共int可见性{get;set;}=0;
公共产品(IBaseProduct netforumProduct、MagentoClient客户端)
{
var existingMagentoProduct=client.GetProductBySku(netforumProduct.Code);
if(existingMagentoProduct!=null)
{
foreach(PropertyInfo属性在typeof(Product).GetProperties()中,其中(p=>p.CanWrite))
{
SetValue(this,property.GetValue(existingMagentoProduct,null),null);
}
}
}   
}

不过,我想指出的是,您可能不应该在类构造函数中使用REST客户机,尤其是在填充其数据时(另外,您正在执行同步操作)。如果有另一个层负责使用客户机填充此类,然后使用AutoMapper之类的工具将数据映射到该类,则会更干净。

有什么问题?您是否不想初始化构造函数中的值,或者您正在寻找一种更干净的方法来进行初始化?我正在寻找一种更干净的方法。在一个本身就是一种类型的对象中初始化30个属性似乎很荒谬。Automapper有它的优点。我很感激对属性做一个setvalue,因为对象本身就是一种类型,应该没问题。谢谢你的提醒。