Given the following sample classes
public class User
{
public string Username { get; set; }
public string FieldFromUser { get; set; }
}
public class Todo
{
public string Title { get; set; } // !! map this one to UsernameWithTodoTitle.TodoTitle !!
public string FieldFromTodo { get; set; }
}
public class UsernameWithTodoTitle
{
public string Username { get; set; }
public string TodoTitle { get; set; } // !! this field represents Todo.Title !!
public string FieldFromUser { get; set; }
public string FieldFromTodo { get; set; }
}
I want to map a user and a todo to UsernameWithTodoTitle. To solve this I'm using the packages
AutoMapper v10.1.1
AutoMapper.Extensions.Microsoft.DependencyInjection v8.1.1
When dealing with multiple sources this solution works fine for me:
public class UsernameWithTodoTitleMappingProfile : Profile
{
public UsernameWithTodoTitleMappingProfile()
{
CreateMap<(User, Todo), UsernameWithTodoTitle>()
.ForMember(
destination => destination.Username,
memberOptions => memberOptions.MapFrom(source => source.Item1.Username))
.ForMember(
destination => destination.TodoTitle,
memberOptions => memberOptions.MapFrom(source => source.Item2.Title))
.ForMember(
destination => destination.FieldFromUser,
memberOptions => memberOptions.MapFrom(source => source.Item1.FieldFromUser))
.ForMember(
destination => destination.FieldFromTodo,
memberOptions => memberOptions.MapFrom(source => source.Item2.FieldFromTodo));
}
}
So I could map a user and a todo like so
var usernameWithTodoTitle = _mapper.Map<UsernameWithTodoTitle>((user, todo));
Since I'm using a tuple I have to specify each field because obviously Automapper can't know where to search for these fields when dealing with a tuple. The only different field names are Todo.Title and UsernameWithTodoTitle.TodoTitle. I would like to know if I can simplify the mapping profile, maybe I don't even have to use a tuple? The following profile is just a pseudo implementation what I would like to achieve
public class UsernameWithTodoTitleMappingProfile : Profile
{
public UsernameWithTodoTitleMappingProfile()
{
CreateMap<(User, Todo), UsernameWithTodoTitle>()
// map all fields by name as expected and ...
.ForMember(
destination => destination.TodoTitle,
memberOptions => memberOptions.MapFrom(source => source.Item2.Title));
}
}