Skip to content

Commit 19c2b77

Browse files
authored
chore: Merge pull request #914 from DocSvartz/docs-separate-datatypes
Fix docs: split datatype.md to specific types of topics
2 parents 2dcc8a0 + 966589e commit 19c2b77

8 files changed

Lines changed: 220 additions & 196 deletions

File tree

docs/api/Reference.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ uid: Mapster.References
1111
| `src.Adapt<Dest>()` | Mapping to new type | [basic](xref:Mapster.Mapping.BasicUsages) |
1212
| `src.Adapt(dest)` | Mapping to existing object | [basic](xref:Mapster.Mapping.BasicUsages) |
1313
| `query.ProjectToType<Dest>()` | Mapping from queryable | [basic](xref:Mapster.Mapping.BasicUsages) |
14-
| | Convention & Data type support | [data types](xref:Mapster.Mapping.DataTypes) |
14+
| | Convention & Data type support | [data types](xref:Mapster.Mapping.DataTypes.Overview) |
1515

1616
### Mapper instance (for dependency injection)
1717

@@ -84,7 +84,7 @@ uid: Mapster.References
8484
| `BeforeMapping` | Add steps before mapping start | | [before-after](xref:Mapster.Settings.BeforeAfterMapping) |
8585
| `ConstructUsing` | Define how to create object | x | [constructor](xref:Mapster.Settings.ConstructorMapping) |
8686
| `EnableNonPublicMembers` | Mapping non-public properties | | [non-public](xref:Mapster.Settings.Custom.NonPublicMembers) |
87-
| `EnumMappingStrategy` | Choose whether mapping enum by value or by name | | [data types](xref:Mapster.Mapping.DataTypes) |
87+
| `EnumMappingStrategy` | Choose whether mapping enum by value or by name | | [data types](xref:Mapster.Mapping.DataTypes.Primitives) |
8888
| `Fork` | Add new settings without side effect on main config | x | [nested mapping](xref:Mapster.Configuration.NestedMapping) |
8989
| `GetMemberName` | Define how to resolve property name | x | [custom naming](xref:Mapster.Settings.Custom.NamingConvention) |
9090
| `Ignore` | Ignore specific properties | x | [ignore](xref:Mapster.Settings.Custom.IgnoringMembers) |

docs/articles/mapping/Data-types.md

Lines changed: 0 additions & 192 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
uid: Mapster.Mapping.DataTypes.Collections
3+
title: "Mapping - Collections"
4+
---
5+
6+
## Collections
7+
8+
This includes mapping among lists, arrays, collections, dictionary including various interfaces: `IList<T>`, `ICollection<T>`, `IEnumerable<T>`, `ISet<T>`, `IDictionary<TKey, TValue>` etc...
9+
10+
```csharp
11+
var list = db.Pocos.ToList();
12+
var target = list.Adapt<IEnumerable<Dto>>();
13+
```
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
uid: Mapster.Mapping.DataTypes.Overview
3+
title: "Mapping - Mappable Objects"
4+
---
5+
6+
## Mappable Objects
7+
8+
Mapster can map two different objects using the following rules:
9+
10+
- Source and destination property names are the same. Ex: `dest.Name = src.Name`
11+
- Source has get method. Ex: `dest.Name = src.GetName()`
12+
- Source property has child object which can flatten to destination. Ex: `dest.ContactName = src.Contact.Name` or `dest.Contact_Name = src.Contact.Name`
13+
14+
Example:
15+
16+
```csharp
17+
class Staff {
18+
public string Name { get; set; }
19+
public int GetAge() {
20+
return (DateTime.Now - this.BirthDate).TotalDays / 365.25;
21+
}
22+
public Staff Supervisor { get; set; }
23+
...
24+
}
25+
26+
struct StaffDto {
27+
public string Name { get; set; }
28+
public int Age { get; set; }
29+
public string SupervisorName { get; set; }
30+
}
31+
32+
var dto = staff.Adapt<StaffDto>();
33+
//dto.Name = staff.Name, dto.Age = staff.GetAge(), dto.SupervisorName = staff.Supervisor.Name
34+
```
35+
36+
**Mappable Object types are included:**
37+
38+
- POCO classes
39+
- POCO structs
40+
- POCO interfaces
41+
- Dictionary type implement `IDictionary<string, T>`
42+
- Record types (either class, struct, and interface)
43+
44+
Example for object to dictionary:
45+
46+
```csharp
47+
var point = new { X = 2, Y = 3 };
48+
var dict = point.Adapt<Dictionary<string, int>>();
49+
dict["Y"].ShouldBe(3);
50+
```
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
uid: Mapster.Mapping.DataTypes.Primitives
3+
title: "Mapping - Primitive Types"
4+
---
5+
6+
## Primitives
7+
8+
Converting between primitive types (ie. int, bool, double, decimal) is supported, including when those types are nullable. For all other types, if you can cast types in c#, you can also cast in Mapster.
9+
10+
```csharp
11+
decimal i = 123.Adapt<decimal>(); //equal to (decimal)123;
12+
```
13+
14+
## Enums
15+
16+
Mapster maps enums to numerics automatically, but it also maps strings to and from enums automatically in a fast manner.
17+
The default Enum.ToString() in .NET is quite slow. The implementation in Mapster is double the speed. Likewise, a fast conversion from strings to enums is also included. If the string is null or empty, the enum will initialize to the first enum value.
18+
19+
In Mapster, flagged enums are also supported.
20+
21+
```csharp
22+
var e = "Read, Write, Delete".Adapt<FileShare>();
23+
//FileShare.Read | FileShare.Write | FileShare.Delete
24+
```
25+
26+
For enum to enum with different type, by default, Mapster will map enum by value. You can override to map enum by name by:
27+
28+
```csharp
29+
TypeAdapterConfig.GlobalSettings.Default
30+
.EnumMappingStrategy(EnumMappingStrategy.ByName);
31+
```
32+
33+
## Strings
34+
35+
When Mapster maps other types to string, Mapster will use `ToString` method. And whenever Mapster maps string to the other types, Mapster will use `Parse` method.
36+
37+
```csharp
38+
var s = 123.Adapt<string>(); //equal to 123.ToString();
39+
var i = "123".Adapt<int>(); //equal to int.Parse("123");
40+
```

0 commit comments

Comments
 (0)