How to get started with AutoMapper and ASP.NET Core 2

If you were to ask a group of ASP.NET developers to name their most installed and used packages, I’d be surprised if the majority didn’t have AutoMapper on their list.

At the time of writing, AutoMapper on NuGet.org has been downloaded 17,291,772 times and sits at the number 6 spot in the top 100 most downloaded community packages over the last 6 weeks. But what is AutoMapper?

Logo from AutoMapper GitHub

What is AutoMapper?

I’m sure you’ve been in this scenario countless times: you’ve got an ASP.NET web application, and you want to keep the separation between your domain models and your view models. So you write code that allows you to map your domain model into your view model. Then, as you add more views and domain models, you end up writing more mappers. Then you write mappers to map your domain transfer objects from your database layer into your domain objects…you get the point.

This code is mundane and repetitive. And this is where AutoMapper comes in. It’s a convention-based object-to-object mapper that requires little configuration.

AutoMapper is a convention-based object-to-object mapper that requires little configuration.

Here’s how you can get started with AutoMapper.

Getting started with AutoMapper

Step 1: Installing AutoMapper

For this example you’ll need to create an ASP.NET Core 2 web application (I’m using Visual Studio 2017 in Windows).

There are a couple of ways you can install AutoMapper, you can either right-click the web project and select Manage NuGet Packages and then search for and install the AutoMapper.Extensions.Microsoft.DependencyInjection package.

Or you can use the Package Manager Console with the following command:

Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection

By installing the AutoMapper.Extensions.Microsoft.DependencyInjection package, it will automatically install the AutoMapper package for you as well.

Step 2: Configuring AutoMapper

You can now go ahead and configure AutoMapper in your Startup.cs class under the ConfigureServices method:

Let’s create a User Model in the Models folder:

Along with our User Model we also want to create a new ViewModel that will eventually be used to display our User data:

Now we’ve got our User Model and our User ViewModel, how do we tell AutoMapper to map from our User Mode to our User ViewModel? Well, we use a Profile.

Profiles in AutoMapper are a good way of organizing your mapping collections. To create a Profile, we create a new class and inherit from Profile. We can then add our mapping configuration inside our new classes constructor.

Go head and create a UserProfile class to map our User Model to our User ViewModel:

We now have a UserProfile class that creates the mapping between our User Model and User ViewModel. But how does AutoMapper know about our UserProfile class?

Well, if you remember, towards the start of this example we added this line of code to our ConfigureServices method in Startup.cs:

When our application starts up and adds AutoMapper, AutoMapper will scan our assembly and look for classes that inherit from Profile, then load their mapping configurations.

Step 3: AutoMapper in action

Let’s create a new UserController in the Controllers folder:

To use our configured mappings, we need to inject the IMapper interface into the constructor of our UserController class:

As with Profiles, by calling AddAutoMapper in our Startup.cs ConfigureServices method, it’s taken care of registering IMapper for us.

In our Index Action method, let’s create a User object:

We can now use the IMapper interface that we injected into our constructor and call the Map method.

We give the Map method the type we want to map to and the object we would like to map from:

In the code above, I’ve also passed our newly mapped User ViewModel into the view.

Finally — we can now create a view for our controller using our User ViewModel:

Time for the big reveal — run the application and navigate to /user (in my case I went to http://localhost:64877/user):

There you have it — you should be able to see the properties that we used when creating our User have been mapped to our User ViewModel and are now being shown.

Conclusion

With Automapper you can map one object to another throughout your application with just a little configuration.

We’ve just scratched the surface of what AutoMapper has to offer in terms of mapping objects from one to another, but I hope you can see how easy it is to get up and running with it.

I encourage you to continue exploring AutoMappers feature set and usages in the documentation link below.

Resources:

Jimmy Bogard
 AutoMapper
 AutoMapper — Documentation
 NuGet