find and solve ||
Please wait.....

How to Implement Automapper in ASP.NET Core

AutoMapper is a simple library that helps us to transform one object type into another object. It is based on object-to-object mapping that requires very little configuration. Add the main AutoMapper install package from NuGet. Add the AutoMapper Dependency Injection Package to our project solution via NuGet.

Automapper object-to-object, convention-based mapping library that doesn't need too much configuration it is a simple library that helps us to transform one object type into another (ViewModel to EntityModel or EntityModel to ViewModel).

This article presents a discussion about AutoMapper, why it is useful in core,

In the article, I am going to explain how we can use AutoMapper in our ASP.NET Core application. After that, you will learn about the usage guidelines and best practices and also take a look at what’s happening behind the scenes and how to flatten complex object models.

What is AutoMapper?

AutoMapper is a simple library that helps us to map one object type to another. It is a convention-based object-to-object mapper that requires a very little configuration in core razor syntax. 

The object-to-object mapping works by transforming an input object of one type into an output object of a different type.

Why do we need to use AutoMapper?

When you have to map two different objects (View and Entity Model)  you can use AutoMapper to map Entity properties to ViewModel properties or ViewModel properties to Entity properties. One of the problems is while building the application you have some Entities which are bound in ViewModel and displayed on the view page. So you have to map those Entities to the ViewModel.

How AutoMapper work with ASP.NET Core?

AutoMapper internally uses a great concept of programming called Reflection. The C# Reflection is used to retrieve metadata on types at runtime With the help of Reflection, you can dynamically get a type of existing object and invoke its methods or access its fields and properties. we can read more about Reflection.

Dapper provides some methods that make it easy to communicate with your database. Some of the methods are as given below.

  • Execute:  An extension method that you use to execute a command and return the affected rows. It’s usually used to perform INSERT, UPDATE, and DELETE operations.
  • Query: In this tension method you can execute a query and map the result which is used to fetch multiple objects from the database.
  • QueryFirst: This method executes a query and maps the first result that matches the parameters in the query. This is used when you need just one item that matches the provided specifications.
  • QueryFirstOrDefault: This method is like QueryFirst but returns a default value if the sequence contains no elements.
  • QuerySingle: An extension method that can execute a query and map the result. It throws an exception if there is not exactly one element in the sequence.
  • QuerySingleOrDefault: This method works like QuerySingle but returns a default value if no item is returned from the database.
  • QueryMultiple: An extension method that executes multiple queries within the same command and map results

Step 1.

Create New Applications in the visual studio

  • Open your visual studio 2019 or greater than 2019
  • Click on Create a New Project
  • Select ASP.NET Core Web Application and then Next
  • Provide a Project name and confirm or change the Location. Select Create
  • Select the latest version of ASP.NET Core in the drop-down (.NET Core 5.0 or grater then)and then select Web Application
  • Under Authentication, select Change and set the authentication to Individual User Accounts, and then click on Create button

Step 2.

Mahira  khanna

Mahira khanna

I have the skills you need for you company blog, website, or other content materials

If felt valuable to you, feel free to share it.


Report Response