Home United States USA — software Comparison of Object Mapper Libraries

Comparison of Object Mapper Libraries

215
0
SHARE

In this article, we compare AutoMapper, AgileMapper, and Mapster in C# .NET to solve writing mundane code when assigning values between objects.
Join the DZone community and get the full member experience. In this article, we look at object-to-object mappers in C#. NET. Mappers solve the common problem of writing mundane and repetitive code when assigning values between objects. Three different mapping libraries are evaluated to give an idea of common themes and some differences which might be relevant for your development needs. The mapping libraries AutoMapper, AgileMapper, and Mapster have been used in a simple demo program to explore some simple mapping scenarios. If this is a new area for you then hopefully you’ll gain some knowledge from reading this article. Or perhaps you have been using the same object mapper for some time; should you consider an alternative? Object to object mapping is the process of assigning values from one object to another. This is common in layered architectures, particularly those developed using an ORM framework such as Entity Framework Core. There can be many instances in application code where there is a requirement to translate data between the underlying entity model classes and data transfer objects (DTOs). Another case where object mapping is used is between DTOs, typically from a Web API to view model classes in MVC web application. Mapping code is quite mundane and repetitive to write and a well-adapted solution is to use a mapping library. Mapping libraries generally work by matching members in objects and through object graphs by using a set of conventions based on name and in some cases type too. Solving the object-to-object mapping problem with a mapping library will make your life as a developer easier. Here are some direct benefits: In this post each of the mapping libraries are compared for the following: For this evaluation, a C#. Net Core 5 console application was used using EntityFramework Core and SqlLite database. It is a simplified model of a layered architecture and available on GitHub. The project contains a simple service interface that provides a few basic scenarios to test such as a single-level object to object mapping, a two-level object mapping with an object containing a collection, and a reverse mapping. In the project code, the interface is implemented for each mapping solution. Also for good measure, the interface was implemented without using a mapper. This is the mundane code that we wish to remove by using a mapping library! Mapping can get quite in-depth so not all use cases are covered, but this simple project is available and can be further extended to try out different things. Three object mapping libraries were compared: AutoMapper, AgileMapper, and Mapster. There is a range of libraries to use, but here we have a very popular, quite popular, and less well-known object mapping library for comparison. This is our current object mapper of choice. We decided to use AutoMapper on some recent projects due to its existing popularity and its use in Jon P Smith’s practical book Entity Framework Core in Action. AutoMapper takes a low-configuration approach and is designed to be used in dependency injection scenarios such as in ASP. Net Core applications. AutoMapper demands a configuration that can be divided into profiles and validated. The library can perform a scanning operation to include all profiles when the configuration is built. It creates an expression tree that can be inspected during debugging using an extension to view.

Continue reading...