简体   繁体   English

C# 在构造函数中从 REST 客户端初始化 class 属性

[英]C# Initialize class properties from REST client inside constructor

I've searched a lot and I think this is possible, but I feel like I am just blocked from knowing how to properly format it.我搜索了很多,我认为这是可能的,但我觉得我只是不知道如何正确格式化它。

I have a class representing a product that is a relation class from our CRM to Magento.我有一个 class 代表一个产品,它是从我们的 CRM 到 Magento 的关系 class。

Inside the constructor, I have to do some stuff like this...在构造函数内部,我必须做一些这样的事情......

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;

    }
}

It seems silly to have to initialize all of the properties like that.必须像这样初始化所有属性似乎很愚蠢。 I could use a mapper but that seems like a bandaid.我可以使用映射器,但这似乎是一个创可贴。 I have to see if the product exists in magento first, and populate its ID and values, otherwise whenever I save the product it creates an additional one.我必须首先查看该产品是否存在于 magento 中,并填充其 ID 和值,否则每当我保存产品时,它都会创建一个额外的产品。

I considered doing the class constructor calling a static method, but I couldn't get the syntax right.我考虑过调用 static 方法的 class 构造函数,但我无法获得正确的语法。

It might just be too late and I need to think about something else for awhile.可能为时已晚,我需要考虑一下其他事情。

If you must do it in the constructor, you can get rid of a lot of code by first setting 'default' values to the 'Product' properties.如果您必须在构造函数中执行此操作,则可以通过首先将“默认”值设置为“产品”属性来摆脱大量代码。 This will remove the need to do them in the constructor.这将消除在构造函数中执行它们的需要。 Next, if you wanted to automatically set the class's properties, you can use reflection.接下来,如果你想自动设置类的属性,你可以使用反射。

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

Though, I would like to point out that you probably shouldn't be using a REST client inside a class constructor, especially to just populate its data (also, you are performing a synchronous operation).不过,我想指出您可能不应该在 class 构造函数中使用 REST 客户端,尤其是仅填充其数据(另外,您正在执行同步操作)。 It would be cleaner to have another layer that is responsible for populating this class using the client, and then use something like AutoMapper to map the data to it.使用客户端负责填充此 class 的另一层会更干净,然后使用 AutoMapper 之类的东西将 map 数据发送给它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 构造函数是在 C# 中的类中初始化不可为空属性的唯一方法吗? - Is constructor the only way to initialize non-nullable properties in a class in C#? C#从列表中删除具有特定构造函数属性的类对象 - c# deleting class object with specific constructor properties from a list 自动属性无法在C#实例构造函数中初始化 - Automatic properties fail to initialize in C# instance constructor 类构造函数(来自C#Web服务)不会在C#MVC中自动实现属性 - Class constructor (from C# web service) won't auto-implement properties in C# MVC C#如何初始化或将默认构造函数值设置为从类注入的依赖项 - c# how to initialize or set a default constructor value to a injected dependency from class 在C#的子类构造函数中初始化基类的字段 - Initialize base class’s fields in subclass constructor in C# 在构造函数中初始化类列表,在C#中返回溢出异常 - Initialize a list of class in constructor returns overflow exception in c# C# 属性、构造函数、 - C# properties, constructor, 在C#中,如何从派生类构造函数访问派生类的属性? - In C# how do I access the properties of a derived class from the derived class constructor? C#构造函数与初始化 - c# constructor vs initialize
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM