Automapper map Array to Object with example
Today in this article, we will see how to perform Automapper map Array to Object 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 or 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.
What is AutoMapper
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.
We shall be using the below classes where we have source type as “CheckingAccountSource” and destination as “CheckingAccountDestination”.
I have used the class naming convention as simple for understanding purposes.
Below is our sample source JSON file,
{
"ID": "Bank of ABCD",
"Branches": [
{
"Address": [
{
"StreetName": "address1",
"ZipCode": "Zip",
"ST": "USA"
},
{
"StreetName": "address1",
"Zip": "Zip",
"ST": "USA"
}
]
}
]
}
Below is the expected destination JSON output after mapping file,
{
"Bank": "Bank of ABCD",
"BranchInfo": [
{
"Location":
{
"Street": "address1",
"Zip": "Zip",
"State": "USA"
}
}
]
}
Let’s define the Source and destination classes as below. here below our aim is to map the array from source to the object.
So nee here is to map only the first element from the array of objects to the destination object.
In the above example, We have a very simple one-to-one mapping which can be achieved easily by creating the below Mapping profile.
DI IMapper into the module
Dependency Injection of the IMapper interface is explained here already with more details,
Automapper map Array to Object – 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 the best way of organizing all your mapping configurations in one place.
Below is 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.BranchInfo, o => o.MapFrom(src => src.Branches));
CreateMap<Branches, BranchInfo>()
.ForMember(dest => dest.Location, o => o.MapFrom(src => src.Address[0]));
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);
}
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 an array of objects from the source to an array of objects using an auto mapper.
Useful 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.