Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion docs/articles/configuration/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ TypeAdapterConfig.GlobalSettings.ForDestinationType<IValidator>()
.AfterMapping(dest => dest.Validate());
```

NOTE: `ForDestinationType` above will always apply to all types assignable to `IValidator`. If destination class implements `IValidator`, it will also apply the `AfterMapping` config.
>[!NOTE]
>`ForDestinationType` above will always apply to all types assignable to `IValidator`. If destination class implements `IValidator`, it will also apply the `AfterMapping` config.

## Open generics

Expand All @@ -80,3 +81,14 @@ If the mapping type is generic, you can create a setting by passing generic type
TypeAdapterConfig.GlobalSettings.ForType(typeof(GenericPoco<>), typeof(GenericDto<>))
.Map("value", "Value");
```
>[!Note]
> Starting with Mapster version 10.0.8, [validation](xref:Mapster.Configuration.ValidationAndCompilation#validating-mappings) for type pairs settings containing open generic type is disabled.
>If you require validation of such settings, you must register the configuration for at least one pair of closed generic types:
>```csharp
>TypeAdapterConfig.GlobalSettings.RequireDestinationMemberSource = true; // check all destination member mapping
>TypeAdapterConfig.GlobalSettings.ForType(typeof(GenericPoco<>), typeof(GenericDto<>))
> .Map("value", "Value");
>
> TypeAdapterConfig<GenericPoco<int>, GenericDto<string>>.NewConfig();
> TypeAdapterConfig.GlobalSettings.Compile(); // validate
>```
52 changes: 49 additions & 3 deletions docs/articles/settings/Shallow-merge.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,62 @@ uid: Mapster.Settings.ShallowMerge
title: "Settings - Shallow merge"
---

## Deep copy vs. shallow copy
## Deep copy vs. Shallow copy vs. Direct assignment

By default, Mapster will recursively map nested objects (deep copy). You can do shallow copying by setting `ShallowCopyForSameType` to `true`.
Depending on the settings specified, the mapping behavior may differ.
```csharp
var src = new Source { AProperty A {get; set;} , BProperty B {get; set;} }
var dest = src.Adapt<Destination>();
```

### Deep copy

```csharp
TypeAdapterConfig<TSource, TDestination>
// Deep copy - default behavior
var dest = new Destination { A = new AProperty{ int X = src.A.X }, B = new BProperty { int X = src.B.X} }
```

### Shallow copy

>[!NOTE]
> 1. Changes mapping behavior only for member(property,field) source and destination type in registered custom mapping configuration.
> 2. Disable when destination member(property,field) using [`UseDestinationValue`](xref:Mapster.Settings.Custom.ReadonlyProperty) setting.
> 3. Disable when registered custom mapping configuration `.NewConfig()` for Type member(property,field).

```csharp
//Shallow copy
TypeAdapterConfig<Source, Destination>
.NewConfig()
.ShallowCopyForSameType(true);

var dest = new Destination { A = src.A, B = src.B }
```

### Direct assignment [v10.0.8+]

>[!NOTE]
> 1. Changes mapping behavior for Type.
> 2. Disable when destination member(property,field) using [`UseDestinationValue`](xref:Mapster.Settings.Custom.ReadonlyProperty) setting.
> 3. Disable when registered custom mapping configuration `.NewConfig()` contains the [`.MapWith()` and/or `MapToTargetWith()`](xref:Mapster.Settings.CustomConversionLogic) setting.

```csharp
// DirectAssignment
TypeAdapterConfig<AProperty, AProperty>
.NewConfig()
.DirectAssignmentForSameType(true);

var dest = new Destination { A = src.A, B = new BProperty { int X = src.B.X} }

//for mapping AProperty to AProperty
var a = new AProperty();
var result = a.Adapt<AProperty>();

// behavior equal
var result = a;
```



## Copy vs. Merge

By default, Mapster will map all properties, even source properties containing null values. You can copy only properties that have values (merge) by using `IgnoreNullValues` method.
Expand Down
12 changes: 10 additions & 2 deletions docs/articles/settings/custom/Mapping-readonly-prop.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Order {
}
```

## Using `UseDestinationValue` attribute
### Using `UseDestinationValue` attribute

You can make your type pure readonly and annotate with `[UseDestinationValue]`.

Expand All @@ -27,7 +27,7 @@ public class Order {
}
```

## Convention based setup using `UseDestinationValue` Extension Method
### Convention based setup using `UseDestinationValue` Extension Method

Or you can apply without annotate each type, for example, if you would like all readonly `ICollection<>` to use destination value.

Expand All @@ -37,3 +37,11 @@ TypeAdapterConfig.GlobalSettings.Default
member.Type.IsGenericType &&
member.Type.GetGenericTypeDefinition() == typeof(ICollection<>));
```

### Using `UseDestinationValue` [v10.0.4+]

```csharp
TypeAdapterConfig.GlobalSettings
.ForDestinationType<Order>()
.UseDestinationValue(dest=>dest.Items);
```
Loading