1024programmer Asp.Net How to use Mapster, the best-performing object mapping framework in .NET

How to use Mapster, the best-performing object mapping framework in .NET

How to use Mapster, the best-performing object mapping framework in .NET

Mapster is an open source .NET object mapping library that provides a simple and powerful way to handle mapping between objects. In this article, I will detail how to use Mapster in .NET and provide some examples and source code.

Performance comparison with other frameworks:

Mapster installation and configuration:

  1. First, open Visual Studio and create a new .NET project.
  2. Run the following command in the NuGet package manager console to install Mapster: Install-Package Mapster.
  3. Add a new class file to the project and name it MappingConfig.cs. This class will be used to configure Mapster mapping rules.

Configure mapping rules:
In the MappingConfig.cs file, add the following code to configure mapping rules:

using Mapster;

 public static class MappingConfig
     public static void Configure()
         //Add mapping rules
             .Map(dest => dest.DestinationProperty, src => src.SourceProperty)
             .Map(dest => dest.AnotherProperty, src => src.AnotherProperty);

In the above code, we first set some global settings of Mapster.
NameMatchingStrategy.Flexible means that the property name is not case-sensitive. PreserveReference(true) means to preserve the reference relationship.

Then, we use the NewConfig method of the TypeAdapterConfig class to create a new mapping rule. In this example, we map the MySource class to the MyDestination class. Use the Map method to specify the mapping relationship between attributes.

Use Mapster for object mapping:
After we configure the mapping rules, we can use Mapster in the code to map between objects. Here’s a simple example:

using Mapster;

 public class MySource
     public string SourceProperty { get; set; }
     public string AnotherProperty { get; set; }

 public class MyDestination
     public string DestinationProperty { get; set; }
     public string AnotherProperty { get; set; }

 public class Program
     static void Main(string[] args)
         //Configure mapping rules
         //Create source object
         var source = new MySource
             SourceProperty = "Hello",
             AnotherProperty = "World"
         //Execute mapping
         var destination = source.Adapt();
         //output result
         Console.WriteLine(destination.DestinationProperty); // Output: Hello
         Console.WriteLine(destination.AnotherProperty); // Output: World

In the above example, we first call the MappingConfig.Configure method to configure the mapping rules. Then, we create a source object source and set its property values. Next, we use the Adapt method to map the source object to the destination object. Finally, we can obtain the mapping result by accessing the properties of the target object.

Advanced usage:
Mapster also provides some advanced usage for handling more complex mapping scenarios.

  1. Ignore attribute mapping:
    Sometimes, we may want to ignore certain attributes during the mapping process. This can be achieved using the Ignore method:
     .Ignore(dest => dest.DestinationProperty);
  1. Custom attribute mapping:
    You can use the MapWith method to customize the mapping logic between attributes:
     .Map(dest => dest.DestinationProperty, src =>src.SourceProperty.ToUpper());
  1. Collection mapping:
    Mapster also supports mapping between collections. For example, if we have a list containing multiple MySource objects, we can use the Adapt method to map them to a list containing multiple MyDestination objects:
var sourceList = new List
     new MySource { SourceProperty = "Hello", AnotherProperty = "World" },
     new MySource { SourceProperty = "Foo", AnotherProperty = "Bar" }

 var destinationList = sourceList.Adapt<List>();
  1. Nested object mapping:
    If the source object and the target object contain nested objects, we can use the MapWith method to handle the mapping of nested objects:
     .Map(dest => dest.NestedObject, src => src.NestedObject.Adapt());

The above is an introduction to the methods, steps and some advanced usage of using Mapster for object mapping. By using Mapster, we can easily handle mapping between objects and customize and extend it as needed.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/811220

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us


Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top