Skip to content

Commit

Permalink
补全剩余的文档
Browse files Browse the repository at this point in the history
  • Loading branch information
yihango committed Jun 13, 2021
1 parent 9f32126 commit f9fdac2
Show file tree
Hide file tree
Showing 19 changed files with 292 additions and 256 deletions.
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
26 changes: 16 additions & 10 deletions cn/Custom-conversion-logic.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,50 @@
# 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
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.
42 changes: 23 additions & 19 deletions cn/Debugging.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,57 @@
# Debugging
# 调试映射

### Step-into debugging
### 步进调试

PM> Install-Package ExpressionDebugger
> 这个插件允许使用Roslyn执行步进调试!
> 步进调试就是默认 F11 快捷键的调试
This plugin allows you to perform step-into debugging using Roslyn!
PM> Install-Package ExpressionDebugger

##### Usage
##### 如何使用

Then add following code on start up (or anywhere before mapping is compiled)
在启动时或编译映射之前的任何地方添加以下代码:

```csharp
TypeAdapterConfig.GlobalSettings.Compiler = exp => exp.CompileWithDebugInfo();
```

Now in your mapping code (only in `DEBUG` mode).
现在处于 DEBUG 模式时,可以步进调试映射代码:

```csharp
var dto = poco.Adapt<SimplePoco, SimpleDto>(); //<--- you can step-into this function!!
var dto = poco.Adapt<SimplePoco, SimpleDto>(); //<--- 你可以步进调试到这段代码中
```

![image](https://cloud.githubusercontent.com/assets/5763993/26521773/180427b6-431b-11e7-9188-10c01fa5ba5c.png)
![image-20210613142212048](Debugging.assets/image-20210613142212048.png)

##### Using internal classes or members
##### 使用内部类或成员

`private`, `protected` and `internal` aren't allowed in debug mode.
调试模式不支持 `private``protected` `internal`

### Get mapping script
### 查看映射逻辑

We can also see how Mapster generates mapping logic with `ToScript` method.
通过 `ToScript` 方法可以查看 Mapster 生成的映射逻辑:

```
var script = poco.BuildAdapter()
.CreateMapExpression<SimpleDto>()
.ToScript();
```

### Visual Studio for Mac
To step-into debugging, you might need to emit file
### 在Visual Studio for Mac中调试映射
在 Visual Studio for Mac 中进行步进映射调试,需要配置 `EmitFile``true`

```csharp
var opt = new ExpressionCompilationOptions { EmitFile = true };
TypeAdapterConfig.GlobalSettings.Compiler = exp => exp.CompileWithDebugInfo(opt);
...
var dto = poco.Adapt<SimplePoco, SimpleDto>(); //<-- you can step-into this function!!
var dto = poco.Adapt<SimplePoco, SimpleDto>(); //<--- 你可以步进调试到这段代码中
```

### Do not worry about performance
In `RELEASE` mode, Roslyn compiler is actually faster than default dynamic compilation by 2x.
Here is the result:
### 是否会对性能产生影响
`RELEASE` 编译模式下,Roslyn 编译器比默认的动态编译快2倍,因此不必担心会出现性能下降问题。

结果如下:

| Method | Mean | StdDev | Error | Gen 0 | Gen 1 | Gen 2 | Allocated |
| ------------------------ | --------: | -------: | -------: | ---------: | ----: | ----: | --------: |
Expand Down
24 changes: 14 additions & 10 deletions cn/Dependency-Injection.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# Dependency Injection
# 依赖注入



### Dependency injection support
### 依赖注入支持

> 这个插件允许将映射配置添加到依赖注入容器中
PM> Install-Package Mapster.DependencyInjection

This plugin allows you to inject service into mapping configuration.

#### Usage

On startup, register `TypeAdapterConfig`, and `ServiceMapper`.
#### 如何使用

在启动时,注册 `TypeAdapterConfig``ServiceMapper`

```csharp
public void ConfigureServices(IServiceCollection services)
Expand All @@ -25,20 +27,22 @@ public void ConfigureServices(IServiceCollection services)
}
```

NOTE: lifetime of `ServiceMapper` is up to services you would like to inject. It can be singleton, if you inject only singleton services. Or it can be transient, if any injected services is transient.
> 注意! `ServiceMapper` 可以根据实际的需求来决定在依赖注入容器的生命周期,但 `TypeAdapterConfig` 必须是 `Singleton`
##### 映射配置

##### Mapping configuration
可以通过 `MapContext.Current.GetService<TService>()` 从依赖注入容器中获取服务。

You can get service by `MapContext.Current.GetService<>()`, for example
例如从 `MapContext.Current.GetService` 获取 `INameFormatter` 服务:

```csharp
config.NewConfig<Poco, Dto>()
.Map(dest => dest.Name, src => MapContext.Current.GetService<INameFormatter>().Format(src.Name));
```

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

If you setup service injection, you need to use mapper instance to map object.
如果配置了依赖注入,那么需要注入 `IMapper` 实例用于对象映射:

```csharp
public class FooService {
Expand Down
Loading

0 comments on commit f9fdac2

Please sign in to comment.