diff --git a/docs/articles/configuration/Configuration.md b/docs/articles/configuration/Configuration.md index 34a30549..4254a951 100644 --- a/docs/articles/configuration/Configuration.md +++ b/docs/articles/configuration/Configuration.md @@ -70,7 +70,8 @@ TypeAdapterConfig.GlobalSettings.ForDestinationType() .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 @@ -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, GenericDto>.NewConfig(); +> TypeAdapterConfig.GlobalSettings.Compile(); // validate +>``` \ No newline at end of file diff --git a/docs/articles/settings/Shallow-merge.md b/docs/articles/settings/Shallow-merge.md index ba101264..05dce624 100644 --- a/docs/articles/settings/Shallow-merge.md +++ b/docs/articles/settings/Shallow-merge.md @@ -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(); +``` + +### Deep copy ```csharp -TypeAdapterConfig +// 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 .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 + .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(); + + // 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. diff --git a/docs/articles/settings/custom/Mapping-readonly-prop.md b/docs/articles/settings/custom/Mapping-readonly-prop.md index c027b911..9a442ded 100644 --- a/docs/articles/settings/custom/Mapping-readonly-prop.md +++ b/docs/articles/settings/custom/Mapping-readonly-prop.md @@ -14,7 +14,7 @@ public class Order { } ``` -## Using `UseDestinationValue` attribute +### Using `UseDestinationValue` attribute You can make your type pure readonly and annotate with `[UseDestinationValue]`. @@ -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. @@ -37,3 +37,11 @@ TypeAdapterConfig.GlobalSettings.Default member.Type.IsGenericType && member.Type.GetGenericTypeDefinition() == typeof(ICollection<>)); ``` + +### Using `UseDestinationValue` [v10.0.4+] + +```csharp +TypeAdapterConfig.GlobalSettings + .ForDestinationType() + .UseDestinationValue(dest=>dest.Items); +``` \ No newline at end of file