In this comprehensive guide, we will analyze the AutoMapper prototype in C# with the aid of examples.
What is AutoMapper in C#?
In C#, the act of mapping the properties of one object to another is made easy by the open-source package of .NET known as “AutoMapper”. By automatically matching characteristics with similar names or by enabling explicit mapping configurations, it reduces the requirement for manual property mapping. With the help of AutoMapper, object mapping should become more easy, effective, and more readable.
Example: Use of AutoMapper Library in C# Program
To implement the AutoMapper library in C#, first, users need to install the “Install-Package AutoMapper” package in their compiler. Then, add the following header files at the top of the code file:
using AutoMapper;
Here:
- “using System” integrates the System namespace, and it provides the essential types and functionalities of the C#.
- “using AutoMapper” imports the namespace for AutoMapper. It is known as the well-liked object-to-object mapping package in C#.
Next, define a “Person” class that has properties(contains personal data of string type, such as “Fist_Name”, “Last_Name”, “person_Address”, “person_Email” and integer type “total_Age”). Each property specifies a public access modifier, “get”, and “set” methods that tell the default behavior for getting and setting their values:
{
public string First_Name { get; set; }
public string Last_Name { get; set; }
public int total_Age { get; set; }
public string person_Address { get; set; }
public string person_Email { get; set; }
}
Next, define a second class “PersonData” having fewer-property that the subset of the “Person” class, such as “Name”,”total_Age”,”person_Address”, and “person_Email”:
{
public string Name { get; set; }
public int total_Age { get; set; }
public string person_Address { get; set; }
public string person_Email { get; set; }
}
After this, the class “Program” is used in the “main()” function to start the program execution.
AutoMapper is set up for mapping the properties associated with the “Person” class and the properties of the class “PersonData” inside the “main()” function. Then, configure a mapping setup between the two classes by using the “CreateMap” function. After that, the properties “First_Name” and “Last_Name” map from the “Person” class to the “Name” of the “PersonData” property. Next, use the method “ForMember()” to combine the “First_Name” and “Last_Name” properties to alter the association of the “Name” property. Then, create the object of the class “Person” using the sample data. Next, use the “Mapper.Map()” function to implement the mapping from the class “Person” according to the already mentioned configuration. Now, save the result in the “PersonData” variable.
Finally, use “Console.WriteLine()” to display the original instance of the “Person” class that is mapped with the “PersonData” class object:
{
public static void Main()
{
Mapper.CreateMap<Person, PersonData>().ForMember(dest=>dest.Name, a => a.MapFrom(src => string.Concat(src.First_Name," ", src.Last_Name)) );
var person = new Person { First_Name="Team" , Last_Name = "Sam", total_Age=23, person_Email = "[email protected]", person_Address="place Address here" };
var PersonData = Mapper.Map<PersonData>(person);
Console.WriteLine("\nperson.First_Name : {0} \nperson.Last_Name : {1} \nperson.total_Age : {2} \nperson.person_Address : {3} \nperson.Email : {4}", person.First_Name, person.Last_Name, person.total_Age, person.person_Email, person.person_Address);
Console.WriteLine("\npersonData.Name : {0} \npersonData.Age : {1} \nperson.Address : {2} \nperson.Email : {3}", PersonData.Name, PersonData.total_Age, PersonData.person_Email, PersonData.person_Address);
}
}
Output
That’s it! We have explained the AutoMapper library in C#.
Note: Use this tool to compile and test the above-given code example as it already has the AutoMapper Package.
Conclusion
In C#, AutoMapper is an effective library used in many applications to simplify the technique of object mapping. It decreases code duplication, increases maintainability, and boosts developer productivity by simplifying the mapping process. This guide discussed the AutoMapper in C#.