namespace Umbraco.Cms.Core.Mapping;
///
/// Represents a mapper context.
///
public class MapperContext
{
private readonly IUmbracoMapper _mapper;
private IDictionary? _items;
///
/// Initializes a new instance of the class.
///
public MapperContext(IUmbracoMapper mapper) => _mapper = mapper;
///
/// Gets a value indicating whether the context has items.
///
public bool HasItems => _items != null;
///
/// Gets the context items.
///
public IDictionary Items => _items ??= new Dictionary();
#region Map
///
/// Maps a source object to a new target object.
///
/// The target type.
/// The source object.
/// The target object.
public TTarget? Map(object? source)
=> _mapper.Map(source, this);
// let's say this is a bad (dangerous) idea, and leave it out for now
/*
///
/// Maps a source object to a new target object.
///
/// The target type.
/// The source object.
/// A mapper context preparation method.
/// The target object.
public TTarget Map(object source, Action f)
{
f(this);
return _mapper.Map(source, this);
}
*/
///
/// Maps a source object to a new target object.
///
/// The source type.
/// The target type.
/// The source object.
/// The target object.
public TTarget? Map(TSource? source)
=> _mapper.Map(source, this);
// let's say this is a bad (dangerous) idea, and leave it out for now
/*
///
/// Maps a source object to a new target object.
///
/// The source type.
/// The target type.
/// The source object.
/// A mapper context preparation method.
/// The target object.
public TTarget Map(TSource source, Action f)
{
f(this);
return _mapper.Map(source, this);
}
*/
///
/// Maps a source object to an existing target object.
///
/// The source type.
/// The target type.
/// The source object.
/// The target object.
/// The target object.
public TTarget Map(TSource source, TTarget target)
=> _mapper.Map(source, target, this);
// let's say this is a bad (dangerous) idea, and leave it out for now
/*
///
/// Maps a source object to an existing target object.
///
/// The source type.
/// The target type.
/// The source object.
/// The target object.
/// A mapper context preparation method.
/// The target object.
public TTarget Map(TSource source, TTarget target, Action f)
{
f(this);
return _mapper.Map(source, target, this);
}
*/
///
/// Maps an enumerable of source objects to a new list of target objects.
///
/// The type of the source objects.
/// The type of the target objects.
/// The source objects.
/// A list containing the target objects.
public List MapEnumerable(IEnumerable source) =>
source.Select(Map).Where(x => x is not null).ToList()!;
#endregion
}