c# - How to map two class properties based on Customer
Solution:
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
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))
}
Answer
Solution:
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
}
Answer
Solution:
A very simple option is to use the "mapping info" within theOrder
class, and then modify theOrderNote
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 thenOrderNote
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.
Source
Didn't find the answer?
Our community is visited by hundreds of Shopify development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.