13

Please can any one advise how to use conditional mapping in AutoMapper to map a value in the TARGET object from a SOURCE object based upon an existing TARGET property value?

So my source class is:

public class UserDetails
{
    public String Nickname { get; set; }
}

My target class is:

public class ProfileViewModel
{
    public Boolean NicknameIsVisible { get; set;
    public String Nickname { get; set; }
}

I want to set the "Nickname" property value in the TARGET to match the "Nickname" property value in the SOURCE only if the target property "NicknameIsVisible" value is already set to TRUE, otherwise I want to set the TARGET "Nickname" property value to an empty string.

I was trying something like this (which wont compile )...

Mapper.CreateMap<UserDetails, ProfileViewModel>()
.ForMember(
            destination => destination.Nickname,
            option => option.
                .MapFrom(
                    source => source.NicknameIsVisible ? 
                    source.Nickname :
                    String.Empty)
);

but "NicknameIsVisible" is not a property of my SOURCE but of my TARGET.

BTW, My ProfileViewModel is bound to three entities using Owain Wragg's method (http://consultingblogs.emc.com/owainwragg/archive/2010/12/22/automapper-mapping-from-multiple-objects.aspx) and it is another entity that gives the value to the "NicknameIsVisible" property.

Please can anyone suggest the right syntax to use for this problem?

2 Answers 2

15

Try this:

Mapper.CreateMap<UserDetails, ProfileViewModel>()
.ForMember(
        destination => destination.Nickname,
        option => 
        {
            option.Condition(rc => 
            {
                var profileViewModel = (ProfileViewModel)rc.InstanceCache.First().Value;
                return profileViewModel.NicknameIsVisible;
            });

            option.MapFrom(source => source.Nickname);
        }
);
3
  • Thank you. I will try that later and let you know.
    – Dib
    Jun 23, 2014 at 7:41
  • I am getting compile errors with your example. I have changed it to below to my version below to get the code to compile:
    – Dib
    Jun 23, 2014 at 12:33
  • what version is this? rc is source object in my version which only contains source properties. Things like InstanceCache do no exist.
    – JSON
    Mar 13, 2019 at 15:23
6

Using devduder's example I now have the following code which compiles:

.ForMember(
    destination => destination.Nickname,
    option => 
    {
        option.Condition(resolutionContext =>
            (resolutionContext.InstanceCache.First().Value as ProfileViewModel).NicknameIsVisible);
        option.MapFrom(source => source.Nickname);
    }
);

However although it compiles and runs through it is not populating the destination.Nickname with anything.

Edit: I had to change the order of my mapping so the preferences object (which has the values for the "NicknameIsVisible" property was mapped first so the value was available to test against!)

So the call to my three-way mapping was:

var profileViewModel = EntityMapper.Map<ProfileViewModel>(preferences, member, account);

This ensured that the preferences object was mapped to the ViewModel first, then the conditional mapping for the account object could take place once the values had been set.

So this is my solution, but I cannot up-vote my own answer!

3
  • I think it may be because the property has not been set in the destination, so it cannot use it. I will probably need to change my mapping order to achieve what I require. I will feed back with my findings.
    – Dib
    Jun 23, 2014 at 12:43
  • 1
    Let us know how you go, I would be interested to know what you find.
    – devduder
    Jun 23, 2014 at 22:57
  • I have changed the order the mapping happens so the condition in my code above was met before the condition was tested for. So in essence the code that worked for me is the code here. But massive thanks to @devduder whose suggested code got me on the right track to my solution in the first place.
    – Dib
    Jun 24, 2014 at 6:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.