0

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 ?

Tom
  • 8,175
  • 41
  • 136
  • 267
  • 1
    You should configure your maps *once*, when the application starts; this code will re-map your entities every time the method is called. –  Nov 14 '16 at 23:37
  • Hi Amy, I am looking at an example. Could you explain how it is done. – Tom Nov 15 '16 at 06:45
  • What do you not understand? Move the code that configures your maps into a separate method that gets called on application startup. –  Nov 15 '16 at 16:26
  • Hi Amy, as you can see above, I have the maps in seperate method and calling it in the global.asax. But I am getting the error in my controller. I have mentioned the exact issue in my post above. I am not able to understand your suggestion – Tom Nov 15 '16 at 16:48

0 Answers0