AutoMapper How to map nested object from an ObjectId

Posted by RayMartinsHair on Stack Overflow See other posts from Stack Overflow or by RayMartinsHair
Published on 2010-04-14T01:07:43Z Indexed on 2010/04/14 1:13 UTC
Read the original article Hit count: 368

Filed under:

I am trying to map the ReferralContract.AssessmentId property to Referral.Assessment.Id The below code works but I am sure that there is a cleaner way to do.... Please tell me this is so ;-)

// Destination classes
public class Referral
{

    public Referral()
    {
        Assessment = new Assessment();
    }

    public int Id { get; set; }
    public Assessment Assessment { get; set; }
}

public class Assessment
{
    public int Id { get; set; }
}

// Source Class
public class ReferralContract
{
    public int Id { get; set; }
    public int AssessmentId { get; set; }
}

The Automapper mapping I am using is

Mapper.CreateMap<ReferralContract, Referral>()
            .ForMember(x => x.Assessment, opt => opt.MapFrom(scr => new Assessment
                                                                        {
                                                                            Id = scr.AssessmentId
                                                                        }));

© Stack Overflow or respective owner

Related posts about automapper