diff --git a/src/Umbraco.Core/Mapping/MapperContext.cs b/src/Umbraco.Core/Mapping/MapperContext.cs index 9e385b32a8..49d906ccbc 100644 --- a/src/Umbraco.Core/Mapping/MapperContext.cs +++ b/src/Umbraco.Core/Mapping/MapperContext.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; namespace Umbraco.Core.Mapping { @@ -7,6 +8,7 @@ namespace Umbraco.Core.Mapping /// public class MapperContext { + private readonly UmbracoMapper _mapper; private IDictionary _items; /// @@ -14,14 +16,9 @@ namespace Umbraco.Core.Mapping /// public MapperContext(UmbracoMapper mapper) { - Mapper = mapper; + _mapper = mapper; } - /// - /// Gets the mapper. - /// - public UmbracoMapper Mapper { get;} - /// /// Gets a value indicating whether the context has items. /// @@ -31,5 +28,81 @@ namespace Umbraco.Core.Mapping /// Gets the context items. /// public IDictionary Items => _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); + + /// + /// 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); + + /// + /// 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); + + /// + /// 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); + } + + #endregion } } diff --git a/src/Umbraco.Web/Models/Mapping/CommonMapper.cs b/src/Umbraco.Web/Models/Mapping/CommonMapper.cs index 985cc2d628..7bf4a94b1c 100644 --- a/src/Umbraco.Web/Models/Mapping/CommonMapper.cs +++ b/src/Umbraco.Web/Models/Mapping/CommonMapper.cs @@ -34,19 +34,19 @@ namespace Umbraco.Web.Models.Mapping _localizedTextService = localizedTextService; } - public UserProfile GetOwner(IContentBase source, UmbracoMapper mapper) + public UserProfile GetOwner(IContentBase source, MapperContext context) { var profile = source.GetCreatorProfile(_userService); - return profile == null ? null : mapper.Map(profile); + return profile == null ? null : context.Map(profile); } - public UserProfile GetCreator(IContent source, UmbracoMapper mapper) + public UserProfile GetCreator(IContent source, MapperContext context) { var profile = source.GetWriterProfile(_userService); - return profile == null ? null : mapper.Map(profile); + return profile == null ? null : context.Map(profile); } - public ContentTypeBasic GetContentType(IContentBase source, UmbracoMapper mapper) + public ContentTypeBasic GetContentType(IContentBase source, MapperContext context) { // TODO: We can resolve the UmbracoContext from the IValueResolver options! // OMG @@ -54,7 +54,7 @@ namespace Umbraco.Web.Models.Mapping && Composing.Current.UmbracoContext.Security.CurrentUser.AllowedSections.Any(x => x.Equals(Constants.Applications.Settings))) { var contentType = _contentTypeBaseServiceProvider.GetContentTypeOf(source); - var contentTypeBasic = mapper.Map(contentType); + var contentTypeBasic = context.Map(contentType); return contentTypeBasic; } @@ -98,4 +98,4 @@ namespace Umbraco.Web.Models.Mapping return apps; } } -} \ No newline at end of file +} diff --git a/src/Umbraco.Web/Models/Mapping/ContentMapDefinition.cs b/src/Umbraco.Web/Models/Mapping/ContentMapDefinition.cs index a1a3b19a43..adc33c1360 100644 --- a/src/Umbraco.Web/Models/Mapping/ContentMapDefinition.cs +++ b/src/Umbraco.Web/Models/Mapping/ContentMapDefinition.cs @@ -65,7 +65,7 @@ namespace Umbraco.Web.Models.Mapping private static void Map(IContent source, ContentPropertyCollectionDto target, MapperContext context) { // must pass the context through - target.Properties = source.Properties.Select(p => context.Mapper.Map(p, context)); + target.Properties = source.Properties.Select(context.Map); } // Umbraco.Code.MapAll -AllowPreview -Errors -PersistedContent @@ -76,7 +76,7 @@ namespace Umbraco.Web.Models.Mapping target.ContentApps = _commonMapper.GetContentApps(source); target.ContentTypeAlias = source.ContentType.Alias; target.ContentTypeName = _localizedTextService.UmbracoDictionaryTranslate(source.ContentType.Name); - target.DocumentType = _commonMapper.GetContentType(source, context.Mapper); + target.DocumentType = _commonMapper.GetContentType(source, context); target.Icon = source.ContentType.Icon; target.Id = source.Id; target.IsBlueprint = source.Blueprint; @@ -84,7 +84,7 @@ namespace Umbraco.Web.Models.Mapping target.IsContainer = source.ContentType.IsContainer; target.IsElement = source.ContentType.IsElement; target.Key = source.Key; - target.Owner = _commonMapper.GetOwner(source, context.Mapper); + target.Owner = _commonMapper.GetOwner(source, context); target.ParentId = source.ParentId; target.Path = source.Path; target.SortOrder = source.SortOrder; @@ -94,12 +94,12 @@ namespace Umbraco.Web.Models.Mapping target.TreeNodeUrl = _commonMapper.GetTreeNodeUrl(source); target.Udi = Udi.Create(source.Blueprint ? Constants.UdiEntityType.DocumentBlueprint : Constants.UdiEntityType.Document, source.Key); target.UpdateDate = source.UpdateDate; - target.Updater = _commonMapper.GetCreator(source, context.Mapper); + target.Updater = _commonMapper.GetCreator(source, context); target.Urls = GetUrls(source); target.Variants = _contentVariantMapper.Map(source, context); target.ContentDto = new ContentPropertyCollectionDto(); - target.ContentDto.Properties = source.Properties.Select(context.Mapper.Map); + target.ContentDto.Properties = source.Properties.Select(context.Map); } // Umbraco.Code.MapAll -Segment -Language @@ -125,17 +125,17 @@ namespace Umbraco.Web.Models.Mapping target.Id = source.Id; target.Key = source.Key; target.Name = GetName(source, context); - target.Owner = _commonMapper.GetOwner(source, context.Mapper); + target.Owner = _commonMapper.GetOwner(source, context); target.ParentId = source.ParentId; target.Path = source.Path; // must pass the context through - target.Properties = source.Properties.Select(p => context.Mapper.Map(p, context)); + target.Properties = source.Properties.Select(context.Map); target.SortOrder = source.SortOrder; target.State = _basicStateMapper.Map(source, context); target.Trashed = source.Trashed; target.Udi = Udi.Create(source.Blueprint ? Constants.UdiEntityType.DocumentBlueprint : Constants.UdiEntityType.Document, source.Key); target.UpdateDate = GetUpdateDate(source, context); - target.Updater = _commonMapper.GetCreator(source, context.Mapper); + target.Updater = _commonMapper.GetCreator(source, context); target.VariesByCulture = source.ContentType.VariesByCulture(); } diff --git a/src/Umbraco.Web/Models/Mapping/ContentTypeMapDefinition.cs b/src/Umbraco.Web/Models/Mapping/ContentTypeMapDefinition.cs index 7007ca7674..394c5fb0cf 100644 --- a/src/Umbraco.Web/Models/Mapping/ContentTypeMapDefinition.cs +++ b/src/Umbraco.Web/Models/Mapping/ContentTypeMapDefinition.cs @@ -78,7 +78,7 @@ namespace Umbraco.Web.Models.Mapping // no MapAll - take care private void Map(DocumentTypeSave source, IContentType target, MapperContext context) { - MapSaveToTypeBase(source, target, context.Mapper); + MapSaveToTypeBase(source, target, context); MapComposition(source, target, alias => _contentTypeService.Get(alias)); target.AllowedTemplates = source.AllowedTemplates @@ -93,14 +93,14 @@ namespace Umbraco.Web.Models.Mapping // no MapAll - take care private void Map(MediaTypeSave source, IMediaType target, MapperContext context) { - MapSaveToTypeBase(source, target, context.Mapper); + MapSaveToTypeBase(source, target, context); MapComposition(source, target, alias => _mediaTypeService.Get(alias)); } // no MapAll - take care private void Map(MemberTypeSave source, IMemberType target, MapperContext context) { - MapSaveToTypeBase(source, target, context.Mapper); + MapSaveToTypeBase(source, target, context); MapComposition(source, target, alias => _memberTypeService.Get(alias)); foreach (var propertyType in source.Groups.SelectMany(x => x.Properties)) @@ -122,10 +122,10 @@ namespace Umbraco.Web.Models.Mapping target.AllowCultureVariant = source.VariesByCulture(); //sync templates - target.AllowedTemplates = source.AllowedTemplates.Select(context.Mapper.Map).ToArray(); + target.AllowedTemplates = source.AllowedTemplates.Select(context.Map).ToArray(); if (source.DefaultTemplate != null) - target.DefaultTemplate = context.Mapper.Map(source.DefaultTemplate); + target.DefaultTemplate = context.Map(source.DefaultTemplate); //default listview target.ListViewEditorName = Constants.Conventions.DataTypes.ListViewPrefix + "Content"; @@ -237,7 +237,7 @@ namespace Umbraco.Web.Models.Mapping // no MapAll - take care private void Map(DocumentTypeSave source, DocumentTypeDisplay target, MapperContext context) { - MapTypeToDisplayBase(source, target, context.Mapper); + MapTypeToDisplayBase(source, target, context); //sync templates var destAllowedTemplateAliases = target.AllowedTemplates.Select(x => x.Alias); @@ -250,7 +250,7 @@ namespace Umbraco.Web.Models.Mapping { var template = templates.SingleOrDefault(t => t.Alias == x); return template != null - ? context.Mapper.Map(template) + ? context.Map(template) : null; }) .WhereNotNull() @@ -263,7 +263,7 @@ namespace Umbraco.Web.Models.Mapping if (target.DefaultTemplate == null || source.DefaultTemplate != target.DefaultTemplate.Alias) { var template = _fileService.GetTemplate(source.DefaultTemplate); - target.DefaultTemplate = template == null ? null : context.Mapper.Map(template); + target.DefaultTemplate = template == null ? null : context.Map(template); } } else @@ -275,13 +275,13 @@ namespace Umbraco.Web.Models.Mapping // no MapAll - take care private void Map(MediaTypeSave source, MediaTypeDisplay target, MapperContext context) { - MapTypeToDisplayBase(source, target, context.Mapper); + MapTypeToDisplayBase(source, target, context); } // no MapAll - take care private void Map(MemberTypeSave source, MemberTypeDisplay target, MapperContext context) { - MapTypeToDisplayBase(source, target, context.Mapper); + MapTypeToDisplayBase(source, target, context); } // Umbraco.Code.MapAll -CreateDate -UpdateDate -DeleteDate -Key -PropertyTypes @@ -312,7 +312,7 @@ namespace Umbraco.Web.Models.Mapping target.Name = source.Name; target.SortOrder = source.SortOrder; - target.Properties = source.Properties.Select(context.Mapper.Map); + target.Properties = source.Properties.Select(context.Map); } // Umbraco.Code.MapAll -ContentTypeId -ParentTabContentTypes -ParentTabContentTypeNames @@ -325,7 +325,7 @@ namespace Umbraco.Web.Models.Mapping target.Name = source.Name; target.SortOrder = source.SortOrder; - target.Properties = source.Properties.Select(context.Mapper.Map); + target.Properties = source.Properties.Select(context.Map); } // Umbraco.Code.MapAll -Editor -View -Config -ContentTypeId -ContentTypeName -Locked @@ -364,7 +364,7 @@ namespace Umbraco.Web.Models.Mapping // Umbraco.Code.MapAll -CreatorId -Level -SortOrder // Umbraco.Code.MapAll -CreateDate -UpdateDate -DeleteDate // Umbraco.Code.MapAll -ContentTypeComposition (done by AfterMapSaveToType) - private static void MapSaveToTypeBase(TSource source, IContentTypeComposition target, UmbracoMapper mapper) + private static void MapSaveToTypeBase(TSource source, IContentTypeComposition target, MapperContext context) where TSource : ContentTypeSave where TSourcePropertyType : PropertyTypeBasic { @@ -420,12 +420,12 @@ namespace Umbraco.Web.Models.Mapping foreach (var sourceGroup in sourceGroups) { // get the dest group - var destGroup = MapSaveGroup(sourceGroup, destOrigGroups, mapper); + var destGroup = MapSaveGroup(sourceGroup, destOrigGroups, context); // handle local properties var destProperties = sourceGroup.Properties .Where(x => x.Inherited == false) - .Select(x => MapSaveProperty(x, destOrigProperties, mapper)) + .Select(x => MapSaveProperty(x, destOrigProperties, context)) .ToArray(); // if the group has no local properties, skip it, ie sort-of garbage-collect @@ -453,7 +453,7 @@ namespace Umbraco.Web.Models.Mapping // handle local properties var destProperties = genericPropertiesGroup.Properties .Where(x => x.Inherited == false) - .Select(x => MapSaveProperty(x, destOrigProperties, mapper)) + .Select(x => MapSaveProperty(x, destOrigProperties, context)) .ToArray(); // ensure no duplicate alias, then assign the generic properties collection @@ -523,7 +523,7 @@ namespace Umbraco.Web.Models.Mapping } // no MapAll - relies on the non-generic method - private void MapTypeToDisplayBase(TSource source, TTarget target, UmbracoMapper mapper) + private void MapTypeToDisplayBase(TSource source, TTarget target, MapperContext context) where TSource : ContentTypeSave where TSourcePropertyType : PropertyTypeBasic where TTarget : ContentTypeCompositionDisplay @@ -531,7 +531,7 @@ namespace Umbraco.Web.Models.Mapping { MapTypeToDisplayBase(source, target); - target.Groups = source.Groups.Select(mapper.Map>); + target.Groups = source.Groups.Select(context.Map>); } private IEnumerable MapLockedCompositions(IContentTypeComposition source) @@ -580,7 +580,7 @@ namespace Umbraco.Web.Models.Mapping return Udi.Create(udiType, source.Key); } - private static PropertyGroup MapSaveGroup(PropertyGroupBasic sourceGroup, IEnumerable destOrigGroups, UmbracoMapper mapper) + private static PropertyGroup MapSaveGroup(PropertyGroupBasic sourceGroup, IEnumerable destOrigGroups, MapperContext context) where TPropertyType : PropertyTypeBasic { PropertyGroup destGroup; @@ -591,7 +591,7 @@ namespace Umbraco.Web.Models.Mapping destGroup = destOrigGroups.FirstOrDefault(x => x.Id == sourceGroup.Id); if (destGroup != null) { - mapper.Map(sourceGroup, destGroup); + context.Map(sourceGroup, destGroup); return destGroup; } @@ -602,11 +602,11 @@ namespace Umbraco.Web.Models.Mapping // insert a new group, or update an existing group that has // been deleted in the meantime and we need to re-create // map/create - destGroup = mapper.Map(sourceGroup); + destGroup = context.Map(sourceGroup); return destGroup; } - private static PropertyType MapSaveProperty(PropertyTypeBasic sourceProperty, IEnumerable destOrigProperties, UmbracoMapper mapper) + private static PropertyType MapSaveProperty(PropertyTypeBasic sourceProperty, IEnumerable destOrigProperties, MapperContext context) { PropertyType destProperty; if (sourceProperty.Id > 0) @@ -616,7 +616,7 @@ namespace Umbraco.Web.Models.Mapping destProperty = destOrigProperties.FirstOrDefault(x => x.Id == sourceProperty.Id); if (destProperty != null) { - mapper.Map(sourceProperty, destProperty); + context.Map(sourceProperty, destProperty); return destProperty; } @@ -627,7 +627,7 @@ namespace Umbraco.Web.Models.Mapping // insert a new property, or update an existing property that has // been deleted in the meantime and we need to re-create // map/create - destProperty = mapper.Map(sourceProperty); + destProperty = context.Map(sourceProperty); return destProperty; } diff --git a/src/Umbraco.Web/Models/Mapping/ContentVariantMapper.cs b/src/Umbraco.Web/Models/Mapping/ContentVariantMapper.cs index 4c826ac8b0..aa71b9817a 100644 --- a/src/Umbraco.Web/Models/Mapping/ContentVariantMapper.cs +++ b/src/Umbraco.Web/Models/Mapping/ContentVariantMapper.cs @@ -25,14 +25,14 @@ namespace Umbraco.Web.Models.Mapping if (!source.ContentType.VariesByCulture()) { //this is invariant so just map the IContent instance to ContentVariationDisplay - result.Add(context.Mapper.Map(source)); + result.Add(context.Map(source)); } else { var allLanguages = _localizationService.GetAllLanguages().OrderBy(x => x.Id).ToList(); if (allLanguages.Count == 0) return Enumerable.Empty(); //this should never happen - var langs = context.Mapper.Map>(allLanguages, context).ToList(); + var langs = context.Map>(allLanguages).ToList(); //create a variant for each language, then we'll populate the values var variants = langs.Select(x => @@ -40,7 +40,7 @@ namespace Umbraco.Web.Models.Mapping //We need to set the culture in the mapping context since this is needed to ensure that the correct property values //are resolved during the mapping context.SetCulture(x.IsoCode); - return context.Mapper.Map(source, context); + return context.Map(source); }).ToList(); for (int i = 0; i < langs.Count; i++) diff --git a/src/Umbraco.Web/Models/Mapping/DataTypeMapDefinition.cs b/src/Umbraco.Web/Models/Mapping/DataTypeMapDefinition.cs index 122f934818..7240bdec50 100644 --- a/src/Umbraco.Web/Models/Mapping/DataTypeMapDefinition.cs +++ b/src/Umbraco.Web/Models/Mapping/DataTypeMapDefinition.cs @@ -36,9 +36,9 @@ namespace Umbraco.Web.Models.Mapping mapper.Define((source, context) => new DataTypeBasic(), Map); mapper.Define((source, context) => new DataTypeBasic(), Map); mapper.Define((source, context) => new DataTypeDisplay(), Map); - mapper.Define>((source, context) => MapPreValues(source, mapper)); + mapper.Define>(MapPreValues); mapper.Define((source, context) => new DataType(_propertyEditors[source.EditorAlias]) { CreateDate = DateTime.Now },Map); - mapper.Define>((source, context) => MapPreValues(source, mapper)); + mapper.Define>(MapPreValues); } // Umbraco.Code.MapAll @@ -93,14 +93,14 @@ namespace Umbraco.Web.Models.Mapping // Umbraco.Code.MapAll -HasPrevalues private void Map(IDataType source, DataTypeDisplay target, MapperContext context) { - target.AvailableEditors = MapAvailableEditors(source, context.Mapper); + target.AvailableEditors = MapAvailableEditors(source, context); target.Id = source.Id; target.IsSystemDataType = SystemIds.Contains(source.Id); target.Key = source.Key; target.Name = source.Name; target.ParentId = source.ParentId; target.Path = source.Path; - target.PreValues = MapPreValues(source, context.Mapper); + target.PreValues = MapPreValues(source, context); target.SelectedEditor = source.EditorAlias.IsNullOrWhiteSpace() ? null : source.EditorAlias; target.Trashed = source.Trashed; target.Udi = Udi.Create(Constants.UdiEntityType.DataType, source.Key); @@ -124,16 +124,16 @@ namespace Umbraco.Web.Models.Mapping target.ParentId = source.ParentId; } - private IEnumerable MapAvailableEditors(IDataType source, UmbracoMapper mapper) + private IEnumerable MapAvailableEditors(IDataType source, MapperContext context) { var contentSection = Current.Configs.Settings().Content; return _propertyEditors .Where(x => !x.IsDeprecated || contentSection.ShowDeprecatedPropertyEditors || source.EditorAlias == x.Alias) .OrderBy(x => x.Name) - .Select(mapper.Map); + .Select(context.Map); } - private IEnumerable MapPreValues(IDataType dataType, UmbracoMapper mapper) + private IEnumerable MapPreValues(IDataType dataType, MapperContext context) { // in v7 it was apparently fine to have an empty .EditorAlias here, in which case we would map onto // an empty fields list, which made no sense since there would be nothing to map to - and besides, @@ -143,7 +143,7 @@ namespace Umbraco.Web.Models.Mapping throw new InvalidOperationException($"Could not find a property editor with alias \"{dataType.EditorAlias}\"."); var configurationEditor = editor.GetConfigurationEditor(); - var fields = configurationEditor.Fields.Select(mapper.Map).ToArray(); + var fields = configurationEditor.Fields.Select(context.Map).ToArray(); var configurationDictionary = configurationEditor.ToConfigurationEditor(dataType.Configuration); MapConfigurationFields(fields, configurationDictionary); @@ -181,7 +181,7 @@ namespace Umbraco.Web.Models.Mapping return ValueTypes.ToStorageType(valueType); } - private IEnumerable MapPreValues(IDataEditor source, UmbracoMapper mapper) + private IEnumerable MapPreValues(IDataEditor source, MapperContext context) { // this is a new data type, initialize default configuration // get the configuration editor, @@ -190,7 +190,7 @@ namespace Umbraco.Web.Models.Mapping var configurationEditor = source.GetConfigurationEditor(); - var fields = configurationEditor.Fields.Select(mapper.Map).ToArray(); + var fields = configurationEditor.Fields.Select(context.Map).ToArray(); var defaultConfiguration = configurationEditor.DefaultConfiguration; if (defaultConfiguration != null) diff --git a/src/Umbraco.Web/Models/Mapping/EntityMapDefinition.cs b/src/Umbraco.Web/Models/Mapping/EntityMapDefinition.cs index fe9cc622ce..cd51dd24ec 100644 --- a/src/Umbraco.Web/Models/Mapping/EntityMapDefinition.cs +++ b/src/Umbraco.Web/Models/Mapping/EntityMapDefinition.cs @@ -26,8 +26,8 @@ namespace Umbraco.Web.Models.Mapping mapper.Define((source, context) => new EntityBasic(), Map); mapper.Define((source, context) => new SearchResultEntity(), Map); mapper.Define((source, context) => new SearchResultEntity(), Map); - mapper.Define>((source, context) => source.Select(context.Mapper.Map).ToList()); - mapper.Define, IEnumerable>((source, context) => source.Select(context.Mapper.Map).ToList()); + mapper.Define>((source, context) => source.Select(context.Map).ToList()); + mapper.Define, IEnumerable>((source, context) => source.Select(context.Map).ToList()); } // Umbraco.Code.MapAll -Alias diff --git a/src/Umbraco.Web/Models/Mapping/LanguageMapDefinition.cs b/src/Umbraco.Web/Models/Mapping/LanguageMapDefinition.cs index dcdd737676..21d42f0bc2 100644 --- a/src/Umbraco.Web/Models/Mapping/LanguageMapDefinition.cs +++ b/src/Umbraco.Web/Models/Mapping/LanguageMapDefinition.cs @@ -45,7 +45,7 @@ namespace Umbraco.Web.Models.Mapping if (!(target is List list)) throw new NotSupportedException($"{nameof(target)} must be a List."); - var temp = source.Select(context.Mapper.Map).ToList(); + var temp = source.Select(context.Map).ToList(); //Put the default language first in the list & then sort rest by a-z var defaultLang = temp.SingleOrDefault(x => x.IsDefault); diff --git a/src/Umbraco.Web/Models/Mapping/MacroMapDefinition.cs b/src/Umbraco.Web/Models/Mapping/MacroMapDefinition.cs index b3cd662315..0d6f779b82 100644 --- a/src/Umbraco.Web/Models/Mapping/MacroMapDefinition.cs +++ b/src/Umbraco.Web/Models/Mapping/MacroMapDefinition.cs @@ -23,7 +23,7 @@ namespace Umbraco.Web.Models.Mapping public void DefineMaps(UmbracoMapper mapper) { mapper.Define((source, context) => new EntityBasic(), Map); - mapper.Define>((source, context) => source.Properties.Values.Select(context.Mapper.Map).ToList()); + mapper.Define>((source, context) => source.Properties.Values.Select(context.Map).ToList()); mapper.Define((source, context) => new MacroParameter(), Map); } diff --git a/src/Umbraco.Web/Models/Mapping/MediaMapDefinition.cs b/src/Umbraco.Web/Models/Mapping/MediaMapDefinition.cs index b1d5955368..b147687ed3 100644 --- a/src/Umbraco.Web/Models/Mapping/MediaMapDefinition.cs +++ b/src/Umbraco.Web/Models/Mapping/MediaMapDefinition.cs @@ -42,14 +42,14 @@ namespace Umbraco.Web.Models.Mapping // Umbraco.Code.MapAll private static void Map(IMedia source, ContentPropertyCollectionDto target, MapperContext context) { - target.Properties = source.Properties.Select(context.Mapper.Map); + target.Properties = source.Properties.Select(context.Map); } // Umbraco.Code.MapAll -Properties -Errors -Edited -Updater -Alias -IsContainer private void Map(IMedia source, MediaItemDisplay target, MapperContext context) { target.ContentApps = _commonMapper.GetContentApps(source); - target.ContentType = _commonMapper.GetContentType(source, context.Mapper); + target.ContentType = _commonMapper.GetContentType(source, context); target.ContentTypeAlias = source.ContentType.Alias; target.ContentTypeName = source.ContentType.Name; target.CreateDate = source.CreateDate; @@ -59,7 +59,7 @@ namespace Umbraco.Web.Models.Mapping target.Key = source.Key; target.MediaLink = string.Join(",", source.GetUrls(Current.Configs.Settings().Content, _logger)); target.Name = source.Name; - target.Owner = _commonMapper.GetOwner(source, context.Mapper); + target.Owner = _commonMapper.GetOwner(source, context); target.ParentId = source.ParentId; target.Path = source.Path; target.SortOrder = source.SortOrder; @@ -81,10 +81,10 @@ namespace Umbraco.Web.Models.Mapping target.Id = source.Id; target.Key = source.Key; target.Name = source.Name; - target.Owner = _commonMapper.GetOwner(source, context.Mapper); + target.Owner = _commonMapper.GetOwner(source, context); target.ParentId = source.ParentId; target.Path = source.Path; - target.Properties = source.Properties.Select(context.Mapper.Map); + target.Properties = source.Properties.Select(context.Map); target.SortOrder = source.SortOrder; target.State = null; target.Trashed = source.Trashed; diff --git a/src/Umbraco.Web/Models/Mapping/MemberMapDefinition.cs b/src/Umbraco.Web/Models/Mapping/MemberMapDefinition.cs index b59f3ca120..84f9340713 100644 --- a/src/Umbraco.Web/Models/Mapping/MemberMapDefinition.cs +++ b/src/Umbraco.Web/Models/Mapping/MemberMapDefinition.cs @@ -45,9 +45,9 @@ namespace Umbraco.Web.Models.Mapping private void Map(MembershipUser source, MemberDisplay target, MapperContext context) { //first convert to IMember - var member = context.Mapper.Map(source); + var member = context.Map(source); //then convert to MemberDisplay - context.Mapper.Map(member); + context.Map(member); } // TODO: SD: I can't remember why this mapping is here? @@ -86,7 +86,7 @@ namespace Umbraco.Web.Models.Mapping target.MemberProviderFieldMapping = GetMemberProviderFieldMapping(); target.MembershipScenario = GetMembershipScenario(); target.Name = source.Name; - target.Owner = _commonMapper.GetOwner(source, context.Mapper); + target.Owner = _commonMapper.GetOwner(source, context); target.ParentId = source.ParentId; target.Path = source.Path; target.SortOrder = source.SortOrder; @@ -108,10 +108,10 @@ namespace Umbraco.Web.Models.Mapping target.Id = int.MaxValue; target.Key = source.Key; target.Name = source.Name; - target.Owner = _commonMapper.GetOwner(source, context.Mapper); + target.Owner = _commonMapper.GetOwner(source, context); target.ParentId = source.ParentId; target.Path = source.Path; - target.Properties = context.Mapper.Map>(source.Properties); + target.Properties = context.Map>(source.Properties); target.SortOrder = source.SortOrder; target.State = null; target.Udi = Udi.Create(Constants.UdiEntityType.Member, source.Key); @@ -149,7 +149,7 @@ namespace Umbraco.Web.Models.Mapping // Umbraco.Code.MapAll private static void Map(IMember source, ContentPropertyCollectionDto target, MapperContext context) { - target.Properties = source.Properties.Select(context.Mapper.Map); + target.Properties = source.Properties.Select(context.Map); } private MembershipScenario GetMembershipScenario() diff --git a/src/Umbraco.Web/Models/Mapping/TabsAndPropertiesMapper.cs b/src/Umbraco.Web/Models/Mapping/TabsAndPropertiesMapper.cs index 89cf58aaeb..29aefdfeda 100644 --- a/src/Umbraco.Web/Models/Mapping/TabsAndPropertiesMapper.cs +++ b/src/Umbraco.Web/Models/Mapping/TabsAndPropertiesMapper.cs @@ -106,7 +106,7 @@ namespace Umbraco.Web.Models.Mapping protected virtual List MapProperties(IContentBase content, List properties, MapperContext context) { // must pass the context through - return properties.OrderBy(x => x.PropertyType.SortOrder).Select(x => context.Mapper.Map(x, context)).ToList(); + return properties.OrderBy(x => x.PropertyType.SortOrder).Select(x => context.Map(x)).ToList(); } } diff --git a/src/Umbraco.Web/Models/Mapping/UserMapDefinition.cs b/src/Umbraco.Web/Models/Mapping/UserMapDefinition.cs index aa20fa4227..e2c7fb611e 100644 --- a/src/Umbraco.Web/Models/Mapping/UserMapDefinition.cs +++ b/src/Umbraco.Web/Models/Mapping/UserMapDefinition.cs @@ -146,7 +146,7 @@ namespace Umbraco.Web.Models.Mapping target.Name = source.Name; target.ParentId = -1; target.Path = "-1," + source.Id; - MapUserGroupBasic(target, source.AllowedSections, source.StartContentId, source.StartMediaId, context.Mapper); + MapUserGroupBasic(target, source.AllowedSections, source.StartContentId, source.StartMediaId, context); } // Umbraco.Code.MapAll -ContentStartNode -MediaStartNode -Sections -Notifications @@ -161,7 +161,7 @@ namespace Umbraco.Web.Models.Mapping target.ParentId = -1; target.Path = "-1," + source.Id; target.UserCount = source.UserCount; - MapUserGroupBasic(target, source.AllowedSections, source.StartContentId, source.StartMediaId, context.Mapper); + MapUserGroupBasic(target, source.AllowedSections, source.StartContentId, source.StartMediaId, context); } // Umbraco.Code.MapAll -Udi -Trashed -AdditionalData -AssignedPermissions @@ -210,12 +210,12 @@ namespace Umbraco.Web.Models.Mapping target.Path = "-1," + source.Id; target.UserCount = source.UserCount; - MapUserGroupBasic(target, source.AllowedSections, source.StartContentId, source.StartMediaId, context.Mapper); + MapUserGroupBasic(target, source.AllowedSections, source.StartContentId, source.StartMediaId, context); //Important! Currently we are never mapping to multiple UserGroupDisplay objects but if we start doing that // this will cause an N+1 and we'll need to change how this works. var users = _userService.GetAllInGroup(source.Id); - target.Users = context.Mapper.Map>(users); + target.Users = context.Map>(users); //Deal with assigned permissions: @@ -244,7 +244,7 @@ namespace Umbraco.Web.Models.Mapping { var contentPermissions = allContentPermissions[entity.Id]; - var assignedContentPermissions = context.Mapper.Map(entity); + var assignedContentPermissions = context.Map(entity); assignedContentPermissions.AssignedPermissions = AssignedUserGroupPermissions.ClonePermissions(target.DefaultPermissions); //since there is custom permissions assigned to this node for this group, we need to clear all of the default permissions @@ -267,8 +267,8 @@ namespace Umbraco.Web.Models.Mapping { target.AvailableCultures = _textService.GetSupportedCultures().ToDictionary(x => x.Name, x => x.DisplayName); target.Avatars = source.GetUserAvatarUrls(_appCaches.RuntimeCache); - target.CalculatedStartContentIds = GetStartNodes(source.CalculateContentStartNodeIds(_entityService), UmbracoObjectTypes.Document, "content/contentRoot", context.Mapper); - target.CalculatedStartMediaIds = GetStartNodes(source.CalculateMediaStartNodeIds(_entityService), UmbracoObjectTypes.Media, "media/mediaRoot", context.Mapper); + target.CalculatedStartContentIds = GetStartNodes(source.CalculateContentStartNodeIds(_entityService), UmbracoObjectTypes.Document, "content/contentRoot", context); + target.CalculatedStartMediaIds = GetStartNodes(source.CalculateMediaStartNodeIds(_entityService), UmbracoObjectTypes.Media, "media/mediaRoot", context); target.CreateDate = source.CreateDate; target.Culture = source.GetUserCulture(_textService, _globalSettings).ToString(); target.Email = source.Email; @@ -283,10 +283,10 @@ namespace Umbraco.Web.Models.Mapping target.Navigation = CreateUserEditorNavigation(); target.ParentId = -1; target.Path = "-1," + source.Id; - target.StartContentIds = GetStartNodes(source.StartContentIds.ToArray(), UmbracoObjectTypes.Document, "content/contentRoot", context.Mapper); - target.StartMediaIds = GetStartNodes(source.StartMediaIds.ToArray(), UmbracoObjectTypes.Media, "media/mediaRoot", context.Mapper); + target.StartContentIds = GetStartNodes(source.StartContentIds.ToArray(), UmbracoObjectTypes.Document, "content/contentRoot", context); + target.StartMediaIds = GetStartNodes(source.StartMediaIds.ToArray(), UmbracoObjectTypes.Media, "media/mediaRoot", context); target.UpdateDate = source.UpdateDate; - target.UserGroups = source.Groups.Select(context.Mapper.Map); + target.UserGroups = source.Groups.Select(context.Map); target.Username = source.Username; target.UserState = source.UserState; } @@ -307,7 +307,7 @@ namespace Umbraco.Web.Models.Mapping target.Name = source.Name; target.ParentId = -1; target.Path = "-1," + source.Id; - target.UserGroups = source.Groups.Select(context.Mapper.Map); + target.UserGroups = source.Groups.Select(context.Map); target.Username = source.Username; target.UserState = source.UserState; } @@ -333,18 +333,18 @@ namespace Umbraco.Web.Models.Mapping // helpers - private void MapUserGroupBasic(UserGroupBasic target, IEnumerable sourceAllowedSections, int? sourceStartContentId, int? sourceStartMediaId, UmbracoMapper mapper) + private void MapUserGroupBasic(UserGroupBasic target, IEnumerable sourceAllowedSections, int? sourceStartContentId, int? sourceStartMediaId, MapperContext context) { var allSections = _sectionService.GetSections(); - target.Sections = allSections.Where(x => sourceAllowedSections.Contains(x.Alias)).Select(mapper.Map
); + target.Sections = allSections.Where(x => sourceAllowedSections.Contains(x.Alias)).Select(context.Map
); if (sourceStartMediaId > 0) - target.MediaStartNode = mapper.Map(_entityService.Get(sourceStartMediaId.Value, UmbracoObjectTypes.Media)); + target.MediaStartNode = context.Map(_entityService.Get(sourceStartMediaId.Value, UmbracoObjectTypes.Media)); else if (sourceStartMediaId == -1) target.MediaStartNode = CreateRootNode(_textService.Localize("media/mediaRoot")); if (sourceStartContentId > 0) - target.ContentStartNode = mapper.Map(_entityService.Get(sourceStartContentId.Value, UmbracoObjectTypes.Document)); + target.ContentStartNode = context.Map(_entityService.Get(sourceStartContentId.Value, UmbracoObjectTypes.Document)); else if (sourceStartContentId == -1) target.ContentStartNode = CreateRootNode(_textService.Localize("content/contentRoot")); @@ -377,7 +377,7 @@ namespace Umbraco.Web.Models.Mapping private static string MapContentTypeIcon(EntitySlim entity) => entity is ContentEntitySlim contentEntity ? contentEntity.ContentTypeIcon : null; - private IEnumerable GetStartNodes(int[] startNodeIds, UmbracoObjectTypes objectType, string localizedKey, UmbracoMapper mapper) + private IEnumerable GetStartNodes(int[] startNodeIds, UmbracoObjectTypes objectType, string localizedKey, MapperContext context) { if (startNodeIds.Length <= 0) return Enumerable.Empty(); @@ -387,7 +387,7 @@ namespace Umbraco.Web.Models.Mapping startNodes.Add(CreateRootNode(_textService.Localize(localizedKey))); var mediaItems = _entityService.GetAll(objectType, startNodeIds); - startNodes.AddRange(mapper.Map>(mediaItems)); + startNodes.AddRange(context.Map>(mediaItems)); return startNodes; }