DEV Community

Juarez Júnior for Develop4Us

Posted on • Edited on

2

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

Image of Quadratic

Free AI chart generator

Upload data, describe your vision, and get Python-powered, AI-generated charts instantly.

Try Quadratic free

Top comments (0)

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay