How to use automapper for ViewModel with IEnumerable <T> property

I am trying to follow Jimmy Bogard's advice for autopilot through an actionfilter (which works great in most cases). But what if I have a custom view model with the collection property that I want to display? For instance,

public class WidgetSearchViewModel { public WidgetSearchOptionsViewModel Options { get; set; } public GenericListPagerViewModel Pager { get; set; } public IEnumerable<WidgetSearchResultModel> Results { get; set; } } 

The Results property comes from the repository as IEnumerable domain objects that I want to convert to WidgetSearchResultModel instances using automapper. The problem is that I will need to create a WidgetSearchViewModel in the controller to populate the Options and Pager properties. How can I use an AutoMapper ActionFilter to populate the Results OnActionExecuted property? Is this possible, or will I need to put the Mapper.Map call into the controller and load all the mappings in my unit tests?

+4
source share
1 answer

Automapper already has built-in support for matching between any nested set that implements IEnumerable. If you define parent-child property mappings, then automapper will display nested collections just fine.

Automapper can also handle displaying anything if you implement CustomTypeConvertor: http://automapper.codeplex.com/wikipage?title=Custom%20Type%20Converters&referringTitle=Home

+2
source

Source: https://habr.com/ru/post/1336069/


All Articles