1

I am currently facing an error whilst trying to map data objects as shown below;

Method 'get_Item' in type 'Proxy_System.Collections.Generic.IList`1[[InsureAfrika_API.Model.DataModel, InsureAfrika API, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]_28141317_' from assembly 'AutoMapper.Proxies, Version=0.0.0.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005' does not have an implementation.

Below are my data sources; QuoteSourceDto

public string ResponseStatus { get; set; }
public IList<Data> Data { get; set; }

DataClass

public string Client { get; set; }
public IList<Extensions> Extensions { get; set; }

Extensions Source Class

public Decimal Limit { get; set; }
public Decimal Rate { get; set; }

My Destination Class; QuoteDestinationModel

public string ResponseStatus { get; set; }
public IList<DataModel> Data { get; set; }

DataModel

public string Client { get; set; }
public IList<ExtensionsModel> Extensions { get; set; }

ExtensionModel Class

public Decimal Limit { get; set; }
public Decimal Rate { get; set; }

Below is my mapper configuration;

var config = new MapperConfiguration(cfg => {
   cfg.CreateMap<IList<Extensions>, IList<ExtensionsModel>>();
   cfg.CreateMap<IList<Data>, IList<DataModel>>();

   cfg.CreateMap<QuoteSourceDto, QuoteDestinationModel>()
                    .ForMember(dest => dest.Data, opt => opt.MapFrom(src => src.Data))
                    .ForMember(dest => dest.ResponseStatus, opt => opt.Ignore());
})

My Data Source;

IList<Extensions> exts =new List<Extensions>
{
   new Extensions{Rate=1.34M,Limit=21400.00M};
}

IList<Data> dat = new List<Data>
{
  new Data{Client="Micael Angelus", Extensions =exts};
};

QuoteSourceDto dtos = new QuoteSourceDto
{
     ResponseStatus = "Success",
     Data = dat
};

Thus my Map function call is as shown below;


var _client = mapper.Map<QuoteSourceDto, QuoteDestinationModel>(dtos);

When I use type List it returns no error and no result just an empty list and when I use IList it returns the stated error above. All I want to do is map results to QuoteDestinationModel.

EDIT I'm Using Automapper Version 9.0.0

4
  • docs.automapper.org/en/latest/Lists-and-arrays.html Commented Dec 10, 2019 at 8:22
  • I've spent 3hrs debugging using their documentation and I still haven't figured out why I'm getting this error. Commented Dec 10, 2019 at 8:25
  • There are many examples available online. Try the tests in the AM repo. Commented Dec 10, 2019 at 9:45
  • 1
    That's not an answer Commented Dec 10, 2019 at 11:15

1 Answer 1

3

AutoMapper documentation states:

"AutoMapper only requires configuration of element types, not of any array or list type that might be used. " ~ Documentation

What that means is that you don't have to (and you shouldn't) create mapping profiles between particular types of collections (using generic type parameter), like you did:

// Don't do that!
cfg.CreateMap<IList<Extensions>, IList<ExtensionsModel>>();

Instead, create mapping profiles between concrete types:

var config = new MapperConfiguration(cfg =>
{
    cfg.CreateMap<Extensions, ExtensionsModel>();
    cfg.CreateMap<Data, DataModel>();
    cfg.CreateMap<QuoteSourceDto, QuoteDestinationModel>()
        .ForMember(dest => dest.ResponseStatus, opt => opt.Ignore());
});

and AutoMapper will create a collection for you. Types of source collections supported:

  • IEnumerable
  • IEnumerable<T>
  • ICollection
  • ICollection<T>
  • IList
  • IList<T>
  • List<T>
  • Array

Notice that I removed the configuration part telling AutoMapper how to map between Data members. As both - source and destination - models have the same name, which is just Data, AutoMapper will map them just fine without explicitly telling it how to do that. That is, if you previously create a map between Data and DataModel types in configuration.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.