| Muhammad Waseem

About Newsletter
Books
30 .NET Tips
Sponsorship
Mapster in .NET Core for Mappings
Mar 31, 2023
5 min read

Mapster in .NET Core for Mappings

Sponsor this Newsletter

Why we need Mappers

While working with .NET we often need to convert the different objects (Database objects to DTO’s/Models or vice versa). That’s where mappers come in action.

In the world of mapping we have multiple libraries but these two has got big following and usage .

  1. Automapper (395M Downloads as per article published date)
  2. Mapster (12M Downloads as per article published date)

I have already covered the Automapper in a different post you can check here , let’s see how to use Mapster in this episode.

Some Benefits

Before moving forward here are the benefits of Mapster

  1. Performance wise it is better than Automapper (Memory and Time)

  2. Easy to use because we are mostly playing with adapt method.

  3. Comes with variety of mapping scenarios.

Installation

Install Mapster with the NuGet CLI

Install-Package Mapster

How to use

It is quite simple to use Mapster using adapt method of library , suppose we have DTO(or Model whatever you name it) and Entity (Supposed to be a database class)

public class StudentDTO
{
    public int StudentId { get; set; }
    public string Name { get; set; } = string.Empty;
    public string Email { get; set; } = string.Empty;
}

public class StudentEntity
{
    public int StudentId { get; set; }
    public string Name { get; set; } = string.Empty;
    public string Email { get; set; } = string.Empty;
}

We have two ways to achieve this

  1. Use .Adapt method directly in desired class
  2. Create a static class that contains all Mappings (I am using this)
public static class Mappings
{
    public static StudentEntity MapStudentDtoToEntity(StudentDTO dto)
    {
        var entity = dto.Adapt<StudentEntity>();
        return entity;
    }

    public static StudentDTO MapStudentEntityToDto(StudentEntity entity)
    {
        var dto = entity.Adapt<StudentDTO>();
        return dto;
    }
}

// Use it like this in your desired class
var studentEntity = Mappings.MapStudentDtoToEntity(studentDTO);

While working with database you can use Mapster via Queryable as well , it has ProjectToType method

using (SomeDbContext context = new SomeDbContext())
{
    var students = context.Students
                          .ProjectToType<StudentDto>()
                          .ToList();
}

You can add your custom mappings as well by creating an extension method of IServiceCollection and then by adding its configuration in Program.cs

public static class MappingConfig
{
    public static void RegisterMappings(this IServiceCollection services)
    {
        TypeAdapterConfig<TeacherDTO, TeacherEntity>
            .NewConfig()
            .Map(dest => dest.Id, src => src.TeacherId)
            .Map(dest => dest.TeacherName, src => src.Name)
            .Map(dest => dest.TeacherEmail, src => src.Email);
    }
}

// In Program.cs
builder.Services.RegisterMappings();

Key Points

Here are some other points about Mapster :

  1. If you want to ignore some properties then use .Ignore while mapping.
  2. For two ways mappings use .TwoWays
  3. For further details check here at official Github Repo of Mapster

This article was originally published at https://mwaseemzakir.substack.com/ on Mar 31, 2023 .

Whenever you're ready, there are 3 ways I can help you:

  1. Subscribe to my youtube channel : For in-depth tutorials, coding tips, and industry insights.
  2. Promote yourself to 9,000+ subscribers : By sponsoring this newsletter
  3. Patreon community : Get access to all of my blogs and articles at one place
Previous Next

Subscribe to Newsletter

Join 9,000 Software Engineers

Buy Me a Coffee

Enjoy my articles? Support me by buying a coffee!

Buy Me a Coffee

Muhammad Waseem

Resources
  • Books
  • Courses
Newsletter
  • Articles
  • Sponsorship
Books
  • 30 .NET Tips
  • 100 .NET Tips (Soon)
Author
  • About Us
  • Contact Us
Policy
  • Privacy Policy
  • Terms and Conditions
Interview
  • C# & .NET
  • Web API

Join my .NET newsletter and stay updated!

© 2025 Muhammad Waseem. All rights reserved.