4

I'm trying to map a collection of EntityFramework objects with a collection of view models.

 public class Channel
{
    public Guid Id { get; set; }

    public string Name { get; set; }

    public IEnumerable<Report> Reports { get; set; }
}

public class ChannelListViewModel
{
    public Guid Id { get; set; }

    public string Name { get; set; }

    public IEnumerable<Report> Reports { get; set; }
}

Using the code below the Reports list is not being mapped. What am I doing wrong?

 IList<ChannelListViewModel> viewModelList = channelList.Select(x => new ChannelListViewModel().InjectFrom(x)).Cast<ChannelListViewModel>().ToList();
NullReference
  • 4,404
  • 12
  • 53
  • 90
  • actually this should work, the value of Reports property should be copied on the other side, because they are of the same name `Reports` and type `IEnumerable`; probably you havent showed the actual viewmodel – Omu Mar 15 '12 at 17:50

2 Answers2

3

No, not by default, you have to use a custom injecter. This is why I switched back to automapper after trying out valueinjecter. How to map lists with ValueInjector

Community
  • 1
  • 1
danludwig
  • 46,965
  • 25
  • 159
  • 237
  • Thanks for the response. So it doesn't handle collections and the method you linked seems to work but what about properties that are collections? – NullReference Mar 14 '12 at 22:22
  • a collection is also a property, and if there is going to be a property with the same name and type at other side then the value is going to be copied – Omu Mar 15 '12 at 17:47
  • oh God... I almost loved VI until found it doesn't support such mega-important feature. It's much easier to write `InjectFrom` extension for AutoMapper than write `CollectionsConvention` for `Injecter`. – Sergey Metlov Feb 27 '13 at 19:35
1

there's an automapper simulation which does that:

http://valueinjecter.codeplex.com/releases/view/60311#DownloadId=318259

you can download it and see how collections are mapped automatically

here's the article: http://valueinjecter.codeplex.com/wikipage?title=Automapper%20Simulation&referringTitle=Home

you can see the unit tests there

Omu
  • 69,856
  • 92
  • 277
  • 407
  • 1
    The AutoMapper simulation doesn't seem to work quite right. It maps collection data incorrectly on objects that have more than one collection. – Mun Oct 22 '13 at 21:58