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.
We will create an ASP.NET Core MVC Web Application template by going to File
| New
| Project
| AspNet Core WebApplication
.
- First, let's add the
AutoMapper
dependency to the project. - Next, we will create the
ProductRepository
with fake data. OurHomeController
will consume the data from this repository. The objects consumed from the repository will beProductDto
objects, but we will use them only to transport data between the repository and the controller:
public class ProductDataStore { public static ProductDataStore Current...