-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MappingTarget attribute to set the mapping target as the first pa…
…rameter
- Loading branch information
Showing
20 changed files
with
423 additions
and
234 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
This file was deleted.
Oops, something went wrong.
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,114 @@ | ||
--- | ||
sidebar_position: 9 | ||
description: Map to an existing target object | ||
--- | ||
|
||
import Tabs from '@theme/Tabs'; | ||
import TabItem from '@theme/TabItem'; | ||
|
||
# Existing target object | ||
|
||
If an existing object instance should be used as target, you can define the mapping method as void with the target as second parameter: | ||
|
||
<Tabs> | ||
<TabItem value="declaration" label="Declaration" default> | ||
```csharp | ||
[Mapper] | ||
public partial class CarMapper | ||
{ | ||
// highlight-start | ||
public partial void UpdateCarDto(Car car, CarDto dto); | ||
// highlight-end | ||
} | ||
``` | ||
</TabItem> | ||
<TabItem value="usage" label="Usage"> | ||
```csharp | ||
var mapper = new CarMapper(); | ||
var car = new Car { NumberOfSeats = 10, ... }; | ||
var dto = new CarDto(); | ||
|
||
mapper.UpdateCarDto(car, dto); | ||
dto.NumberOfSeats.Should().Be(10); | ||
``` | ||
</TabItem> | ||
</Tabs> | ||
|
||
## Merge objects | ||
|
||
To merge two objects together, `AllowNullPropertyAssignment` can be set to `false`. | ||
This ignores all properties on the source with a `null` value. | ||
|
||
<Tabs> | ||
<TabItem value="declaration" label="Declaration" default> | ||
```csharp | ||
// highlight-start | ||
[Mapper(AllowNullPropertyAssignment = false)] | ||
// highlight-end | ||
static partial class FruitMapper | ||
{ | ||
// highlight-start | ||
public static partial void ApplyUpdate(FruitUpdate update, Fruit fruit); | ||
// highlight-end | ||
} | ||
|
||
class Fruit { public required string Name { get; set; } public required string Color { get; set; } } | ||
record FruitUpdate(string? Name, string? Color); | ||
``` | ||
</TabItem> | ||
<TabItem value="usage" label="Usage"> | ||
```csharp | ||
FruitMapper.ApplyUpdate(myUpdateRequest, myFruit); | ||
``` | ||
</TabItem> | ||
<TabItem value="generated" label="Generated code" default> | ||
```csharp | ||
static partial class FruitMapper | ||
{ | ||
public static partial void Update(global::FruitUpdate update, global::Fruit fruit) | ||
{ | ||
if (update.Name != null) | ||
{ | ||
fruit.Name = update.Name; | ||
} | ||
if (update.Color != null) | ||
{ | ||
fruit.Color = update.Color; | ||
} | ||
} | ||
} | ||
``` | ||
</TabItem> | ||
</Tabs> | ||
|
||
See also [null value handling](./mapper.mdx#null-values). | ||
|
||
The `MappingTarget` attribute allows setting the first method parameter as mapping target: | ||
|
||
<Tabs> | ||
<TabItem value="declaration" label="Declaration" default> | ||
|
||
```csharp | ||
// highlight-start | ||
[Mapper(AllowNullPropertyAssignment = false)] | ||
// highlight-end | ||
static partial class FruitMapper | ||
{ | ||
// highlight-start | ||
public static partial void ApplyUpdate([MappingTarget] this Fruit fruit, FruitUpdate update); | ||
// highlight-end | ||
} | ||
|
||
class Fruit { public required string Name { get; set; } public required string Color { get; set; } } | ||
record FruitUpdate(string? Name, string? Color); | ||
``` | ||
|
||
</TabItem> | ||
<TabItem value="usage" label="Usage"> | ||
```csharp | ||
myFruit.ApplyUpdate(myUpdateRequest); | ||
``` | ||
</TabItem> | ||
</Tabs> | ||
|
||
See also [extension methods](./static-mappers.md). |
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
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,10 @@ | ||
using System.Diagnostics; | ||
|
||
namespace Riok.Mapperly.Abstractions; | ||
|
||
/// <summary> | ||
/// Marks a given parameter as the mapping target. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Parameter)] | ||
[Conditional("MAPPERLY_ABSTRACTIONS_SCOPE_RUNTIME")] | ||
public sealed class MappingTargetAttribute : Attribute; |
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
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,3 +1 @@ | ||
#nullable enable | ||
Riok.Mapperly.Abstractions.MapPropertyAttribute.MapPropertyAttribute(string! source, string![]! target) -> void | ||
Riok.Mapperly.Abstractions.MapPropertyAttribute.MapPropertyAttribute(string![]! source, string! target) -> void |
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
Oops, something went wrong.