将封装的属性映射到IEnumerable

时间:2013-10-14 11:19:46

标签: c# automapper automapper-3

鉴于以下课程

public class Foo {
    public string Name { get; set; }
}

public class Bar {
    public string Name { get; set; }
}

public class Foos : IEnumerable<Foo> {
    private IList<Foo> _foos;

    public Foos(int max = 10)
    {
        _foos = Enumerable.Range(0, max)
            .Select(i => new Foo() { Name = "Foo" + i})
            .ToList();
    }

    public IEnumerator<Foo> GetEnumerator()
    {
        return _foos.GetEnumerator();
    }

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

和以下映射:

Mapper.CreateMap<Foo, Bar>();

我可以将Foos映射到IEnumerable<Bar>

Mapper.Map<IEnumerable<Bar>>(new Foos(5));

但是,如果我有一个看起来像这样的附加类:

public class FooContainer {
    private Foos _foos;

    public FooContainer()
    {
        _foos = new Foos(5);
    }

    public Foos Foos { get { return _foos; } }
}

如何设置映射,以便FooContainer映射到IEnumerable<Bar>

修改

好的,只是为了澄清这个问题 - 如果我有以下三个课程:

public class Foo {
    public string Name { get; set; }
}

public class Bar {
    public Guid SourceId { get; set; }
    public string Name { get; set; }
}

public class FooContainer {
    public Guid Id { get; set; }
    public List<Foo> Foos { get; set; }
}

如何设置FooContainerIEnumerable<Bar>的映射,以便FooContainer.Id映射到每个Bar.SourceIdFoo.Name映射到Bar.Name

1 个答案:

答案 0 :(得分:1)

对于复杂逻辑,您可以实现ITypeConverter

public class FooContainerTypeConverter
    : ITypeConverter<FooContainer, IEnumerable<Bar>>
{
    public IEnumerable<Bar> Convert(ResolutionContext context)
    {
        var source = context.SourceValue as FooContainer;

        if(source == null)
        {
            return Enumerable.Empty<Bar>();
        }

        var result = source.Foos.Select(foo => MapBar(foo, source.Id));
        return result;
    }

    private static Bar MapBar(Foo item, Guid id)
    {
        // Use the existing Foo => Bar mapping
        var bar = Mapper.Map<Foo,Bar>(item);
        bar.Id = id;
        return bar;
    }
}

用法:

Mapper.CreateMap<FooContainer, IEnumerable<Bar>>()
      .ConvertUsing<FooContainerTypeConverter>();