0

I am trying to map a class, that contains a generic collection, and would like to map that class to another class where the collection is of another custom type. However, when i try to do the mapping i get an AutomapperMapperException. I made a simplified test project that reproduces the problem:

[TestFixture]
public class Test
{
    [Test]
    public void TestBehavior()
    {
        Mapper.CreateMap<TestEntity, TestDto>()
              .ForMember(x => x.Foos, m => m.ResolveUsing<FooCollectionResolver>());

              //.ForMember(x => x.Foos, m => m.Ignore())
              //.AfterMap((testEntity, testDto) =>
              //{ testDto.Foos = new FooCollection<Foo>(testEntity.Foos); });

        Mapper.AssertConfigurationIsValid();

        var entity = new TestEntity()
        {
            Id = Guid.NewGuid(),
            Name = "Test entity",
            Foos = new Collection<Foo>
            {
                new Foo { Id = Guid.NewGuid(), Name = "First" },
                new Foo { Id = Guid.NewGuid(), Name = "Second "},
                new Foo { Id = Guid.NewGuid(), Name = "Third" }
            }
        };

        var dto = Mapper.Map<TestDto>(entity);

        Assert.IsNotNull(dto);
    }
}

public class FooCollectionResolver : ValueResolver<TestEntity, IFooCollection<Foo>>
{
    protected override IFooCollection<Foo> ResolveCore(TestEntity source)
    {
        return new FooCollection<Foo>(source.Foos) {CustomProperty = "Something interesting"};
    }
}

// Custom collection class looks like this (simplified for this example)
public class FooCollection<T> : IFooCollection<T>
{
    public FooCollection(IEnumerable<T> items)
    {
        Items = items;
    }

    protected IEnumerable<T> Items { get; set; }

    public IEnumerator<T> GetEnumerator()
    {
        return Items.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public string CustomProperty { get; set; }
}

when calling the Mapper.Map() method i get the following exception, that would suggest to me that it doesn't know how to map from FooCollection to IFooCollection:

Mapping types:
FooCollection`1 -> IFooCollection`1
AutomapperTest.FooCollection`1[[AutomapperTest.Foo, AutomapperTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> AutomapperTest.IFooCollection`1[[AutomapperTest.Foo, AutomapperTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

Destination path:
TestDto.Foos.Foos

Source value:
AutomapperTest.FooCollection`1[AutomapperTest.Foo]

Inner exception says:

{"Unable to cast object of type 'System.Collections.Generic.List`1[AutomapperTest.Foo]' to type 'AutomapperTest.IFooCollection`1[AutomapperTest.Foo]'."}

...so finally my question is: how do i get automapper to map a collection to my custom collection type? I could successfully do the conversion manually using .AfterMap(..), but i'm not sure that's the intended solution for this problem?

carl
  • 375
  • 4
  • 17
  • Somewhere (probably in the FooCollectionResolver) you're casting a `List` to an `IFooCollection`. Can you show the FooCollectionResolver source? – Gert Arnold Feb 26 '14 at 09:43
  • The source to `FooCollectionResolver` is in the post (might need to scroll a little bit though). I'm not aware that i'm casting from `List` to `IFooCollection` anywhere? – carl Feb 26 '14 at 14:01

0 Answers0