[non professional translator] Mapster - data type

Posted by daniel244rock on Tue, 25 Jan 2022 14:07:14 +0100

[non professional translator] Mapster - data type

Coupon website www.cps3.com cn

Series introduction

[non professional translation] is a series of blogs that translate no Chinese documents. The articles are composed of machine translation and the translator's own understanding. They are all different from the original text, but the meaning is basically the same.

Due to limited personal ability, please correct any mistakes and forgive me.

text

This article explains the data types in Mapster

Basic type

Conversion of basic types, such as int/bool/dobule/decimal, including nullable basic types.

As long as C# supports the type of type conversion, conversion is also supported in Mapster.

decimal i = 123.Adapt<decimal>(); //equal to (decimal)123;

Enumeration type

Mapster automatically maps enumerations to numeric types. It also supports string to enumeration and enumeration to string mapping.

The. NET default implementation of enumeration / string conversion is very slow, and Mapster is faster than NET is twice as fast as the default implementation.

In Mapster, the string is converted to enumeration. If the string is empty or empty, the enumeration will be initialized to the first enumeration value.

In Mapster, tag enumeration is also supported.

var e = "Read, Write, Delete".Adapt<FileShare>();  
//FileShare.Read | FileShare.Write | FileShare.Delete

For different types of enumerations, Mapster maps values to enumerations by default. The EnumMappingStrategy method can be called to specify the enumeration mapping method, such as:

TypeAdapterConfig.GlobalSettings.Default
    .EnumMappingStrategy(EnumMappingStrategy.ByName);

String type

In Mapster, when mapping other types to strings, Mapster will call the ToString method of type.

If you map a string to a type, Mapster calls the Parse method of the type.

var s = 123.Adapt<string>(); // Equivalent to: 123 ToString();
var i = "123".Adapt<int>();  // Equivalent to: int.Parse("123");

aggregate

Including the mapping between lists, arrays, sets, dictionaries including various interfaces: IList < T >, icollection < T >, IEnumerable < T >, Iset < T >, idictionary < tkey, tvalue >, etc

var list = db.Pocos.ToList();
var target = list.Adapt<IEnumerable<Dto>>();  

Mappable object

Mapster can map two different objects using the following rules

  • The source type and target type attribute names are the same. For example: dest Name = src. Name
  • The source type has a GetXXXX method. For example: dest Name = src. GetName()
  • The source type attribute has sub attributes, which can be assigned to qualified target type attributes, such as dest ContactName = src. Contact. Name or dest Contact_ Name = src. Contact. Name

Example:

class Staff {
    public string Name { get; set; }
    public int GetAge() { 
        return (DateTime.Now - this.BirthDate).TotalDays / 365.25; 
    }
    public Staff Supervisor { get; set; }
    ...
}

struct StaffDto {
    public string Name { get; set; }
    public int Age { get; set; }
    public string SupervisorName { get; set; }
}

var dto = staff.Adapt<StaffDto>();  
//dto.Name = staff.Name, dto.Age = staff.GetAge(), dto.SupervisorName = staff.Supervisor.Name

Mappable object types include:

  • class
  • structural morphology
  • Interface
  • Dictionary type that implements the idictionary < string, t > interface
  • Record type (class, structure, interface)

Examples of converting objects to dictionaries:

var point = new { X = 2, Y = 3 };
var dict = point.Adapt<Dictionary<string, int>>();
dict["Y"].ShouldBe(3);

Example of Record type:

class Person {
    public string Name { get; }
    public int Age { get; }

    public Person(string name, int age) {
        this.Name = name;
        this.Age = age;
    }
}

var src = new { Name = "Mapster", Age = 3 };
var target = src.Adapt<Person>();

Automatic mapping of Record types has some limitations:

  • Record type property must have no set
  • There is only one non empty constructor
  • All parameter names in the constructor must be the same as the property name

If the above rules are not met, additional MapToConstructor configuration needs to be added