Using Automapper in .NET Core Console Application
In today’s post, we will see how to Use Automapper in Console Application based on C# .NET for mapping source and target objects easily.
We will be using an approach of DI (dependency injection). We shall use Generic HostBuilder to create a tiny IoC DI Container and inject the required dependency including Automapper within the Console application.
Today in this article, we will cover below aspects,
Why use AutoMapper?
- Mapping is a great technique to centralize your mapping logic and use it as many times as needed anywhere in your code.
- Better controlled mapping helps you map only the properties that are needed.
- Reduces Code duplication
- Easy maintenance of code
Getting started
Create a .NET Core Console application
Add below Nuget packages,
PM> Install-Package Microsoft.Extensions.Hosting -Version 5.0.0
Note: Please use the latest available version
For References: Dependency Injection in Console application using the Generic HostBuilder
Step 1- Register Automapper in DI Container
Register Automapper in IoC Container using AddAutoMapper .This can be easily done using Generic HostBuilder as below,
var builder = Host.CreateDefaultBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddAutoMapper(typeof(MyApplication).Assembly);
services.AddTransient<MyApplication>();
}).UseConsoleLifetime();
var host = builder.Build();
Here is the complete code,
var builder = Host.CreateDefaultBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddAutoMapper(typeof(MyApplication).Assembly);
services.AddTransient<MyApplication>();
}).UseConsoleLifetime();
var host = builder.Build();
using (var serviceScope = host.Services.CreateScope())
{
var services = serviceScope.ServiceProvider;
try
{
var myService = services.GetRequiredService<MyApplication>();
var result = await myService.Run();
Console.WriteLine(result);
}
catch (Exception ex)
{
Console.WriteLine("Error Occured");
}
}
Step 2- Configuring Profile Instances
Profiles let you organize all mapping configurations in one place. Here you need to create a class that inherits from the class Profile then please add your configuration of mapping into the Constructor.
Here is a sample implementation,
Step3 – Automapper via Constructor DI
Once IMapper is registered in the IoC as above, it can be injected from the Constructor of any class.
IMapper interface will be available to use in every class/module of your console application. Lifetime management of injected IMapper instances will be done through the IoC Service container itself.
Now IMapper instance can be used for mapping the required source and destination object as required.
I have below two basic types called Source and Destination as an example,
public class Source
{
public string FirstName { get; set; }
public string Address { get; set; }
public string Id { get; set; }
}
public class Destination
{
public string Name { get; set; }
public string Address { get; set; }
public string Identity { get; set; }
}
The above types are unrelated and have a different property name defined.
Lets now Map Source object to the Destination object as below,
That’s all, your logic is ready to Map the Source object to the Destination object as below,
Below is the complete sample implementation,
References :
Do you have any comments or ideas or any better suggestions to share?
Please sound off your comments below.
Happy Coding !!
Summary
In this article, we looked at how to use Automapper in a .NET Core Console application and map the source objects to target objects of different types. We used Generic HostBuilder to create a tiny IoC DI Container and injected the Automapper within the Console application.
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.