public DocumentMappingResult <TElement> MapToType <TElement>(Func <Dictionary <string, string> > getFieldsMethod, SelectMethod selectMethod)
        {
            // if the result type is not IStandardTemplateItem, use the default functionality
            if (!IsSynthesisType <TElement>())
            {
                return(new DocumentMappingResult <TElement>(default(TElement), false));
            }

            // initializers can't really support sub-selects of objects. Error if that's what's being used.
            if (selectMethod != null)
            {
                throw new NotSupportedException("Using Select on a Synthesis object type is supported. Convert the query to a list or array before selecting, then select using LINQ to objects.");
            }

            var evaluatedFields = getFieldsMethod();

            ShortID templateId;

            if (!evaluatedFields.ContainsKey("_template") || !ShortID.TryParse(evaluatedFields["_template"], out templateId))
            {
                templateId = ID.Null.ToShortID();
            }

            var initializer = _overrideInitializer ?? ProviderResolver.FindGlobalInitializer(templateId.ToID());

            var result = initializer.CreateInstanceFromSearch(evaluatedFields);

            if (result is TElement)
            {
                return(new DocumentMappingResult <TElement>((TElement)result, true));
            }

            return(new DocumentMappingResult <TElement>(default(TElement), true));           // note that this is still 'success', because we mapped onto a Synthesis type so we do not want to use default mapping
        }
Example #2
0
        /// <summary>
        /// Converts an Item into a strongly typed item
        /// </summary>
        /// <returns>The converted item, or null if the conversion failed</returns>
        /// <remarks>If a conversion exception occurs, it can be found in the Sitecore log</remarks>
        public static IStandardTemplateItem AsStronglyTyped(this Item item)
        {
            if (item == null)
            {
                return(null);
            }

            ITemplateInitializer initializer = ProviderResolver.FindGlobalInitializer(item.TemplateID);

            return(initializer.CreateInstance(item));
        }