generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAutoMapperConverterWrapper.cs
62 lines (51 loc) · 3.25 KB
/
AutoMapperConverterWrapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using System;
namespace CoreEx.Mapping.Converters
{
/// <summary>
/// Wraps an <see cref="IConverter"/> to enable <see cref="AutoMapper.IValueConverter{TSourceMember, TDestinationMember}"/> to/from capabilities.
/// </summary>
/// <typeparam name="TSource">The source <see cref="Type"/>.</typeparam>
/// <typeparam name="TDestination">The destination <see cref="Type"/>.</typeparam>
/// <typeparam name="TConverter">The <see cref="IConverter{TSource, TDestination}"/> <see cref="Type"/>.</typeparam>
/// <typeparam name="TSelf">The declaring <see cref="Type"/> itself to enable <see cref="Default"/>.</typeparam>
/// <param name="create">The optional function to create the <typeparamref name="TConverter"/> instance.</param>
public abstract class AutoMapperConverterWrapper<TSource, TDestination, TConverter, TSelf>(Func<TConverter>? create = null)
where TConverter : IConverter<TSource, TDestination>, new()
where TSelf : AutoMapperConverterWrapper<TSource, TDestination, TConverter, TSelf>, new()
{
private readonly Func<TConverter> _create = create ?? (() => new TConverter());
/// <summary>
/// Gets or sets the default (singleton) instance.
/// </summary>
public static TSelf Default { get; set; } = new();
/// <summary>
/// Gets the source to destination <see cref="AutoMapper.IValueConverter{TSource, TDestination}"/>.
/// </summary>
public AutoMapper.IValueConverter<TSource, TDestination> ToDestination => new ToDestinationMapper(_create().ToDestination);
/// <summary>
/// Gets the destination to source <see cref="AutoMapper.IValueConverter{TDestination, TSource}"/>.
/// </summary>
public AutoMapper.IValueConverter<TDestination, TSource> ToSource => new ToSourceMapper(_create().ToSource);
/// <summary>
/// Represents the source to destination <see cref="AutoMapper.IValueConverter{TSource, TDestination}"/> struct.
/// </summary>
public readonly struct ToDestinationMapper : AutoMapper.IValueConverter<TSource, TDestination>
{
private readonly IValueConverter<TSource, TDestination> _valueConverter;
internal ToDestinationMapper(IValueConverter<TSource, TDestination> valueConverter) => _valueConverter = valueConverter;
/// <inheritdoc/>
public TDestination Convert(TSource sourceMember, AutoMapper.ResolutionContext context) => _valueConverter.Convert(sourceMember)!;
}
/// <summary>
/// Represents the destination to source <see cref="AutoMapper.IValueConverter{TDestination, TSource}"/> struct.
/// </summary>
public readonly struct ToSourceMapper : AutoMapper.IValueConverter<TDestination, TSource>
{
private readonly IValueConverter<TDestination, TSource> _valueConverter;
internal ToSourceMapper(IValueConverter<TDestination, TSource> valueConverter) => _valueConverter = valueConverter;
/// <inheritdoc/>
public TSource Convert(TDestination sourceMember, AutoMapper.ResolutionContext context) => _valueConverter.Convert(sourceMember)!;
}
}
}