Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper Dictionary Flattening

Tags:

automapper

I've got a Dictionary<User, bool>

User is as follows:

 public class User {
   public string Username { get; set; }
   public string Avatar { get; set;
}

The second type, bool, indicates whether this user is a friend of the logged in User. I want to flatten this Dictionary into a List<UserDto> UserDto is defined as:

public class UserDto {
   public string Username { get; set; }
   public string Avatar { get; set; }
   public bool IsFriend { get; set; }
}

IsFriend represents the value of the dictionary.

How can I do this?

like image 722
reach4thelasers Avatar asked May 09 '12 15:05

reach4thelasers


1 Answers

You should be able to do this with just one mapping1:

You need to map a KeyValuePair<User, bool> to UserDto. This is necessary for AutoMapper to be able to map the contents of the dictionary to the contents of the List<T> we're ultimately creating (more of an explanation can be found in this answer).

Mapper.CreateMap<KeyValuePair<User, bool>, UserDto>()
    .ForMember(dest => dest.Username, opt => opt.MapFrom(src => src.Key.UserName))
    .ForMember(dest => dest.Avatar, opt => opt.MapFrom(src => src.Key.Avatar))
    .ForMember(dest => dest.IsFriend, opt => opt.MapFrom(src => src.Value));

Then, use the mapping in your .Map call:

Mapper.Map<Dictionary<User, bool>, List<UserDto>>(...);

You don't need to map the collections themselves, as AutoMapper can handle mapping the Dictionary to a List as long as you've mapped the contents of the collections to each other (in our case, KeyValuePair<User, bool> to UserDto).


Edit: Here's another solution that doesn't require mapping every User property to UserDto:

Mapper.CreateMap<User, UserDto>();
Mapper.CreateMap<KeyValuePair<User, bool>, UserDto>()
    .ConstructUsing(src => Mapper.Map<User, UserDto>(src.Key))
    .ForMember(dest => dest.IsFriend, opt => opt.MapFrom(src => src.Value));

1Using AutoMapper 2.0

like image 173
Andrew Whitaker Avatar answered Nov 15 '22 09:11

Andrew Whitaker