Skip to content

Commit

Permalink
解决冲突
Browse files Browse the repository at this point in the history
  • Loading branch information
yihango committed Jun 21, 2021
2 parents 24512c1 + d929ee6 commit 4c9f324
Show file tree
Hide file tree
Showing 31 changed files with 437 additions and 389 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
# Mapster-docs

[Mapster](https://github.com/MapsterMapper/Mapster) 是一个优秀高效的对象映射框架,由于没有中文文档导致在中国国内使用并不广泛,因此我翻译了 [Mapster](https://github.com/MapsterMapper/Mapster) 的官方文档帮助大家更好的学习和使用这个框架。

> 因个人能力有限,如有谬误之处还请指正,多多包含
[Mapster github repo](https://github.com/MapsterMapper/Mapster)

## 文档列表

[Mapster 官方英文文档](https://github.com/MapsterMapper/Mapster/wiki)

[Mapster wiki](https://github.com/MapsterMapper/Mapster/wiki)

[中文文档](cn/README.md)


## Documents
## LICENSES
![GitHub](https://img.shields.io/github/license/rivenfx/Modular?color=brightgreen)
[![Badge](https://img.shields.io/badge/link-996.icu-%23FF4D5B.svg?style=flat-square)](https://996.icu/#/zh_CN)
[![LICENSE](https://img.shields.io/badge/license-Anti%20996-blue.svg?style=flat-square)](https://github.com/996icu/996.ICU/blob/master/LICENSE)

Please note: once the use of the open source projects as well as the reference for the project or containing the project code for violating labor laws (including but not limited the illegal layoffs, overtime labor, child labor, etc.) in any legal action against the project, the author has the right to punish the project fee, or directly are not allowed to use any contains the source code of this project!


[中文文档](cn/README.md)

18 changes: 9 additions & 9 deletions cn/Async.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Async
# 异步支持

### Async support
### 安装 nuget 包

PM> Install-Package Mapster.Async
> 这个插件允许为映射执行异步操作
This plugin allows you to perform async operation for mapping
PM> Install-Package Mapster.Async

##### Setup
##### 配置

Use `AfterMappingAsync` to setup async operation
使用 `AfterMappingAsync`方法配置映射完成后的处理过程:

```csharp
config.NewConfig<Poco, Dto>()
Expand All @@ -20,17 +20,17 @@ config.NewConfig<Poco, Dto>()
});
```

##### Mapping
##### 映射

Then map asynchronously with `AdaptToTypeAsync`.
然后使用 `AdaptToTypeAsync` 进行异步映射:

```csharp
var dto = await poco.BuildAdapter()
.AdaptToTypeAsync<Dto>();
```


Or like this, if you use mapper instance.
如果使用 `IMapper` 实例的话,可以像下面的例子这样进行异步映射:

```csharp
var dto = await _mapper.From(poco)
Expand Down
62 changes: 30 additions & 32 deletions cn/Attribute-base-Code-generation.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
# Attribute base Code generation
# 基于 Attribute 的代码生成

### Generate models
### 生成模型

Annotate your class with `[AdaptFrom]`, `[AdaptTo]`, or `[AdaptTwoWays]`.
在类上标记 `[AdaptFrom]`, `[AdaptTo]`, `[AdaptTwoWays]`

Example:
```csharp
[AdaptTo("[name]Dto")]
public class Student {
...
}
```

Then Mapster will generate:
这将会生成:
```csharp
public class StudentDto {
...
}
```

#### Ignore some properties on generation
#### 在生成时忽略一些成员

By default, code generation will ignore properties that annotated `[AdaptIgnore]` attribute. But you can add more settings which include `IgnoreAttributes`, `IgnoreNoAttributes`, `IgnoreNamespaces`.
默认代码生成将忽略标记了 `[AdaptIgnore]` 特性的成员,但是可以通过 `IgnoreAttributes`, `IgnoreNoAttributes`, `IgnoreNamespaces` 特性标记自定义生成代码忽略成员的逻辑。

Example:
例如,使用 `IgnoreNoAttributes` 特性配置让没有 `[DataMember]` 特性标记的成员在生成时忽略:
```csharp
[AdaptTo("[name]Dto", IgnoreNoAttributes = new[] { typeof(DataMemberAttribute) })]
public class Student {

[DataMember]
public string Name { get; set; } //this property will be generated
public string Name { get; set; } // 这个属性将生成到DTO
public string LastName { get; set; } //this will not be generated
public string LastName { get; set; } // 这个属性将不会生成到DTO
}
```

#### Change property types
#### 修改属性类型

By default, if property type annotated with the same adapt attribute, code generation will forward to that type. (For example, `Student` has `ICollection<Enrollment>`, after code generation `StudentDto` will has `ICollection<EnrollmentDto>`).
默认情况下,代码生成将在同一声明上转发类型。例如,`Student` `ICollection<Enrollment>`,代码生成后`StudentDto` 将有`ICollection<EnrollmentDto>`

You can override this by `[PropertyType(typeof(Target))]` attribute. This annotation can be annotated to either on property or on class.
可以通过 `[PropertyType(typeof(Target))]` 特性标记重写默认行为

For example:
> 这个特性标记既可以注释到属性上,也可以特性标记到类上。
例:
```csharp
[AdaptTo("[name]Dto")]
public class Student {
Expand All @@ -55,7 +56,7 @@ public class Enrollment {
}
```

This will generate:
这将生成:
```csharp
public class StudentDto {
public ICollection<DataItem> Enrollments { get; set; }
Expand All @@ -65,19 +66,18 @@ public class EnrollmentDto {
}
```

#### Generate readonly properties
#### 生成只读属性

For `[AdaptTo]` and `[AdaptTwoWays]`, you can generate readonly properties with `MapToConstructor` setting.
对于 `[AdaptTo]` `[AdaptTwoWays]` 特性标记,可以通过设置 `MapToConstructor`来生成只读属性:

For example:
```csharp
[AdaptTo("[name]Dto", MapToConstructor = true)]
public class Student {
public string Name { get; set; }
}
```

This will generate:
这将生成:
```csharp
public class StudentDto {
public string Name { get; }
Expand All @@ -88,39 +88,37 @@ public class StudentDto {
}
```

#### Generate nullable properties
#### 生成可空属性

For `[AdaptFrom]`, you can generate nullable properties with `IgnoreNullValues` setting.
对于 `[AdaptFrom]` 特性标记,可以通过设置 `IgnoreNullValues`来生成可空属性:

For example:
```csharp
[AdaptFrom("[name]Merge", IgnoreNullValues = true)]
public class Student {
public int Age { get; set; }
}
```

This will generate:
这将生成:
```csharp
public class StudentMerge {
public int? Age { get; set; }
}
```

### Generate extension methods
### 生成扩展方法

#### Generate using `[GenerateMapper]` attribute
For any POCOs annotate with `[AdaptFrom]`, `[AdaptTo]`, or `[AdaptTwoWays]`, you can add `[GenerateMapper]` in order to generate extension methods.
#### 使用 [GenerateMapper] 特性标记
对于任何带有 `[AdaptFrom]` `[AdaptTo]` `[AdaptTwoWays]` 特性标记的实体类,可以通过添加 `[GenerateMapper]` 特性标记实现生成扩展方法:

Example:
```csharp
[AdaptTo("[name]Dto"), GenerateMapper]
public class Student {
...
}
```

Then Mapster will generate:
这将生成:
```csharp
public class StudentDto {
...
Expand All @@ -132,8 +130,8 @@ public static class StudentMapper {
}
```

#### Configuration
If you have configuration, it must be in `IRegister`
#### 配置
如果有映射配置,它必须在实现了 `IRegister` 的类中:

```csharp
public class MyRegister : IRegister
Expand All @@ -145,9 +143,9 @@ public class MyRegister : IRegister
}
```

#### Generate using configuration
#### 使用配置生成

You can also generate extension methods and add extra settings from configuration.
可以生成扩展方法并从映射配置中添加额外的设置。

```csharp
public class MyRegister : IRegister
Expand Down
4 changes: 2 additions & 2 deletions cn/Before-after-mapping.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 映射前/映射后

###


### 在映射之前执行操作

Expand Down Expand Up @@ -34,7 +34,7 @@ TypeAdapterConfig.GlobalSettings.ForDestinationType<IValidatable>()

### 在 映射前/映射后 使用 Expression 生成代码

`BeforeMapping``AfterMapping `接受 `Action` 参数, `Action` 中可以有多行语句但不能动态生成代码。
`BeforeMapping``AfterMapping ` 接受 `Action` 参数, `Action` 中可以有多行语句但不能动态生成代码。

`BeforeMappingInline``AfterMappingInline` 接受 `Expression` 作为参数, `Expression` 可以动态编译为代码,实现懒加载等等。

Expand Down
4 changes: 2 additions & 2 deletions cn/Config-inheritance.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

Mapster 默认会把 源类型的 映射配置 应用到 源类型的子类。

如果你创建了一个 `SimplePoco` -> `SimpleDto` 的映射配置:
如创建了一个 `SimplePoco` -> `SimpleDto` 的映射配置:

```csharp
TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig()
Expand All @@ -24,7 +24,7 @@ var dest = TypeAdapter.Adapt<DerivedPoco, SimpleDto>(src);
//dest.Name = src.Name + "_Suffix"
```

如果不希望子类使用父类映射配置,可以设置 `AllowImplicitSourceInheritance` 关闭
如果不希望子类使用父类映射配置,可以设置 `AllowImplicitSourceInheritance` `false` 关闭继承

```csharp
TypeAdapterConfig.GlobalSettings.AllowImplicitSourceInheritance = false;
Expand Down
2 changes: 1 addition & 1 deletion cn/Config-instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var newConfig = TypeAdapterConfig.GlobalSettings.Clone();
var newConfig = oldConfig.Clone();
```

### Fork 配置实例
### Fork 配置

`Fork` 方法内部直接调用 `Clone` 方法,但是使用 `Fork` 方法的形式与使用 `Clone` 方法有些许差别。

Expand Down
2 changes: 0 additions & 2 deletions cn/Config-location.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ var dto = poco.Adapt<Dto>(

##### 在泛型类或方法中使用 Fork

`Fork` method uses filename and line number and the key. But if you use `Fork` method inside generic class or method, you must specify your own key (with all type names) to prevent `Fork` to return invalid config from different type arguments.

`Fork` 方法默认使用文件名、行号作为键值。但是如果在在泛型类或泛型方法中调用`Fork`方法,必须指定键和类型全名称,防止 `Fork` 从不同的类型参数返回无效的配置。

```csharp
Expand Down
2 changes: 1 addition & 1 deletion cn/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ TypeAdapterConfig<TSource, TDestination>



如果你不想覆盖之前已经创建好的映射配置,那你可以使用 `TypeAdapterConfig<TSource, TDestination>.ForType()`
如若不想覆盖之前已经创建好的映射配置,可以使用 `TypeAdapterConfig<TSource, TDestination>.ForType()`

`ForType` 方法与 `NewConfig` 的差别:如果指定类型映射配置不存在,那它将创建一个新的映射,如果指定类型的映射配置已存在,那么它将会扩展已有的映射配置,而不是删除或替换已有的映射配置。

Expand Down
27 changes: 16 additions & 11 deletions cn/Custom-conversion-logic.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,49 @@
# Custom conversion logic
# 自定义映射逻辑

### Custom type conversion
### 自定义类型映射

In some cases, you may want to have complete control over how an object is mapped. You can register specific transformations using the `MapWith` method.
使用 `MapWith` 方法可以指定特定类型的映射逻辑。

例如将字符串映射为 `char[]`:

```csharp
//Example of transforming string to char[].
TypeAdapterConfig<string, char[]>.NewConfig()
.MapWith(str => str.ToCharArray());
```

`MapWith` also useful if you would like to copy instance rather than deep copy the object, for instance, `JObject` or `DbGeography`, these should treat as primitive types rather than POCO.
在某些情况下,应该将映射行为修改为复制实例而不是深度拷贝,出现这种情况时同样可以使用 `MapWith`

例如,配置 `JObject` 映射到 `JObject`

```csharp
TypeAdapterConfig<JObject, JObject>.NewConfig()
.MapWith(json => json);
```

In case you would like to combine `MapWith` with other settings, for example, `PreserveReference`, `Include`, or `AfterMapping`, you can pass `applySettings` to true.
`MapWith` 默认不和其它映射配置产生关系,如果想要应用其它映射配置,例如 `PreserveReference` 、 `Include` `AfterMapping` 等方法,只需在调用 `MapWith` 方法的时候将 `applySettings` 参数设置为 `true` 即可:

```csharp
TypeAdapterConfig<ComplexPoco, ComplexDto>.NewConfig()
.PreserveReference(true)
.MapWith(poco => poco.ToDto(), applySettings: true);
```

### Custom mapping data to existing object
### 自定义到现有对象的映射数据

通过 `MapToTargetWith` 方法可以控制到现有对象逻辑的映射。

You can control mapping to existing object logic by `MapToTargetWith`. For example, you can copy data to existing array.
例如,可以将数据复制到现有数组中:

```csharp
TypeAdapterConfig<string[], string[]>.NewConfig()
.MapToTargetWith((src, dest) => Array.Copy(src, dest, src.Length));
```

NOTE: if you set `MapWith` setting but no `MapToTargetWith` setting, Mapster will use logic from `MapWith` setting.
> 注意!如果配置了 `MapWith`,但没有配置 `MapToTargetWith`Mapster 将使用 `MapWith` 的映射配置。
### Custom actions after mapping
### 自定义映射完成后的操作

You might not need to specify custom mapping logic completely. You can let Mapster do the mapping, and you do logic where Mapster cannot cover by using `AfterMapping`.
有些情况下,不需要完全自定义映射逻辑;可以让 Mapster 自动处理映射,然后通过 `AfterMapping` 方法在映射完成后对映射结果进行处理:

```csharp
TypeAdapterConfig<Poco, Dto>.NewConfig()
Expand Down
7 changes: 4 additions & 3 deletions cn/Data-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

基本类型的转换 ,例如: `int/bool/dobule/decimal` ,包括可空的基本类型。

只要C#支持的类型转换,那么在 Mapster 中也同样支持转换。
只要C#支持类型转换的类型,那么在 Mapster 中也同样支持转换。

```csharp
decimal i = 123.Adapt<decimal>(); //equal to (decimal)123;
Expand All @@ -27,7 +27,7 @@ Mapster 会自动把枚举映射到数字类型,同样也支持 字符串到
var e = "Read, Write, Delete".Adapt<FileShare>();
//FileShare.Read | FileShare.Write | FileShare.Delete
```
对于不同类型的枚举,Mapster 默认将值映射为枚举。你可以调用 `EnumMappingStrategy` 方法指定枚举映射方式,如:
对于不同类型的枚举,Mapster 默认将值映射为枚举。调用 `EnumMappingStrategy` 方法可以指定枚举映射方式,如:

```csharp
TypeAdapterConfig.GlobalSettings.Default
Expand All @@ -45,7 +45,7 @@ TypeAdapterConfig.GlobalSettings.Default
var s = 123.Adapt<string>(); // 等同于: 123.ToString();
var i = "123".Adapt<int>(); // 等同于: int.Parse("123");
```
###


### 集合

Expand All @@ -57,6 +57,7 @@ var target = list.Adapt<IEnumerable<Dto>>();
```



### 可映射对象

Mapster 可以使用以下规则映射两个不同的对象
Expand Down
Binary file added cn/Debugging.assets/image-20210613142212048.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 4c9f324

Please sign in to comment.