7

Is it possible to set all string properties that are null in my source object to some default value within my destination object using AutoMapper?

For example, let's say I had the following two class definitions:

public class UniversalForm
{
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string FaxNumber { get; set; }
    ...
}

public class UniversalFormDto
{
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string FaxNumber { get; set; }
    ...
}

Now, MiddleName and FaxNumber are properties that are likely to be null in the UniversalForm class. So what I would like to be able to do is if FaxNumber or MiddleName are null then in UniversalFormDto object I would like for the value of the corresponding properties to be set to "N/A".

I know this can be accomplished by creating a mapping for each individual member, but I would like to avoid that if at all possible.

I'm looking for a way to define a default value for all my string properties to be used when the corresponding property on my source object (UniversalForm) is null.

4 Answers 4

11

Here's a few methods I know:

ForAllMembers

All null values on all properties will be substituted as "N/A". Will crash if any of the properties can't be mapped to a string:

cfg.CreateMap<UniversalForm, UniversalFormDto>()
.ForAllMembers(opt => opt.NullSubstitute("N/A"));

ForMember

A substitute for every individual property:

cfg.CreateMap<UniversalForm, UniversalFormDto>()
.ForMember(dto => dto.FirstName, opt => opt.NullSubstitute("N/A"))
...
;

ConvertUsing

Universal substitute for all null strings in any property:

cfg.CreateMap<string, string>().ConvertUsing(s => s ?? "N/A");
Sign up to request clarification or add additional context in comments.

Comments

5

Not sure if these existed when the question was posted, but I'd suggest using a Value Transformer like this:

var configuration = new MapperConfiguration(cfg => {
    cfg.ValueTransformers.Add<string>(val => val ?? "N/A");
});

or if using profiles:

public class MyProfile : Profile
{
   public MyProfile()
   {
     ValueTransformers.Add<string>(val => val ?? "N/A");
   }
}

Comments

2

I'm not too familiar with AutoMapper, but after doing some research you may want to try conditional mapping for each property.

Mapper.CreateMap<Source, Target>()
      .ForMember(dest => dest.MiddleName, 
                 opt => opt.MapFrom
                 (src => String.IsNullOrEmpty(src.MiddleName) 
                             ? "N/A" 
                             : src.MiddleName));

Hope this helps.

Comments

1

You can set your desired default properties in the constructor:

public UniversalFormDto() 
{
    FaxNumber = "N/A";
    MiddleName = "N/A";
    //etc...
}

1 Comment

That's probably what I will do if it turns out that AutoMapper can't do what I need it to do.

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.