using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Umbraco.Core.Models;
using Umbraco.Web.Models.ContentEditing;
namespace Umbraco.Web.Models.Mapping
{
internal static class AutoMapperExtensions
{
///
/// This maps an object and passes in the current so the mapping logic can use it
///
///
///
///
///
///
public static TOut MapWithUmbracoContext(TIn obj, UmbracoContext umbCtx)
{
return Mapper.Map(obj, opt => opt.Items["UmbracoContext"] = umbCtx);
}
///
/// Returns an from the mapping options
///
///
///
///
/// If an UmbracoContext is not found in the mapping options, it will try to retrieve it from the singleton
///
public static UmbracoContext GetUmbracoContext(this ResolutionContext res)
{
//get the context from the mapping options set during a mapping operation
object umbCtx;
if (res.Options.Items.TryGetValue("UmbracoContext", out umbCtx))
{
var umbracoContext = umbCtx as UmbracoContext;
if (umbracoContext != null) return umbracoContext;
}
//return the singleton (this could be null)
return UmbracoContext.Current;
}
}
}