Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Automapper Example

I am having a hard time to understand how to map certain objects. Please answer some questions about this simple example.

Example code

class User {     private int id;     private string name; }  class Group {     private int id;     private string name;     private List<User> users; }  [DataContract] public class UserDto {     [DataMember]     public int id { get; set; }     [DataMember]     public string name{ get; set; }       }  [DataContract] public class GroupDto {     [DataMember]     public int id { get; set; }     [DataMember]     public string name{ get; set; }     [DataMember]     public List<User> Users { get; set; }       } 

The mappers

Mapper.CreateMap<User, UserDto>(); Mapper.CreateMap<UserDto, User>();  Mapper.CreateMap<Group, GroupDto>(); Mapper.CreateMap<GroupDto, Group>(); 

When mapping Group to GroupDto, do you have to map User to UserDto internally because the List<User> in Group consist of unmapped Users? If so how do you do this? My guess is

Mapper.CreateMap<Group, GroupDto>()     .ForMember(g => g.id, opt => opt.Ignore());     .ForMember(g => g.name, opt => opt.Ignore());     .ForMember(g => g.Users, opt => opt.MapFrom(u => Mapper.Map<Group, UserDto>(u))) 

Is this correct?

like image 670
David Avatar asked Dec 17 '13 13:12

David


People also ask

What is AutoMapper C# example?

AutoMapper in C# is a library used to map data from one object to another. It acts as a mapper between two objects and transforms one object type into another. It converts the input object of one type to the output object of another type until the latter type follows or maintains the conventions of AutoMapper.

What is an AutoMapper?

AutoMapper is a popular object-to-object mapping library that can be used to map objects belonging to dissimilar types. As an example, you might need to map the DTOs (Data Transfer Objects) in your application to the model objects.

How do I use Auto Mapper?

Open startup. cs class file, add “services. AddAutoMapper(typeof(Startup))” in configure services method. Now the AutoMapper Package was installed and configured in our project.


1 Answers

1- Change the GroupDto to be like this:

[DataContract] public class GroupDto {     [DataMember]     public int id { get; set; }     [DataMember]     public string name{ get; set; }     [DataMember]     public List<UserDTO> Users { get; set; }       } 

2- Create your mappings :

Mapper.CreateMap<User, UserDto>(); Mapper.CreateMap<UserDto, User>(); // Only if you convert back from dto to entity  Mapper.CreateMap<Group, GroupDto>(); Mapper.CreateMap<GroupDto, Group>(); // Only if you convert back from dto to entity 

3- that's all, because auto mapper will automatically map the List<User> to List<UserDto> (since they have same name, and there is already a mapping from user to UserDto)

4- When you want to map you call :

Mapper.Map<GroupDto>(groupEntity); 

Hope that helps.

like image 106
Omar.Alani Avatar answered Sep 19 '22 19:09

Omar.Alani