Examples initializing MapperConfiguration with multiple mappings needed

13,691 views
Skip to first unread message

Dave

unread,
Jan 28, 2016, 8:13:28 PM1/28/16
to AutoMapper-users
I'm using AutoMapper for the 1st time and soon saw the latest version had deprecated the Mapper.Initialize() and Mapper.Map<>() APIs in favor of CreateMapper and MapperConfiguration instead.

The problem is I've found the documentation and general help online to be greatly lacking for these new API's.  I figured out the syntax mapping DB Entity objects to Business layer objects but am unsure about the syntax with MapperConfiguration to initialize for multiple mappings.  I.e. I've got a mapping from my PTT_Event class to Event, but need to map several other classes PTT_Address to Address, etc.  I would like to use just 1 MapperConfiguration class to perform mappings for all my objects of course and not 1 MapperConfiguration class for each set of mappings needed.  

I.e. how do initialize MapperConfiguration object with multiple mappings (PTT_Event -> Event, PTT_Address -> Address, etc)?

Sample code below (prototype code so its bit messy):

mapperConfig = new MapperConfiguration(cfg => cfg.CreateMap < Event, PTT_Event >().ReverseMap());
autoMapper = mapperConfig.CreateMapper();

[TestMethod]
public void GetEventById(int eventid)
{
    Event reqEvent = null;

    using (var context = new PTDLinkRupeeEntities())
    {
        PTT_Event dbEvent = context.PTT_Event
            .Where(e => e.EventId == eventId)
            .Select(e => e)
            .FirstOrDefault();

        reqEvent = autoMapper.Map<Event>(dbEvent);

        Assert.IsNotNull(reqEvent, "Not seeing event returned for eventId {0}.", eventId);
    }
}

peter.c...@agilityapplications.com

unread,
Jan 28, 2016, 8:51:23 PM1/28/16
to AutoMapper-users, dajo...@gmail.com
Hi Dave,

Multiple mappings are created using something like:

mapperConfig = new MapperConfiguration(cfg => {
  cfg.CreateMap<PTT_Event, Event>();
  cfg.CreateMap<PTT_Address, Address>();
});

and doing a CreateMap for as many mapping configurations as you need.

Hope this helps.

Regards,
Peter Callaghan

dajo...@gmail.com

unread,
Jan 28, 2016, 8:59:59 PM1/28/16
to AutoMapper-users, dajo...@gmail.com, peter.c...@agilityapplications.com
Thanks so much for the prompt reply.  I was just returning to the inquiry to post my solution and found you had beat me to it, nice!

I really hope more examples of MapperConfiguration / CreateMapper are posted out there to simplify for others. 

I should be good for now though, thanks again, dave

Jimmy Bogard

unread,
Jan 28, 2016, 8:59:59 PM1/28/16
to automapp...@googlegroups.com
The wiki has been completely updated for the new API. Let me know if there is anything missing there, or if you see any place that still has the old API.
--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

peter.c...@agilityapplications.com

unread,
Jan 28, 2016, 10:50:48 PM1/28/16
to AutoMapper-users
Thanks Jimmy, I was about to post that info too.

Just to make things easier, here's a link to the wiki > https://github.com/AutoMapper/AutoMapper/wiki
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-users+unsubscribe@googlegroups.com.

dajo...@gmail.com

unread,
Feb 2, 2016, 1:38:08 PM2/2/16
to AutoMapper-users, peter.c...@agilityapplications.com
Thanks for the replies, I followed the wiki (https://github.com/AutoMapper/AutoMapper/wiki) as mentioned and was able to get fairly sophisticated nested (with several collections), bi-directional, mappings to work fairly well.

Btw I found there are much more help available on the older version of AutoMapper API's than the latest version.  When searching for AutoMapper / MapperConfiguration in google searches I found many hits on older AutoMapper API's.
Another issue I'm having is with updating nested Entity Framework (EDM) objects in DB, I'll post about the issue in separate topic to help others who may also have similar issues / be looking for a fix.

thanks again, dave
Reply all
Reply to author
Forward
0 new messages