Automapper complex types mapping exception

2019-02-19 17:48发布

问题:

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:

  1. MvcModel.Params to be mapped to Request.Parameters and backwards
  2. MvcModel.Steps to be mapped to Request.RequestContent and backwards
  3. InnerId, Session, Clerk, Private from Request.RequestContent must 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?

回答1:

Thank's to stuartd this are working now!

The thing that I didn't understand was, that I have to map bottom to top! All the sub-objects have to mapped first, so they would be recognized at the moment parent objects are mapped!

Now profile's looks like this:

public class Out: Profile
{
   protected override void Configure()
    {
        CreateMap<Step1, PersonalDetails>();
        CreateMap<Step2, Phones>();
        CreateMap<Step3, ElectonicCommunication>();

        CreateMap<Params, Parameters>();
        CreateMap<Params, RequestContent>()
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.RequestId))
            .ForAllMembers(opt => opt.Ignore());
        CreateMap<Steps, RequestContent>()
            .ForMember(dest => dest.PersonalDetails, opt => opt.MapFrom(src => src.Step1))
            .ForMember(dest => dest.Phones, opt => opt.MapFrom(src => src.Step2))
            .ForMember(dest => dest.ElectonicCommunication, opt => opt.MapFrom(src => src.Step3))
            .ForAllMembers(opt => opt.Ignore());

        CreateMap<MvcModel, Request>()
            .ForMember(dest => dest.Parameters, opt => opt.MapFrom(src => src.Params))
            .ForMember(dest => dest.RequestContent, opt => opt.MapFrom(src => src.Steps));
    }
}