views:

250

answers:

2

hey guys, I'm using automapper version 1.1.0.188

In my AutoMapper.Configure I'm mapping Entities to DTOs and vice versa, like so:

// entity >> DTO
Mapper.CreateMap<MetaTemplate, MetaTemplateDTO>();
Mapper.CreateMap<Person, PersonDTO>();     

// DTO >> Entity
Mapper.CreateMap<MetaTemplateDTO, MetaTemplate>();
Mapper.CreateMap<PersonDTO, Person>();

When I do the below mappings (and vice versa) everything works fine

Mapper.Map<entity, entityDTO>(entity);

Mapper.Map<List<entity>, List<entityDTO>>(entities);

Note above that automapper just works with List<> without me having to configure anything.

I have a Generic Container (simplified for this example):

public class Container<T>
{
    public int TotalItems{get;set;}
    public IList<T> Items{get;set;}
}

Now, without any extra automapping config, when I do:

Mapper.Map<Container<entity>, Container<entityDTO>>(entityContainer);

I get an automapper exception:

Missing type map configuration or unsupported mapping.Exception

However, if I add this line in the automap configure for a specific type, as below, then the Container mapping works.

Mapper.CreateMap<Container<PersonDTO>, Container<Person>>();

However, it will ONLY work for that Person/PersonDTO type.

Why is this? How can I make automapper recognize the Container class as it recognizes List<>??

I don't want to explicitly configure mappings for every type AGAIN.

cool, cheers

+1  A: 

If your generic container class behaves like a list of objects then you may be best off implementing the IEnumerable interface. Then the automapper should be able to iterate through the objects and map them accordingly.

Sugendran
hmm... interesting suggestion, I'll try it out and let you know
andy
nope didn't work. just tried it and implementing all of IEnumerable didn't work. weirdly, as I say above, if I explicitly map.CreateMap Container<T> to Container<T1> it works fine... which is weird??
andy
That's the expected behaviour, the automapper can only map what it knows about
Sugendran
After reading the code you need to implement IList instead of IEnumerable - check out http://github.com/jbogard/AutoMapper/blob/master/src/AutoMapper/Mappers/EnumerableMapper.cs
Sugendran
You may want to also read http://groups.google.com/group/automapper-users/browse_thread/thread/cec579c8d249c1c5?pli=1
Sugendran
+1  A: 

The answer is that Automapper just doesn't deal with unsupported types, even if the Type you're using is a container of supported types.

The solution was to map the object manually

andy