skip to Main Content

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.

3

Answers


  1. 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
    }
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. First, you can make a web form to admin this task

    we need a table we will call it MappingInfo table.

    enter image description here

    The idea is for 1 field you have 2 or 3 options to map it with the other class

    so you will insert into this table all your customer’s mapping options

    after that, you will use the Automapper like this

    var config = new MapperConfiguration(GetMapperConfigurationExpression());
    var _mapper = config.CreateMapper();
    
    private MapperConfigurationExpression GetMapperConfigurationExpression(Int customerId)
    {
        MapperConfigurationExpression mapperConfiguraitonExpression = new MapperConfigurationExpression();  
    
        // Get Data From MappingInfo Table where MappingInfo.customerId == customerId "from the parameters" and MappingInfo.PropertyName == "AppId"
        
        if (MappingInfo.OptionId==1)
          mapperConfiguraitonExpression.CreateMap<srcMyClass, destMyClass>().ForPath(dest => dest.AppId.Code, opt => opt.MapFrom(src => src.InvoiseId))
          
        if (MappingInfo.OptionId==2)
          mapperConfiguraitonExpression.CreateMap<srcMyClass, destMyClass>().ForPath(dest => dest.AppId.Code, opt => opt.MapFrom(src => src.LogecalId))
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search