0

I swear I've done it before, but I can't seem to get it to work now. Basically I'm getting an EndUser containing a Site, and I want to map that Site to a SiteInfo object.

Here's my map configuration:

Mapper.CreateMap<Site, SiteInfo>()
    .ForMember(dest => dest.SiteID, opt => opt.MapFrom( src => src.ID))
    .ForMember(dest => dest.CompanyID, opt => opt.MapFrom(src => src.Company.ID));

Mapper.CreateMap<EndUser, SiteInfo>()                
    .ForMember( dest => dest, opt => opt.MapFrom(src => src.Site ));

So EndUser.Site should be able to map to SiteInfo. I can do this on the outside by just calling Mapper.Map<SiteInfo>(EndUser.Site). I think it's just cleaner syntax to map from the end user to the location info directly.

So how can I map directly from EndUser to SiteInfo? Basically this is the code I'd LIKE to write:

var user = mcp.Users.GetEndUser(userAddress.Address);
var siteInfo = Mapper.Map<SiteInfo>(user);

Currently I'm working around the issue by just mapping from EndUser.Site like this:

var user = GetEndUser(emailAddress);
            var siteInfo = user == null 
                            ? null as SiteInfo 
                            : Mapper.Map<SiteInfo>(user.Site);
CodeRedick
  • 7,346
  • 7
  • 46
  • 72

1 Answers1

0

Do you want to map Location to SiteInfo or LocationInfo? What is the dest property you're mapping to in your EndUser -> SiteInfo mapping? Does SiteInfo contain a combination of both Location and EndUser properties?

Without that info I'm only guessing, but are you trying to do something like this?

var rc = Mapper.Map<SiteInfo>(endUser);
rc = Mapper.Map(endUser.Location, rc);

UPDATE

So if I understand correctly, you're after a mapping like this?

Mapper.CreateMap<EndUser, SiteInfo>()
    .ForMember(dest => dest.SiteID, opt => opt.MapFrom( src => src.Site.ID))
    .ForMember(dest => dest.CompanyID, opt => opt.MapFrom(src => src.Site.Company.ID));
Mightymuke
  • 5,094
  • 2
  • 31
  • 42
  • Edited to answer some of your questions and make things more clear (Location should really be Site throughout the example, but I switched names midway through the project. ) – CodeRedick Dec 02 '12 at 01:45