DEV Community

Juarez Júnior for Develop4Us

Posted on • Edited on

Fast and Efficient Object Mapping with Mapster

Mapster is an object mapping library for .NET that offers high performance and flexibility, similar to AutoMapper but with lower overhead and more control. It allows fast conversion between different types of objects, useful when you need to map entity classes to DTOs (Data Transfer Objects) or vice versa. Mapster is lightweight and easy to use, supporting simple, complex mappings and custom projections. In this example, we’ll configure Mapster to map data from an entity to a DTO.

Libraries:

To use the Mapster library, install the following NuGet package in your project:

Install-Package Mapster
Enter fullscreen mode Exit fullscreen mode

Example Code:

using Mapster;
using System;

namespace MapsterExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Creating an instance of the entity Product
            var product = new Product
            {
                Id = 1,
                Name = "Laptop",
                Price = 3500.99M
            };

            // Configuring Mapster
            MapsterConfig.ConfigureMappings();

            // Mapping to the ProductDTO using Mapster
            var productDto = product.Adapt<ProductDTO>();

            // Displaying the mapped data in the console
            Console.WriteLine($"ProductDTO: Id={productDto.Id}, Name={productDto.Name}, PriceFormatted={productDto.PriceFormatted}");
        }
    }

    // Entity class Product
    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
    }

    // DTO class ProductDTO
    public class ProductDTO
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string PriceFormatted { get; set; }
    }

    // Configuring the mapping to format the price
    public class MapsterConfig
    {
        public static void ConfigureMappings()
        {
            TypeAdapterConfig<Product, ProductDTO>.NewConfig()
                .Map(dest => dest.PriceFormatted, src => src.Price.ToString("C2"));
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Code Explanation:

In this example, we create an entity class Product and a DTO class ProductDTO. Mapster is used to automatically map the data from Product to ProductDTO. In the mapping, the PriceFormatted property is configured to display the price in currency format (C2). The Adapt method from Mapster is used to perform the mapping in a simple and direct way. The result is displayed in the console with formatted data.

Conclusion:

Mapster offers an efficient and flexible solution for object mapping in .NET, providing a high-performance alternative to AutoMapper. Its configuration is simple but allows powerful customizations, making it ideal for converting domain classes to DTOs and vice versa.

Source code: GitHub

Top comments (0)