GThree
GThree

Reputation: 3562

Use common class in Web API request C#

I am trying to create a common class which I can use in my request as a common class. In below request/payload I have "name", "sku", "quantity", "unit_price" and "total_price" as repetitive fields.

When I declare them directly (as shown in option-1), it works fine. Now I am trying to optimize the code (Option - 2) but in that case, I get null. I am learning RESTful web API calls and curious about this one.

Questions:

  1. Is it possible the way I am trying to achieve this?
  2. If yes, what is missing here?

Request Payload:

"item": 
{
    "type": "Combo",
    "name": "The King Double Egg King Menu",
    "sku": null,
    "quantity": 1,
    "unit_price": 1890,
    "total_price": 2030,
    "sub_items": [
        {
            "type": "Product",
            "name": "The King Double Egg",
            "sku": null,
            "quantity": 1,
            "unit_price": 0,
            "total_price": 140,
            "modifiers": [
                {
                  "type": "Extra",
                  "name": "Bacon",
                  "sku": null,
                  "quantity": 1,
                  "unit_price": 100,
                  "total_price": 100
                }]
        }]
}

Option - 1 (This Works):

[DataContract]
public class Item
{
    [DataMember(Name = "type")]
    [JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
    public OrderItemType Type { get; set; }

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

    [DataMember(Name = "sku")]
    public string Sku { get; set; }

    [DataMember(Name = "quantity")]
    public int? Quantity { get; set; }

    [DataMember(Name = "unit_price")]
    public int UnitPrice { get; set; }

    [DataMember(Name = "total_price")]
    public int TotalPrice { get; set; }

    [DataMember(Name = "sub_items")]
    public List<Item> SubItems { get; set; }

    [DataMember(Name = "modifiers")]
    public List<OrderItemModifier> Modifiers { get; set; }
}

[DataContract]
public class OrderItemModifier
{
    [DataMember(Name = "type")]
    [JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
    public OrderModifierType Type { get; set; }

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

    [DataMember(Name = "sku")]
    public string Sku { get; set; }

    [DataMember(Name = "quantity")]
    public int? Quantity { get; set; }

    [DataMember(Name = "unit_price")]
    public int UnitPrice { get; set; }

    [DataMember(Name = "total_price")]
    public int TotalPrice { get; set; }
}

Option - 2 (Trying to make a class for common objects but it doesn't work):

[DataContract]
public class Item
{
    [DataMember(Name = "type")]
    [JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
    public OrderItemType Type { get; set; }

    public Details Details { get; set; }

    [DataMember(Name = "sub_items")]
    public List<Item> SubItems { get; set; }
}

[DataContract]
public class OrderItemModifier
{
    [DataMember(Name = "type")]
    [JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
    public OrderModifierType Type { get; set; }

    public Details ModifierDetails { get; set; }
}

[DataContract]
public class Details
{
    [DataMember(Name = "name")]
    public string Name { get; set; }

    [DataMember(Name = "sku")]
    public string Sku { get; set; }

    [DataMember(Name = "quantity")]
    public int? Quantity { get; set; }

    [DataMember(Name = "unit_price")]
    public int UnitPrice { get; set; }

    [DataMember(Name = "total_price")]
    public int TotalPrice { get; set; }
}

Upvotes: 0

Views: 1402

Answers (1)

GHN
GHN

Reputation: 75

The second example would match a different JSON than you showed here. It would match the following JSON instead:

"item":
{
    "type": "Combo",
    "details":
    {
        "name": "The King Double Egg King Menu",
        "sku": null,
        "quantity": 1,
        "unit_price": 1890,
        "total_price": 2030
    },
    "sub_items": [
    {
            "type": "Product",
            "modifierdetails":
            {
                "name": "The King Double Egg",
                "sku": null,
                "quantity": 1,
                "unit_price": 0,
                "total_price": 140 
            },
            "modifiers": [
            {
                  "type": "Extra",
                  "name": "Bacon",
                  "sku": null,
                  "quantity": 1,
                  "unit_price": 100,
                  "total_price": 100
                }]
        }]
}

So you are adding an extra property details, and modifierdetails.

A better option would be to use intheritance, like:

[DataContract]
public class Details
{
    [DataMember(Name = "name")]
    public string Name { get; set; }

    [DataMember(Name = "sku")]
    public string Sku { get; set; }

    [DataMember(Name = "quantity")]
    public int? Quantity { get; set; }

    [DataMember(Name = "unit_price")]
    public int UnitPrice { get; set; }

    [DataMember(Name = "total_price")]
    public int TotalPrice { get; set; }
}

[DataContract]
public class Item : Details
{
    [DataMember(Name = "type")]
    [JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
    public OrderItemType Type { get; set; }

    [DataMember(Name = "sub_items")]
    public List<Item> SubItems { get; set; }
}

[DataContract]
public class OrderItemModifier : Details
{
    [DataMember(Name = "type")]
    [JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
    public OrderModifierType Type { get; set; }
}

Upvotes: 1

Related Questions