Files
Umbraco-CMS/src/Umbraco.Core/Mapping/MapperContext.cs

131 lines
5.0 KiB
C#
Raw Normal View History

2019-04-07 11:26:47 +02:00
using System;
using System.Collections.Generic;
2019-04-08 10:05:21 +02:00
using System.Linq;
2019-03-26 10:39:50 +01:00
namespace Umbraco.Core.Mapping
{
/// <summary>
/// Represents a mapper context.
/// </summary>
public class MapperContext
{
2019-04-07 11:26:47 +02:00
private readonly UmbracoMapper _mapper;
2019-03-26 10:39:50 +01:00
private IDictionary<string, object> _items;
/// <summary>
/// Initializes a new instance of the <see cref="MapperContext"/> class.
/// </summary>
2019-04-03 10:39:49 +02:00
public MapperContext(UmbracoMapper 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>
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>
public TTarget Map<TTarget>(object source)
=> _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>
public TTarget Map<TSource, TTarget>(TSource source)
=> _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>
public List<TTargetElement> MapEnumerable<TSourceElement, TTargetElement>(IEnumerable<TSourceElement> source)
{
return source.Select(Map<TSourceElement, TTargetElement>).ToList();
}
2019-04-07 11:26:47 +02:00
#endregion
2019-03-26 10:39:50 +01:00
}
}