DEV Community

Aman Singh Parihar
Aman Singh Parihar

Posted on

4 2

Automapper in .NET Core

GitHub link for the project:

GitHub logo aman-singh-parihar / Automapper

Automapper in .NET Core

We will create a project in .NET Core using dotnet cli and will use visual studio code as the IDE.

Go to any directory where you want to create this project.

We are using powershell to setup this project.

PS E:/VSCODE/NETProjects>mkdir Automapper
PS E:/VSCODE/NETProjects>cd Automapper

Lets create a solution in Automapper folder.

PS E:/VSCODE/NETProjects>Automapper>dotnet new sln

Lets create a project in the Automapper folder.

PS E:/VSCODE/NETProjects>Automapper>dotnet new webapi -o Automapper.API

Lets add the project in the solution file because as of now they are disconnected to each other.

dotnet sln add Automapper.API/Automapper.API.csproj

Now lets open Automapper folder in vscode.

PS E:/VSCODE/NETProjects>Automapper>code .

Switch to project folder and install automapper.

PS E:/VSCODE/NETProjects>Automapper>cd Automapper.API

PS E:/VSCODE/NETProjects>Automapper>Automapper.API>dotnet add package AutoMapper.Extensions.Microsoft.DependencyInjection

We setup our project, now lets write some code. First of all delete the weather api controller and weather api class as we don't need this.

Lets create two folders Models and DTO.
Models will have classes that will exactly match with the database tables and DTO will have classes which need to pass to the client.

In our case we are using Employee class in Models folder and EmployeeDTO in DTO folders.

Employee.cs

using System.Collections.Generic;
public class Employee
{
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public int Salary { get; set; }
public static IEnumerable<Employee> SetupEmployee()
{
return new List<Employee>()
{
new Employee(){EmployeeId = 1, EmployeeName ="First", Salary=10000},
new Employee(){EmployeeId = 2, EmployeeName ="Second", Salary=20000},
new Employee(){EmployeeId = 3, EmployeeName ="Third", Salary=30000},
new Employee(){EmployeeId = 4, EmployeeName ="Fourth", Salary=40000},
new Employee(){EmployeeId = 5, EmployeeName ="Fifth", Salary=50000}
};
}
}
view raw Employee.cs hosted with ❤ by GitHub

EmployeeDTO.cs

public class EmployeeDTO
{
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
}
view raw EmployeeDTO.cs hosted with ❤ by GitHub

You can see that we are not passing employee salary to the client.

Now we will create profile which will map from Employee to EmployeeDTO.

Lets create a folder AutoMapperProfile in the project and add a class AutomapperProfile.cs.

AutomapperProfile.cs

using AutoMapper;
public class AutomapperProfile: Profile
{
public AutomapperProfile()
{
//Source to destination.
CreateMap<Employee,EmployeeDTO>();
}
}

Lets setup the ConfigureServices method of startup class for automapper.

Startup.cs

public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddAutoMapper(typeof(Startup));
}
}
view raw Startup.cs hosted with ❤ by GitHub

Now lets create a EmployeeController in Controllers folder.

EmployeeController.cs

using System.Collections.Generic;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
[Route("api/[controller]")]
[ApiController()]
public class EmployeeController : ControllerBase
{
private readonly IMapper _mapper;
public EmployeeController(IMapper mapper)
{
_mapper = mapper;
}
[HttpGet]
public IEnumerable<EmployeeDTO> GetEmployees()
{
/*
Assume it to be a service call/database call
it returns a list of employee, and now we will map it to EmployeeDTO
*/
var employees = Employee.SetupEmployee();
var employeeDTO = _mapper.Map<IEnumerable<EmployeeDTO>>(employees);
return employeeDTO;
}
}

We are almost done with this, lets run this project and see the output.


PS E:/VSCODE/NETProjects>Automapper>Automapper.API>dotnet run

Type this url in the browser to view the output: https://localhost:5001/api/Employee

[
{
"employeeId": 1,
"employeeName": "First"
},
{
"employeeId": 2,
"employeeName": "Second"
},
{
"employeeId": 3,
"employeeName": "Third"
},
{
"employeeId": 4,
"employeeName": "Fourth"
},
{
"employeeId": 5,
"employeeName": "Fifth"
}
]
view raw employee.json hosted with ❤ by GitHub

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay