-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
96 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# 设置值 | ||
|
||
### 计算值 | ||
|
||
可以使用 `Map` 方法指定计算值的逻辑。例如,根据姓 和名 字段生成完整的姓名: | ||
|
||
```csharp | ||
TypeAdapterConfig<Poco, Dto>.NewConfig() | ||
.Map(dest => dest.FullName, src => src.FirstName + " " + src.LastName); | ||
``` | ||
|
||
### 变换值 | ||
|
||
While `Map` method specify logic for single property, `AddDestinationTransform` allows transforms for all items of a type, such as trimming all strings. But really any operation can be performed on the destination value before assignment. | ||
|
||
当 `Map`方法为单个属性指定逻辑时,' AddDestinationTransform '允许对类型的所有项进行转换,例如修剪所有字符串。但实际上,在赋值之前可以对目标值执行任何操作。 | ||
|
||
**Trim string** | ||
|
||
```csharp | ||
TypeAdapterConfig<TSource, TDestination>.NewConfig() | ||
.AddDestinationTransform((string x) => x.Trim()); | ||
``` | ||
|
||
**Null replacement** | ||
```csharp | ||
TypeAdapterConfig<TSource, TDestination>.NewConfig() | ||
.AddDestinationTransform((string x) => x ?? ""); | ||
``` | ||
|
||
**Return empty collection if null** | ||
```csharp | ||
config.Default.AddDestinationTransform(DestinationTransform.EmptyCollectionIfNull); | ||
``` | ||
|
||
### Passing run-time value | ||
|
||
In some cases, you might would like to pass runtime values (ie, current user). On configuration, we can receive run-time value by `MapContext.Current.Parameters`. | ||
|
||
```csharp | ||
TypeAdapterConfig<Poco, Dto>.NewConfig() | ||
.Map(dest => dest.CreatedBy, | ||
src => MapContext.Current.Parameters["user"]); | ||
``` | ||
|
||
To pass run-time value, we need to use `BuildAdapter` method, and call `AddParameters` method to add each parameter. | ||
|
||
```csharp | ||
var dto = poco.BuildAdapter() | ||
.AddParameters("user", this.User.Identity.Name) | ||
.AdaptToType<Dto>(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,49 @@ | ||
# 目录 | ||
|
||
|
||
|
||
|
||
|
||
**映射** | ||
|
||
* [基本用法](Basic-usages.md) | ||
* [映射器](Mapper.md) | ||
##### [References](https://github.com/MapsterMapper/Mapster/wiki) | ||
##### 映射 | ||
* [基本使用](Basic-usages.md) | ||
* [映射器](Mappers.md) | ||
* [数据类型](Data-types.md) | ||
|
||
|
||
|
||
**配置** | ||
|
||
* [基本配置](Configuration.md) | ||
##### 配置 | ||
* [映射配置](Configuration.md) | ||
* [配置继承](Config-inheritance.md) | ||
|
||
* [配置实例](Config-instance.md) | ||
* [配置位置](Config-location.md) | ||
* [配置验证和编译](Config-validation-&-compilation.md) | ||
* [配置嵌套映射](Config-for-nested-mapping.md) | ||
|
||
##### 设置 | ||
* 自定义成员匹配逻辑 | ||
* [自定义映射](Custom-mapping.md) | ||
* [自定义命名约束](Naming-convention.md) | ||
* [Setting by attributes](Setting-by-attributes.md) | ||
* [Ignoring members](Ignoring-members.md) | ||
* [Rule-based member matching](Rule-based-member-mapping.md) | ||
* [Mapping readonly prop](Mapping-readonly-prop.md) | ||
* [Mapping non-public members](Mapping-non-public-members.md) | ||
* [Two ways & unflattening mapping](Two-ways.md) | ||
* [构造函数映射](Constructor-mapping.md) | ||
* [映射前/后](Before-after-mapping.md) | ||
* [设置值](Setting-values.md) | ||
* [浅映射和合并映射](Shallow-merge.md) | ||
* [递归和对象引用](Object-references.md) | ||
* [自定义转换逻辑](Custom-conversion-logic.md) | ||
|
||
|
||
##### 插件 | ||
* [异步支持](Async.md) | ||
* [Debugging](Debugging.md) | ||
* [依赖注入](Dependency-Injection.md) | ||
* [EF 6 & EF Core](EF-6-&-EF-Core.md) | ||
* [FastExpressionCompiler](FastExpressionCompiler.md) | ||
* [Immutable](Immutable.md) | ||
* [Json.net](Json.net.md) | ||
|
||
##### Tools | ||
* [Mapster.Tool](Mapster.Tool.md) | ||
* [Fluent API](Fluent-API-Code-generation.md) | ||
* [Attributes](Attribute-base-Code-generation.md) | ||
* [Interfaces](Interface-base-Code-generation.md) | ||
* [TextTemplate](TextTemplate.md) |