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
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:
A very simple option is to use the "mapping info" within the
Order
class, and then modify theOrderNote
property to use the mapping for getting/setting the note.For example, create a method that provides the mapping:
And then
OrderNote
becomes:Use the same pattern for each type of mapping.
First, you can make a web form to admin this task
we need a table we will call it MappingInfo table.
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