diff --git a/docs/articles/mapping/Data-types.md b/docs/articles/mapping/Data-types.md index f4951ed4..edc0c163 100644 --- a/docs/articles/mapping/Data-types.md +++ b/docs/articles/mapping/Data-types.md @@ -94,6 +94,77 @@ var dict = point.Adapt>(); dict["Y"].ShouldBe(3); ``` +## Record types + +>[!IMPORTANT] +> Mapster treats Record type as an immutable type. +> In this regard, only a with-like non-destructive mutation is available. +> +> ```csharp +> var result = source.adapt(data) +>//equal var result = data with { X = source.X.Adapt(), ...} +>``` + +### Features and Limitations: + +# [v10.0](#tab/Records-v10) + +>[!NOTE] +> By default, all [C# Records](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/record) are defined as a record type. +> Limitations by count of constructors and constructor parameters used in Mapster version 7.4.0 do not apply. + + +#### Using default value in constuctor param + +If the source type does not contain members that can be used as constructor parameters, then will be used the default values ​​for the parameter type. + +Example: + +```csharp + +class SourceData +{ + public string MyString {get; set;} +} + +record RecordDestination(int myInt, string myString); + +var result = source.Adapt() + +// equal var result = new RecordDestination (default(int),source.myString) + +``` + +#### MultyConsturctor Record types + +If there is more than one constructor, by default, mapping will be performed on the constructor with the largest number of parameters. + +Example: + +```csharp +record MultiCtorRecord +{ + public MultiCtorRecord(int myInt) + { + MyInt = myInt; + } + + public MultiCtorRecord(int myInt, string myString) // This constructor will be used + : this(myInt) + { + MyString = myString; + } + +} +``` + +# [v7.4.0](#tab/Records-v7-4-0) + +>[!NOTE] +>Record type must not have a setter and have only one non-empty constructor, and all parameter names must match with properties. + +Otherwise you need to add [`MapToConstructor` configuration](xref:Mapster.Settings.ConstructorMapping#map-to-constructor). + Example for record types: ```csharp @@ -110,5 +181,12 @@ class Person { var src = new { Name = "Mapster", Age = 3 }; var target = src.Adapt(); ``` +--- + +### Support additional mapping features: -There are limitations to map Record type automatically. Record type must not have a setter and have only one non-empty constructor, and all parameter names must match with properties. Otherwise you need to add [`MapToConstructor` configuration](xref:Mapster.Settings.ConstructorMapping#map-to-constructor). +| Mapping features | v7.4.0 | v10.0 | +|:-----------------|:------:|:-----:| +|Custom constructor mapping| - | ✅ | +|Ignore| - | ✅ | +|IgnoreNullValues| - | ✅ | \ No newline at end of file