-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support reference loops (#218)
- Loading branch information
Showing
146 changed files
with
2,392 additions
and
785 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 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,40 @@ | ||
# Reference handling | ||
|
||
Mapperly can support mapping object structures with circular references. | ||
To opt in for reference handling set `UseReferenceHandling` to `true`: | ||
```csharp | ||
// highlight-start | ||
[Mapper(UseReferenceHandling = true)] | ||
// highlight-end | ||
public partial class CarMapper | ||
{ | ||
public partial void CarToCarDto(Car car, CarDto dto); | ||
} | ||
``` | ||
|
||
This enables the usage of a default reference handler | ||
which reuses the same target object instance if encountered the same source object instance. | ||
|
||
## Custom reference handler | ||
|
||
To use a custom `IReferenceHandler` implementation, | ||
a parameter of the type `Riok.Mapperly.Abstractions.ReferenceHandling.IReferenceHandler` | ||
annotated with the `Riok.Mapperly.Abstractions.ReferenceHandling.ReferenceHandlerAttribute` | ||
can be added to the mapping method. | ||
|
||
```csharp | ||
// highlight-start | ||
[Mapper(UseReferenceHandling = true)] | ||
// highlight-end | ||
public partial class CarMapper | ||
{ | ||
// highlight-start | ||
public partial void CarToCarDto(Car car, CarDto dto, [ReferenceHandler] IReferenceHandler myRefHandler); | ||
// highlight-end | ||
} | ||
``` | ||
|
||
## User implemented mappings | ||
|
||
To make use of the `IReferenceHandler` in a user implemented mapping method, | ||
add a parameter as described in the section "[Custom reference handler](#custom-reference-handler)". |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
38 changes: 38 additions & 0 deletions
38
src/Riok.Mapperly.Abstractions/ReferenceHandling/IReferenceHandler.cs
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,38 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Riok.Mapperly.Abstractions.ReferenceHandling; | ||
|
||
/// <summary> | ||
/// A reference handler can store and resolve references | ||
/// of mapping target objects. | ||
/// </summary> | ||
public interface IReferenceHandler | ||
{ | ||
/// <summary> | ||
/// Before an object is created by Mapperly this method is called. | ||
/// It can attempt to resolve existing target object instances based on the source object instance. | ||
/// If <c>false</c> is returned, Mapperly creates a new instance of the target class. | ||
/// If <c>true</c> is returned, target has to be non-null. | ||
/// Mapperly then uses the target instance. | ||
/// </summary> | ||
/// <param name="source">The source object instance.</param> | ||
/// <param name="target">The resolved target object instance or <c>null</c> if none could be resolved.</param> | ||
/// <typeparam name="TSource">The type of the source object.</typeparam> | ||
/// <typeparam name="TTarget">The target object type.</typeparam> | ||
/// <returns></returns> | ||
bool TryGetReference<TSource, TTarget>(TSource source, [NotNullWhen(true)] out TTarget? target) | ||
where TSource : notnull | ||
where TTarget : notnull; | ||
|
||
/// <summary> | ||
/// Stores the created target instance. | ||
/// Called by Mapperly just after a new target object instance is created. | ||
/// </summary> | ||
/// <param name="source">The source object instance.</param> | ||
/// <param name="target">The target object instance.</param> | ||
/// <typeparam name="TSource">The type of the source object.</typeparam> | ||
/// <typeparam name="TTarget">The type of the target object.</typeparam> | ||
void SetReference<TSource, TTarget>(TSource source, TTarget target) | ||
where TSource : notnull | ||
where TTarget : notnull; | ||
} |
64 changes: 64 additions & 0 deletions
64
src/Riok.Mapperly.Abstractions/ReferenceHandling/Internal/PreserveReferenceHandler.cs
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,64 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Riok.Mapperly.Abstractions.ReferenceHandling.Internal; | ||
|
||
/// <summary> | ||
/// A <see cref="IReferenceHandler"/> implementation | ||
/// which returns the same target object instance if encountered the same source object instance. | ||
/// Do not use directly. Should only be used by Mapperly generated code. | ||
/// API surface is not subject to semantic releases and may break in any release. | ||
/// </summary> | ||
public sealed class PreserveReferenceHandler : IReferenceHandler | ||
{ | ||
private readonly Dictionary<(Type, Type), ReferenceHolder> _referenceHolders = new(); | ||
|
||
/// <inheritdoc cref="IReferenceHandler.TryGetReference{TSource,TTarget}"/> | ||
public bool TryGetReference<TSource, TTarget>(TSource source, [NotNullWhen(true)] out TTarget? target) | ||
where TSource : notnull | ||
where TTarget : notnull | ||
{ | ||
var refHolder = GetReferenceHolder<TSource, TTarget>(); | ||
return refHolder.TryGetRef(source, out target); | ||
} | ||
|
||
/// <inheritdoc cref="IReferenceHandler.SetReference{TSource,TTarget}"/> | ||
public void SetReference<TSource, TTarget>(TSource source, TTarget target) | ||
where TSource : notnull | ||
where TTarget : notnull | ||
=> GetReferenceHolder<TSource, TTarget>().SetRef(source, target); | ||
|
||
private ReferenceHolder GetReferenceHolder<TSource, TTarget>() | ||
{ | ||
var mapping = (typeof(TSource), typeof(TTarget)); | ||
if (_referenceHolders.TryGetValue(mapping, out var refHolder)) | ||
return refHolder; | ||
|
||
return _referenceHolders[mapping] = new(); | ||
} | ||
|
||
private class ReferenceHolder | ||
{ | ||
private readonly Dictionary<object, object> _references = new(ReferenceEqualityComparer<object>.Instance); | ||
|
||
public bool TryGetRef<TSource, TTarget>(TSource source, [NotNullWhen(true)] out TTarget? target) | ||
where TSource : notnull | ||
where TTarget : notnull | ||
{ | ||
if (_references.TryGetValue(source, out var targetObj)) | ||
{ | ||
target = (TTarget)targetObj; | ||
return true; | ||
} | ||
|
||
target = default; | ||
return false; | ||
} | ||
|
||
public void SetRef<TSource, TTarget>(TSource source, TTarget target) | ||
where TSource : notnull | ||
where TTarget : notnull | ||
{ | ||
_references[source] = target; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Riok.Mapperly.Abstractions/ReferenceHandling/Internal/ReferenceEqualityComparer.cs
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,27 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Riok.Mapperly.Abstractions.ReferenceHandling.Internal; | ||
|
||
/// <summary> | ||
/// Defines methods to support the comparison of objects for reference equality. | ||
/// </summary> | ||
/// <typeparam name="T">The type of objects to compare.</typeparam> | ||
internal sealed class ReferenceEqualityComparer<T> : IEqualityComparer<T> | ||
{ | ||
// cannot use System.Collections.Generic.ReferenceEqualityComparer since it is not available in netstandard2.0 | ||
|
||
/// <summary> | ||
/// A <see cref="ReferenceEqualityComparer{T}"/> instance. | ||
/// </summary> | ||
public static readonly IEqualityComparer<T> Instance = new ReferenceEqualityComparer<T>(); | ||
|
||
private ReferenceEqualityComparer() | ||
{ | ||
} | ||
|
||
bool IEqualityComparer<T>.Equals(T? x, T? y) | ||
=> ReferenceEquals(x, y); | ||
|
||
int IEqualityComparer<T>.GetHashCode(T obj) | ||
=> RuntimeHelpers.GetHashCode(obj); | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Riok.Mapperly.Abstractions/ReferenceHandling/ReferenceHandlerAttribute.cs
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 @@ | ||
namespace Riok.Mapperly.Abstractions.ReferenceHandling; | ||
|
||
/// <summary> | ||
/// Marks a mapping method parameter as a <see cref="IReferenceHandler"/>. | ||
/// The type of the parameter needs to be <see cref="IReferenceHandler"/>. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Parameter)] | ||
public sealed class ReferenceHandlerAttribute : 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
Oops, something went wrong.