Пример #1
0
        /// <summary>
        /// Creates a map to a strong type from an Umbraco document type.
        /// </summary>
        /// <typeparam name="TDestination">The type to map to.</typeparam>
        /// <param name="documentTypeAlias">The document type alias to map from.</param>
        /// <returns>Further mapping configuration</returns>
        /// <exception cref="DocumentTypeNotFoundException">
        /// If the <paramref name="documentTypeAlias"/> could not be found
        /// </exception>
        public INodeMappingExpression <TDestination> CreateMap <TDestination>(string documentTypeAlias)
            where TDestination : class, new()
        {
            var destinationType = typeof(TDestination);

            // Remove current mapping if any
            if (NodeMappers.ContainsKey(destinationType))
            {
                NodeMappers.Remove(destinationType);
            }

            // Get document type
            var docType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(documentTypeAlias);

            if (docType == null)
            {
                throw new DocumentTypeNotFoundException(documentTypeAlias);
            }

            var nodeMapper = new NodeMapper(this, destinationType, docType);

            NodeMappers[destinationType] = nodeMapper;

            if (_cacheProvider != null)
            {
                _cacheProvider.Clear();
            }

            return(new NodeMappingExpression <TDestination>(this, nodeMapper));
        }
Пример #2
0
        /// <summary>
        /// Creates a map to a strong type from an Umbraco document type.
        /// </summary>
        /// <typeparam name="TDestination">The type to map to.</typeparam>
        /// <param name="documentTypeAlias">The document type alias to map from.</param>
        /// <returns>Further mapping configuration</returns>
        /// <exception cref="DocumentTypeNotFoundException">
        /// If the <paramref name="documentTypeAlias"/> could not be found
        /// </exception>
        public INodeMappingExpression <TDestination> CreateMap <TDestination>(string documentTypeAlias)
            where TDestination : class, new()
        {
            var destinationType = typeof(TDestination);

            // Remove current mapping if any
            if (NodeMappers.ContainsKey(destinationType))
            {
                NodeMappers.Remove(destinationType);
            }

            // Get document type
            var docType = DocumentType.GetAllAsList()
                          .SingleOrDefault(d => string.Equals(d.Alias, documentTypeAlias, StringComparison.InvariantCultureIgnoreCase));

            if (docType == null)
            {
                throw new DocumentTypeNotFoundException(documentTypeAlias);
            }

            var nodeMapper = new NodeMapper(this, destinationType, docType);

            NodeMappers[destinationType] = nodeMapper;

            if (_cacheProvider != null)
            {
                _cacheProvider.Clear();
            }

            return(new NodeMappingExpression <TDestination>(this, nodeMapper));
        }
Пример #3
0
        /// <summary>
        /// Maps a node based on the <paramref name="context"/>.
        /// </summary>
        /// <param name="context">The context which describes the node mapping.</param>
        /// <param name="destinationType">The type to map to.</param>
        /// <returns><c>null</c> if the node does not exist.</returns>
        internal object Map(NodeMappingContext context, Type destinationType)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            else if (destinationType == null)
            {
                throw new ArgumentNullException("destinationType");
            }
            else if (!NodeMappers.ContainsKey(destinationType))
            {
                throw new MapNotFoundException(destinationType);
            }

            string sourceNodeTypeAlias = null;

            if (_cacheProvider != null &&
                _cacheProvider.ContainsAlias(context.Id))
            {
                sourceNodeTypeAlias = _cacheProvider.GetAlias(context.Id);

                if (sourceNodeTypeAlias == null)
                {
                    // Node does not exist
                    return(null);
                }
            }

            if (sourceNodeTypeAlias == null)
            {
                var node = context.GetNode();

                if (node == null || string.IsNullOrEmpty(node.Name))
                {
                    // Node doesn't exist
                    if (_cacheProvider != null)
                    {
                        _cacheProvider.InsertAlias(context.Id, null);
                    }

                    return(null);
                }

                if (_cacheProvider != null)
                {
                    _cacheProvider.InsertAlias(context.Id, node.DocumentTypeAlias);
                }

                sourceNodeTypeAlias = node.DocumentTypeAlias;
            }

            var nodeMapper = GetMapper(sourceNodeTypeAlias, destinationType);

            if (nodeMapper == null)
            {
                return(null);
            }

            return(nodeMapper.MapNode(context));
        }