Hi I am creating the following Web API method which returns data transfer object. The new version of the Auto Mapper works differently. Below is the old way, could somebody help me with the new approach
This is an example of AutoMapping using an old AutoMapper
public IEnumerable<NotiticationDto> GetNewNotifications()
{
var userid = User.Identity.GetUserId();
var userNotifications = _context.UserNotifications
.Where(un => un.UserId == userid)
.Select(un => un.Notification)
.Include(n => n.Gig.Artist)
.ToList();
Mapper.CreateMap<ApplicationUser, UserDto>();
Mapper.CreateMap<Gig, GigDto>();
Mapper.CreateMap<Notitication, NotiticationDto>();
});
return userNotifications.Select(Mapper.Map<Notitication>,<NotiticationDto>);
}
AutoMapping using the new approach
public IEnumerable<NotiticationDto> GetNewNotifications()
{
var userid = User.Identity.GetUserId();
var userNotifications = _context.UserNotifications
.Where(un => un.UserId == userid)
.Select(un => un.Notification)
.Include(n => n.Gig.Artist)
.ToList();
Mapper.Map<UserDto>(ApplicationUser);
Mapper.Map<GigDto>(Gig);
Mapper.Map<NotiticationDto>(Notitication);
});
}
I have created a class called MappingProfile
public class MappingProfile : Profile
{
public static void IntializeMappings()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<ApplicationUser, UserDto>();
cfg.CreateMap<Gig, GigDto>();
cfg.CreateMap<Notitication, NotiticationDto>();
});
}
}
In Global.asax , I have written the following code
Mapper.Initialize(cfg =>
{
cfg.CreateMissingTypeMaps = true;
cfg.AddProfile<MappingProfile>();
});
I am getting compile time error in the GetNewNotifications() web api method for e.g at the following line Mapper.Map(ApplicationUser); saying the type mentioned in the destination parameter cannot be of type class. It is expecting object. Also How do I return the data transfer object in the second example using the new approach like I have done in the first example. Also if somebody can suggest a better way of implementing it ?