private IEnumerable <IMedia> MapDtosToContent(List <ContentDto> dtos, bool withCache = false) { var temps = new List <TempContent <Core.Models.Media> >(); var contentTypes = new Dictionary <int, IMediaType?>(); var content = new Core.Models.Media[dtos.Count]; for (var i = 0; i < dtos.Count; i++) { ContentDto dto = dtos[i]; if (withCache) { // if the cache contains the (proper version of the) item, use it IMedia?cached = IsolatedCache.GetCacheItem <IMedia>(RepositoryCacheKeys.GetKey <IMedia, int>(dto.NodeId)); if (cached != null && cached.VersionId == dto.ContentVersionDto.Id) { content[i] = (Core.Models.Media)cached; continue; } } // else, need to build it // get the content type - the repository is full cache *but* still deep-clones // whatever comes out of it, so use our own local index here to avoid this var contentTypeId = dto.ContentTypeId; if (contentTypes.TryGetValue(contentTypeId, out IMediaType? contentType) == false) { contentTypes[contentTypeId] = contentType = _mediaTypeRepository.Get(contentTypeId); } Core.Models.Media c = content[i] = ContentBaseFactory.BuildEntity(dto, contentType); // need properties var versionId = dto.ContentVersionDto.Id; temps.Add(new TempContent <Core.Models.Media>(dto.NodeId, versionId, 0, contentType, c)); } // load all properties for all documents from database in 1 query - indexed by version id IDictionary <int, PropertyCollection> properties = GetPropertyCollections(temps); // assign properties foreach (TempContent <Core.Models.Media> temp in temps) { if (temp.Content is not null) { temp.Content.Properties = properties[temp.VersionId]; // reset dirty initial properties (U4-1946) temp.Content.ResetDirtyProperties(false); } } return(content); }
private IMedia MapDtoToContent(ContentDto dto) { IMediaType?contentType = _mediaTypeRepository.Get(dto.ContentTypeId); Core.Models.Media media = ContentBaseFactory.BuildEntity(dto, contentType); // get properties - indexed by version id var versionId = dto.ContentVersionDto.Id; var temp = new TempContent <Core.Models.Media>(dto.NodeId, versionId, 0, contentType); IDictionary <int, PropertyCollection> properties = GetPropertyCollections(new List <TempContent <Core.Models.Media> > { temp }); media.Properties = properties[versionId]; // reset dirty initial properties (U4-1946) media.ResetDirtyProperties(false); return(media); }
/// <summary> /// Builds an IMedia item from a dto and content type. /// </summary> public static Core.Models.Media BuildEntity(ContentDto dto, IMediaType contentType) { var nodeDto = dto.NodeDto; var contentVersionDto = dto.ContentVersionDto; var content = new Core.Models.Media(nodeDto.Text, nodeDto.ParentId, contentType); try { content.DisableChangeTracking(); content.Id = dto.NodeId; content.Key = nodeDto.UniqueId; content.VersionId = contentVersionDto.Id; // TODO: missing names? content.Path = nodeDto.Path; content.Level = nodeDto.Level; content.ParentId = nodeDto.ParentId; content.SortOrder = nodeDto.SortOrder; content.Trashed = nodeDto.Trashed; content.CreatorId = nodeDto.UserId ?? Cms.Core.Constants.Security.UnknownUserId; content.WriterId = contentVersionDto.UserId ?? Cms.Core.Constants.Security.UnknownUserId; content.CreateDate = nodeDto.CreateDate; content.UpdateDate = contentVersionDto.VersionDate; // reset dirty initial properties (U4-1946) content.ResetDirtyProperties(false); return(content); } finally { content.EnableChangeTracking(); } }