1

I'm wondering if anyone has ran into this before. I have a simple mapping below to convert between a string and Email.


    public void Register(TypeAdapterConfig config)
    {
        config.NewConfig<string, Email>()
            .MapWith(value => new Email(value));
        config.NewConfig<Email, string>()
            .MapWith(email => email.Value);
    }

The problem is, when the input value for an object is null, Mapster doesn't use this mapping because it sees null instead of string.

For example, say I have a User with this mapping below. This with the above works fine when I pass an email in, but if the email is null, then the mapping won't trigger.


class User 
{
    public Email Email { get; set; }
}

// ...

    public void Register(TypeAdapterConfig config)
    {
        config.NewConfig<UserDto, User>();
        config.NewConfig<User, UserDto>();
        config.NewConfig<UserForCreationDto, User>()
            .TwoWays();
        config.NewConfig<UserForUpdateDto, User>()
            .TwoWays();
    }

I can change the user mapping to this, but I'd rather not have to do this every place I have an email if i can avoid it.


    public void Register(TypeAdapterConfig config)
    {
        config.NewConfig<UserDto, User>();
            .Map(x => x.Email, y => new Email(y.Email));
        config.NewConfig<User, UserDto>();
            .Map(x => x.Email, y => y.Email.Value);
        config.NewConfig<UserForCreationDto, User>()
            .TwoWays();
        config.NewConfig<UserForUpdateDto, User>()
            .TwoWays();
    }
Paul DeVito
  • 1,542
  • 3
  • 15
  • 38

0 Answers0