Creating and using a ViewModel with AutoMapper
In this recipe, you will learn how to create and use ViewModel with AutoMapper. AutoMapper is a convention-based object-to-object mapper library (according to http://automapper.org/). We usually use AutoMapper to create ViewModel, Data Transfer Objects (DTO), and more classes from DataModel classes. AutoMapper makes it very easy to instantiate a class and fill the properties from other classes.
Getting ready
We will create an ASP.NET Core MVC Web Application template by going to File | New | Project | AspNet Core WebApplication.
How to do it...
- First, let's add the
AutoMapperdependency to the project. - Next, we will create the
ProductRepositorywith fake data. OurHomeControllerwill consume the data from this repository. The objects consumed from the repository will beProductDtoobjects, but we will use them only to transport data between the repository and the controller:
public class ProductDataStore
{
public static ProductDataStore Current...