Techiehook Techiehook
Updated date May 09, 2024
We will learn about AutoMapper and how to use it in C# through a sample program, including its output

What is AutoMapper?

AutoMapper is a library in .NET that simplifies the process of mapping objects from one type to another type. This is useful in scenarios where you have different classes representing similar data but with different properties. AutoMapper helps reduce the amount of repetitive code needed for these mappings.

Here is a step-by-step guide to installing AutoMapper in a .NET project:

  1. Install via NuGet Package Manager: We can install AutoMapper via NuGet Package Manager in Visual Studio. The steps are,

    • Right-click on your project in Visual Studio Solution Explorer.
    • Select "Manage NuGet Packages..."
    • In the NuGet Package Manager, search for "AutoMapper".
    • Select the AutoMapper package from the search results.
    • Click on "Install" and follow the prompts to complete the installation.
  2. Configure AutoMapper: Once you have AutoMapper installed, you need to configure it in your project. Typically, this involves setting up mappings between source and destination types. You can do this in your application startup code or in any other appropriate place. We will see how you can configure AutoMapper in console application:

How to Use AutoMapper in C#?

Step 1:

Define the Employee1 and Employee2 classes.

public class Employee1
{
    public string Name { get; set; }
    public string Age { get; set; }
    public string Position { get; set; }
    public string Country { get; set; }
}

public class Employee2
{
    public string Name { get; set; }
    public string Age { get; set; }
    public string Position { get; set; }
    public string Country { get; set; }
}

Step 2:

Configure AutoMapper in the ConfigureAutoMapper method.

static void ConfigureAutoMapper()
{
	Mapper.Initialize(cfg =>
	{
		cfg.CreateMap<Employee1, Employee2>();
	});
}

Step 3:

Create an instance of Employee1 with sample data.

var employee1 = new Employee1
{
	Name = "Raj",
	Age = "30",
	Position = "Developer",
	Country = "India"
};

Step 4:

Use AutoMapper to map Employee1 to Employee2.

var employee2 = Mapper.Map<Employee2>(employee1);

Step 5:

Display the mapped Employee2 object in the console.

Console.WriteLine("Mapped/Updated Employee2 Object:");
Console.WriteLine($"Name: {employee2.Name}");
Console.WriteLine($"Age: {employee2.Age}");
Console.WriteLine($"Position: {employee2.Position}");
Console.WriteLine($"Country: {employee2.Country}");

Entire Program and Output:

using System;
using AutoMapper;

public class Employee1
{
    public string Name { get; set; }
    public string Age { get; set; }
    public string Position { get; set; }
    public string Country { get; set; }
}

public class Employee2
{
    public string Name { get; set; }
    public string Age { get; set; }
    public string Position { get; set; }
    public string Country { get; set; }
}

public class Program
{
    static void Main(string[] args)
    {
        // Configure AutoMapper
        ConfigureAutoMapper();

        // Create an instance of Employee1
        var employee1 = new Employee1
        {
            Name = "Raj",
            Age = "30",
            Position = "Developer",
            Country = "India"
        };

        // Map Employee1 to Employee2
        var employee2 = Mapper.Map<Employee2>(employee1);

        // Display the mapped Employee2 object
        Console.WriteLine("Mapped/Updated Employee2 Object:");
        Console.WriteLine($"Name: {employee2.Name}");
        Console.WriteLine($"Age: {employee2.Age}");
        Console.WriteLine($"Position: {employee2.Position}");
        Console.WriteLine($"Country: {employee2.Country}");

        Console.ReadLine();
    }

    static void ConfigureAutoMapper()
    {
        // Initialize
        Mapper.Initialize(cfg =>
        {
            cfg.CreateMap<Employee1, Employee2>();
        });
    }
}

The output will be the mapped Employee2 object after it's been mapped from Employee1. Since both Employee1 and Employee2 have the same properties with the same values.

Output:

Mapped/Updated Employee2 Object:
Name: Raj
Age: 30
Position: Developer
Country: India

ABOUT THE AUTHOR

Techiehook
Techiehook
Admin, Australia

Welcome to TechieHook.com! We are all about tech, lifestyle, and more. As the site admin, I share articles on programming, tech trends, daily life, and reviews... For more detailed information, please check out the user profile

https://www.techiehook.com/profile/alagu-mano-sabari-m

Comments (0)

There are no comments. Be the first to comment!!!