Automapper Map Array to an Array – Part 2

Automapper Map Array to an Array

Today in this article, we will see how to use Automapper Map Array to an Array of a different type.

Today in this article, we will cover below aspects,

To keep it simple, we shall use very simple examples and later look into all possible combinations of the map array to object map object to an array, or even map array to array mapping examples.

We already learned in our article on Getting started with Automapper in ASP.NET Core.

AutoMapper is an object mapper that helps you transform one object of one type into an output object of a different type.

This type of transformation is often needed when you work on business logic.

Automapper helps to centralize your repeated boring mapping logic, address the separation of concerns, provide better control on mapping making your code clean and easy to maintain.

Today in this article we shall see how to map an Array to an array of objects using Automapper.

We shall be using the below classes where we have the source type as “CheckingAccountSource” and the destination as “CheckingAccountDestination”.

I have used the class naming convention as simple for understanding purposes.

Automapper Map Array to an Array Part II

In the above example, We have a very simple one-to-one mapping which can be achieved easily by creating the below Mapping profile.

Dependency injection IMapper into the module

Dependency Injection of the IMapper interface is explained here already with more details,

Automapper Map Array to an Array – Configuring Profile Instances

Here you need to create a class that inherits from the class Profile then please add your configuration of mapping into the Constructor.

Profiles are one of the best ways of organizing all your mapping configurations in one place.

Below is the Automapper profile for the array of object mapping.

public class CheckingAccountProfile : Profile
    {
        public CheckingAccountProfile()
        {
            CreateMap<CheckingAccountSource, CheckingAccountDestination>()
              .ForMember(dest => dest.Bank, o => o.MapFrom(src => src.ID))
              .ForMember(dest => dest.Branch, o => o.MapFrom(src => src.Branches));

            CreateMap<Branches, BranchInfo>()
             .ForMember(dest => dest.Location, o => o.MapFrom(src=>src.Address));

            CreateMap<Address, Location> ()
            .ForMember(dest => dest.Street, o => o.MapFrom(src => src.ST))
            .ForMember(dest => dest.State, o => o.MapFrom(src => src.StreetName))
            .ForMember(dest => dest.Zip, o => o.MapFrom(src => src.ZipCode));

        }

Here is a simplified example to perform Mapping objects to an array of objects.

        [HttpPost]
        public IActionResult UpdateEmployee([FromBody] CheckingAccountSource soureObject)
        {
            var destObject = _mapper.Map<Destination>(soureObject);

            _logger.LogInformation("Mapping successful");

            return Ok(destObject);
        }

Automapper Map Array to an Array

Below are a few additional scenarios, which are covered in the link below,

Scenario 1

Map an array of objects into a single object using an auto mapper. For more details, please visit the below article –

Scenario 2

Map a single object from the source to an array of objects using an auto mapper.

For more details, please visit the below article – Automapper map object to an array of object

Scenario 3

Map an array of objects from the source to an array of objects using an auto mapper.

We have covered this scenario in the current post.

References:

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



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.



Leave a Reply

Your email address will not be published. Required fields are marked *