3

I need a little help with some mappings I am doing.

I am mapping a Model which has two fields

public ProductCategory
public string FirstType
public string SecondType

to another Model which has only one field

public string ProductType

Now I have to map the First or Second Type to ProductType based on a the content of ProductCategory.And if the condition is not met the ProductType should be null

For example I need something like this:

.ForMember(dest => dest.ProductType, opt => opt.MapFrom(src => 
{
   if (src.ProductCategory.Equals("something")
     { 
        src.FirstType
     }
   else if (src.ProductCategory.Equals("something")
     {
        src.SecondType
     }
   else 
    {
       null 
    }
}))

Of course the syntax is completely wrong and obviously won`t work , I just wanted to explain what I am trying to achieve.

I have a temporary solution

.ForMember(dest => dest.ProductType, opt => opt.MapFrom(src => src.ProductCategory.Contains("something") ? src.FirstType: src.SecondType))

but it is not completely what I need.

Any suggestions?

Thanks in advance

6
  • Why don't you actually extract this onto a method? It might clean up a bit Commented Oct 16, 2018 at 17:11
  • You mean instead using AutoMapper? Commented Oct 16, 2018 at 17:18
  • Nono, you can use AutoMapper but extracting the variable result from a method. Something like .ForMember(dest => dest.ProductType, opt => opt.MapFrom(src => CalculateProductCategory(src.ProductCategory))) and then you write your own CalculateProductCategory method Commented Oct 16, 2018 at 17:19
  • Right, it can work this way , thanks a lot. The only problem is that I have 5 or 6 fields like that which have to be populated based on the same condition , so I have to write 5-6 methods for every field, but it is still a solution to my problem Commented Oct 16, 2018 at 17:25
  • You can reuse resolvers and, for the latest, value converters. Commented Oct 17, 2018 at 5:33

1 Answer 1

3

What you can do to avoid making the map code look very tangled is to actually separate it into methods that you actually know require some checking for the right value to be assigned.

Here's the code

.ForMember(dest => dest.ProductType, opt => opt.MapFrom(src => CalculateProductCategory(src.ProductCategory))) and then you write your own CalculateProductCategory

And your method would look something like this

    public ProductType CalculateProductCategory(ProductCategory category) {

    if (productCategory.Equals("something")
    { 
        return FirstType
    }
    else if (productCategory.Equals("something")
    {
        return SecondType
    }
    else 
    {
       return null 
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.