Home > Software engineering >  How to map two class properties based on Customer
How to map two class properties based on Customer

Time:11-14

I have an application in which I am getting Shopify orders via Shopify webhooks. Now I have an issue is when I have only one client I mapped Shopify to receive an order via the traditional way with object properties and save it into the database.

Now I got two more clients and all clients want to map class fields according to them.

Let's suppose I got a Shopify order object with the following details:

{
        "id": 4655316533326,
        "checkout_id": 29212941516878,
        "user_id": 71894499406,
        "note": "hello world",
        "subtotal_price_set": {
            "shop_money": {
                "amount": "109.00",
                "currency_code": "USD"
            }
        "total_tax": "0.00",
        "total_tax_set": {
            "shop_money": {
                "amount": "0.00",
                "currency_code": "USD"
            },
            "presentment_money": {
                "amount": "0.00",
                "currency_code": "USD"
            }
        }

}

I have a class name Order as well in my application which contains some of the following properties:

public class Order {

    public string OrderNo { get; set; }
    public string DebtorID { get; set; }
    public virtual string DeliveryAddress1 { get; set; }
    public virtual string DeliveryAddress2 { get; set; }
    public virtual string RIRTNo { get; set; }
    public List<OrderLine> Lines { get; set; }
    public string OrderNote { get; set; }
    Public Customer Customer { get; set; }

}

Now some customers want me to map shopify order Note property with mine Order.Customer.Note

and one want to map that Note proerty with Order.Note

same with OrderNo. One wants to map it directly with Order.OrderNo and others want to map it with ``RIRTNo```

How to handle this kind of situation?

I created one table on the database to keep this mapping information.

I can get the customer detail from the Shopify order and then I make the DB query to get map information but the problem is in the database the detail is in dictionary like structure.

Consider this below mapping info for x Customer: the key is a Shopify object property

"Order": {
"note": "Customer.Note"
"Id": "OrderId"
}

the key is the Shopify property name and the value is my application object name but I can't figure out how to make them based on this info.

CodePudding user response:

Basically, this depends on the Customer's mind.

So if you don't have any way to mapping yet.

you can divide your customers into 3 or 4 groups or more.

then you can use a factory design pattern
or simply can use an "if" statement depending on a new field "CustomerGroupId" and this should come with the entity to your code

Then:

if (Customer.CustomerGroupId == 1)
{
 // do mapping like Order.Note = shopify.Note
}
else if (Customer.CustomerGroupId == 1)
{
 // do mapping like Order.Customer.Note = shopify.Note
}

CodePudding user response:

A very simple option is to use the "mapping info" within the Order class, and then modify the OrderNote property to use the mapping for getting/setting the note.

For example, create a method that provides the mapping:

private string _map = "note"; // default value when SetMap is not used

public void SetMap(string map)
    => _map = map;

And then OrderNote becomes:

private string _note;

public string OrderNote { 
    get => GetNote();
    set => SetNote(value);
}

private string GetNote()
{
    if(_map == "note") return _note;
        
    if(_map == "Customer.Note") return Customer.Note;
    
    throw new ArgumentOutOfRangeException("Invalid _map");
}

private void SetNote(string value)
{
    if(_map == "note") { _note = value; return; }
        
    if(_map == "Customer.Note") { Customer.Note = value; return; }
    
    throw new ArgumentOutOfRangeException("Invalid _map");
}

Use the same pattern for each type of mapping.

  • Related