I'm trying to implement the AutoMapper for a new module.
I have the MVC model at the web site, I'm working on, and it looks like this:
public class MvcModel { public Params Params { get; set; } public Steps Steps { get; set; } } public class Params { public int? RequestId { get; set; } public bool NewClient { get; set; } } public class Steps { public Step1 Step1 { get; set; } public Step2 Step2 { get; set; } public Step3 Step3 { get; set; } } public class Step1 { public int Name { get; set; } } public class Step2 { public int Phone { get; set; } } public class Step3 { public int Email { get; set; } } For the other hand I have next class, that I have to pass to some service:
public class Request { public Parameters Parameters { get; set; } public RequestContent RequestContent { get; set; } } public class Parameters { public int NewClient { get; set; } } public class RequestContent { public int Id { get; set; } public int InnerId { get; set; } public string Session { get; set; } public string Clerk { get; set; } public bool Private { get; set; } public PersonalDetails PersonalDetails { get; set; } public Phones Phones { get; set; } public ElectonicCommunication ElectonicCommunication { get; set; } } public class PersonalDetails { public int Name { get; set; } } public class Phones { public int Phone { get; set; } } public class ElectonicCommunication { public int Email { get; set; } } I've created a new AutoMapper configuration class (that is called from Global.asax):
public class AutoMapperConfig { public static void Configure() { MapperConfiguration MapperConfiguration = new MapperConfiguration(cfg => { cfg.AddProfile<Out>(); cfg.AddProfile<In>(); cfg.CreateMap<MvcModel, Request>(); }); MapperConfiguration.AssertConfigurationIsValid(); } } public class Out: Profile { protected override void Configure() { CreateMap<MvcModel, Request>() .ForMember(dest => dest.Parameters, opt => opt.MapFrom(src => src.Params)) .ForMember(dest => dest.RequestContent, opt => opt.MapFrom(src => src.Steps)); } } public class In: Profile { protected override void Configure() { CreateMap<Request, MvcModel>() .ForMember(dest => dest.Params, opt => opt.MapFrom(src => src.Parameters)) .ForMember(dest => dest.Steps, opt => opt.MapFrom(src => src.RequestContent)); } } Those are schematically similar, though the real object are a bit bigger and have different names.
Excpectations: MvcModel to be mapped to Request and backwards.
To be more accurate:
MvcModel.Paramsto be mapped toRequest.Parametersand backwardsMvcModel.Stepsto be mapped toRequest.RequestContentand backwardsInnerId,Session,Clerk,PrivatefromRequest.RequestContentmust be ignored
The problem: I'm getting an error, when on application start:
The following property on ... cannot be mapped:
Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type ... Context: Mapping to property ... from ... to ... Mapping from type ... to ... from source type AutoMapperMessageBugTests.SourceChild Mapping to type Exception of type 'AutoMapper.AutoMapperConfigurationException' was thrown.
I didn't find good documentation on how to use AutoMapper globally for the application, and just can't figure how to use it properly and what exactly I'm doing wrong.
I couldn't find a good example of complex objects mapping ether...
Can someone help me with this?