Given the following classes
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();
}
}
and the following mapping:
Mapper.CreateMap<Foo, Bar>();
I can map Foos
to IEnumerable<Bar>
with:
Mapper.Map<IEnumerable<Bar>>(new Foos(5));
But, if I have an additional class that looks like this:
public class FooContainer {
private Foos _foos;
public FooContainer()
{
_foos = new Foos(5);
}
public Foos Foos { get { return _foos; } }
}
How would I set up a mapping so that FooContainer
maps to IEnumerable<Bar>
?
Edit
OK, just to clarify the question - if I've got the following three classes:
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; }
}
how do I set up a mapping for FooContainer
to IEnumerable<Bar>
such that FooContainer.Id
maps to each Bar.SourceId
and Foo.Name
maps to Bar.Name
?
For complex logic you can implement an 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;
}
}
Usage:
Mapper.CreateMap<FooContainer, IEnumerable<Bar>>()
.ConvertUsing<FooContainerTypeConverter>();