-
-
Notifications
You must be signed in to change notification settings - Fork 216
Add support for explicit Shallow Cloning #2117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6fe7428
3930302
ec83c02
2aabcd7
4c146b0
5e9cca4
44d7cc7
367fffa
bb9bd40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,18 @@ but have not handled the newly discovered members. | |
| Ignore these members to restore the previous behavior. | ||
| See the [private members mapping documentation](../configuration/private-members.mdx) for more details. | ||
|
|
||
| ## `UseDeepCloning` has been deprecated in favour of `CloningStrategy` | ||
|
|
||
| To support allow the implementation of shallow cloning, the `UseDeepCloning` property has been deprecated. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sentence doesn't sound quite correct... |
||
|
|
||
| Two strategies are available: | ||
| - `DeepCloning` - which corresponds to the previous `UseDeepCloning` behaviour | ||
| - `ShallowCloning` - which allows to clone an object into a new destination of the same type, | ||
| while retaining the same values as the source. All the child properties will be copied as-is, reference types included. | ||
|
|
||
| If you previously used `UseDeepCloning = true`, you can replace it with `CloningStrategy = CloningStrategy.DeepCloning` | ||
| to retain the same behaviour. | ||
|
|
||
| ## `Stack<T>` deep cloning order | ||
|
|
||
| When deep cloning a `Stack<T>`, Mapperly now preserves the order of elements by default. | ||
|
|
@@ -37,7 +49,7 @@ To restore the previous behavior, set `StackCloningStrategy` to `StackCloningStr | |
| ```csharp | ||
| [assembly: MapperDefaults(StackCloningStrategy = StackCloningStrategy.ReverseOrder)] | ||
| // or | ||
| [Mapper(UseDeepCloning = true, StackCloningStrategy = StackCloningStrategy.ReverseOrder)] | ||
| [Mapper(CloningStrategy = CloningStrategy.DeepCloning, StackCloningStrategy = StackCloningStrategy.ReverseOrder)] | ||
| public partial class MyMapper | ||
| { | ||
| // ... | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,23 +35,27 @@ Configurations set via `MapperDefaultsAttribute` take precedence over MSBuild pr | |
|
|
||
| ## Copy behavior | ||
|
|
||
| By default, Mapperly does not create deep copies of objects to improve performance. | ||
| By default, Mapperly does not create copies of objects to improve performance. | ||
| If an object can be directly assigned to the target, it will do so | ||
| (eg. if the source and target type are both `Car[]`, the array and its entries will not be cloned). | ||
| To create deep copies, set the `UseDeepCloning` property on the `MapperAttribute` to `true`. | ||
| To create copies, set the `CloningStrategy` property on the `MapperAttribute` to the desired cloning strategy. | ||
|
|
||
| ```csharp | ||
| // highlight-start | ||
| [Mapper(UseDeepCloning = true)] | ||
| [Mapper(CloningStrategy = CloningStrategy.DeepCloning or CloningStrategy.ShallowCloning)] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using an invalid c# syntax I'd create a new second line using the second option with a |
||
| // highlight-end | ||
| public partial class CarMapper | ||
| { | ||
| ... | ||
| } | ||
| ``` | ||
|
|
||
| Using `CloningStrategy.DeepCloning` will perform deep copies, so all reference types will be cloned recursively, | ||
| meanwhile by using `CloningStrategy.ShallowCloning` a new instance will be returned with all the valued copied 1:1 from | ||
| the source object, without performing any cloning on child properties. | ||
|
Comment on lines
+54
to
+55
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need a section explaining what happens if there is a user-defined mapping for a member type (which then could probably be deep-cloned, which may or may not be expected by the user). Anyway, this needs some explanation here, also reference |
||
|
|
||
| :::note | ||
| Deep cloning is not applied to `IQueryable` projection mappings. | ||
| Cloning is not applied to `IQueryable` projection mappings. | ||
| ::: | ||
|
|
||
| ### Stack cloning strategy | ||
|
|
@@ -63,7 +67,7 @@ This behavior can be configured via the `StackCloningStrategy` property on the ` | |
| - `StackCloningStrategy.ReverseOrder`: Reverses the order of elements (legacy behavior). | ||
|
|
||
| ```csharp | ||
| [Mapper(UseDeepCloning = true, StackCloningStrategy = StackCloningStrategy.ReverseOrder)] | ||
| [Mapper(CloningStrategy = CloningStrategy.DeepCloning, StackCloningStrategy = StackCloningStrategy.ReverseOrder)] | ||
| public partial class MyMapper | ||
| { | ||
| // ... | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| namespace Riok.Mapperly.Abstractions; | ||
|
|
||
| /// <summary> | ||
| /// Specifies whether and how to copy objects of the same type and complex types like collections and spans. | ||
| /// </summary> | ||
| public enum CloningStrategy | ||
| { | ||
| /// <summary> | ||
| /// Default behaviour, the original instance will be returned | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a suffix like |
||
| /// </summary> | ||
| None, | ||
|
|
||
| /// <summary> | ||
| /// Always deep copy objects. | ||
| /// Eg. when the type <c>Person[]</c> should be mapped to the same type <c>Person[]</c>, | ||
| /// the array and each person is cloned. | ||
| /// </summary> | ||
| DeepCloning, | ||
|
|
||
| /// <summary> | ||
| /// Always shallow copy objects. | ||
| /// Eg. when the type <c>Person</c> should be mapped to the same type <c>Person</c>, | ||
| /// a new instance will be returned with the same values for all properties. | ||
| /// References will be kept. | ||
| /// </summary> | ||
| ShallowCloning, | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add this to the bullet point list on the top.