2022-04-29 15:02:36 +02:00
|
|
|
|
using System.Collections.Generic;
|
2019-04-08 10:05:21 +02:00
|
|
|
|
using System.Linq;
|
2019-03-26 10:39:50 +01:00
|
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
|
namespace Umbraco.Cms.Core.Mapping
|
2019-03-26 10:39:50 +01:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Represents a mapper context.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class MapperContext
|
|
|
|
|
|
{
|
2021-04-20 19:34:18 +02:00
|
|
|
|
private readonly IUmbracoMapper _mapper;
|
2022-03-31 12:52:26 +02:00
|
|
|
|
private IDictionary<string, object?>? _items;
|
2019-03-26 10:39:50 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="MapperContext"/> class.
|
|
|
|
|
|
/// </summary>
|
2021-04-20 19:34:18 +02:00
|
|
|
|
public MapperContext(IUmbracoMapper mapper)
|
2019-03-26 10:39:50 +01:00
|
|
|
|
{
|
2019-04-07 11:26:47 +02:00
|
|
|
|
_mapper = mapper;
|
2019-03-26 10:39:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a value indicating whether the context has items.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public bool HasItems => _items != null;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the context items.
|
|
|
|
|
|
/// </summary>
|
2022-03-31 12:52:26 +02:00
|
|
|
|
public IDictionary<string, object?> Items => _items ?? (_items = new Dictionary<string, object?>());
|
2019-04-07 11:26:47 +02:00
|
|
|
|
|
|
|
|
|
|
#region Map
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Maps a source object to a new target object.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="TTarget">The target type.</typeparam>
|
|
|
|
|
|
/// <param name="source">The source object.</param>
|
|
|
|
|
|
/// <returns>The target object.</returns>
|
2022-02-28 13:14:02 +01:00
|
|
|
|
public TTarget? Map<TTarget>(object? source)
|
2019-04-07 11:26:47 +02:00
|
|
|
|
=> _mapper.Map<TTarget>(source, this);
|
|
|
|
|
|
|
2019-04-07 12:59:59 +02:00
|
|
|
|
// let's say this is a bad (dangerous) idea, and leave it out for now
|
|
|
|
|
|
/*
|
2019-04-07 11:26:47 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Maps a source object to a new target object.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="TTarget">The target type.</typeparam>
|
|
|
|
|
|
/// <param name="source">The source object.</param>
|
|
|
|
|
|
/// <param name="f">A mapper context preparation method.</param>
|
|
|
|
|
|
/// <returns>The target object.</returns>
|
|
|
|
|
|
public TTarget Map<TTarget>(object source, Action<MapperContext> f)
|
|
|
|
|
|
{
|
|
|
|
|
|
f(this);
|
|
|
|
|
|
return _mapper.Map<TTarget>(source, this);
|
|
|
|
|
|
}
|
2019-04-07 12:59:59 +02:00
|
|
|
|
*/
|
2019-04-07 11:26:47 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Maps a source object to a new target object.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="TSource">The source type.</typeparam>
|
|
|
|
|
|
/// <typeparam name="TTarget">The target type.</typeparam>
|
|
|
|
|
|
/// <param name="source">The source object.</param>
|
|
|
|
|
|
/// <returns>The target object.</returns>
|
2022-02-27 21:20:50 +01:00
|
|
|
|
public TTarget? Map<TSource, TTarget>(TSource? source)
|
2019-04-07 11:26:47 +02:00
|
|
|
|
=> _mapper.Map<TSource, TTarget>(source, this);
|
|
|
|
|
|
|
2019-04-07 12:59:59 +02:00
|
|
|
|
// let's say this is a bad (dangerous) idea, and leave it out for now
|
|
|
|
|
|
/*
|
2019-04-07 11:26:47 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Maps a source object to a new target object.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="TSource">The source type.</typeparam>
|
|
|
|
|
|
/// <typeparam name="TTarget">The target type.</typeparam>
|
|
|
|
|
|
/// <param name="source">The source object.</param>
|
|
|
|
|
|
/// <param name="f">A mapper context preparation method.</param>
|
|
|
|
|
|
/// <returns>The target object.</returns>
|
|
|
|
|
|
public TTarget Map<TSource, TTarget>(TSource source, Action<MapperContext> f)
|
|
|
|
|
|
{
|
|
|
|
|
|
f(this);
|
|
|
|
|
|
return _mapper.Map<TSource, TTarget>(source, this);
|
|
|
|
|
|
}
|
2019-04-07 12:59:59 +02:00
|
|
|
|
*/
|
2019-04-07 11:26:47 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Maps a source object to an existing target object.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="TSource">The source type.</typeparam>
|
|
|
|
|
|
/// <typeparam name="TTarget">The target type.</typeparam>
|
|
|
|
|
|
/// <param name="source">The source object.</param>
|
|
|
|
|
|
/// <param name="target">The target object.</param>
|
|
|
|
|
|
/// <returns>The target object.</returns>
|
|
|
|
|
|
public TTarget Map<TSource, TTarget>(TSource source, TTarget target)
|
|
|
|
|
|
=> _mapper.Map(source, target, this);
|
|
|
|
|
|
|
2019-04-07 12:59:59 +02:00
|
|
|
|
// let's say this is a bad (dangerous) idea, and leave it out for now
|
|
|
|
|
|
/*
|
2019-04-07 11:26:47 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Maps a source object to an existing target object.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="TSource">The source type.</typeparam>
|
|
|
|
|
|
/// <typeparam name="TTarget">The target type.</typeparam>
|
|
|
|
|
|
/// <param name="source">The source object.</param>
|
|
|
|
|
|
/// <param name="target">The target object.</param>
|
|
|
|
|
|
/// <param name="f">A mapper context preparation method.</param>
|
|
|
|
|
|
/// <returns>The target object.</returns>
|
|
|
|
|
|
public TTarget Map<TSource, TTarget>(TSource source, TTarget target, Action<MapperContext> f)
|
|
|
|
|
|
{
|
|
|
|
|
|
f(this);
|
|
|
|
|
|
return _mapper.Map(source, target, this);
|
|
|
|
|
|
}
|
2019-04-07 12:59:59 +02:00
|
|
|
|
*/
|
2019-04-07 11:26:47 +02:00
|
|
|
|
|
2019-04-08 10:05:21 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Maps an enumerable of source objects to a new list of target objects.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="TSourceElement">The type of the source objects.</typeparam>
|
|
|
|
|
|
/// <typeparam name="TTargetElement">The type of the target objects.</typeparam>
|
|
|
|
|
|
/// <param name="source">The source objects.</param>
|
|
|
|
|
|
/// <returns>A list containing the target objects.</returns>
|
2022-04-29 08:19:34 +02:00
|
|
|
|
public List<TTargetElement> MapEnumerable<TSourceElement, TTargetElement>(IEnumerable<TSourceElement> source)
|
2019-04-08 10:05:21 +02:00
|
|
|
|
{
|
2022-04-29 15:02:36 +02:00
|
|
|
|
return source.Select(Map<TSourceElement, TTargetElement>).Where(x => x is not null).ToList()!;
|
2019-04-08 10:05:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-07 11:26:47 +02:00
|
|
|
|
#endregion
|
2019-03-26 10:39:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|