예제 #1
0
        public object CreateElementInstance(Type baseType, IDictionary <string, object> fieldValues, IEnumerable <IExecutionContext> executionContexts)
        {
            var typeConfig = Context.Default.GetTypeConfiguration <SitecoreTypeConfiguration>(baseType);

            if (typeConfig == null || typeConfig.TemplateId == (ID)null)
            {
                return(_defaultDocumentMapper.CreateElementInstance(baseType, fieldValues, executionContexts));
            }

            var sitecoreService     = new SitecoreContext();
            var typeCreationContext = new SitecoreTypeCreationContext
            {
                SitecoreService = sitecoreService,
                RequestedType   = baseType,
                InferType       = true,
                IsLazy          = true,
                TemplateId      = ID.Parse(fieldValues[BuiltinFields.Template]),
            };

            using (new SearchSwitcher())
            {
                var proxy = sitecoreService.InstantiateObject(typeCreationContext);
                SetupProxy(ID.Parse(fieldValues[BuiltinFields.Group]), fieldValues, proxy as IProxyTargetAccessor);
                return(proxy);
            }
        }
예제 #2
0
        protected virtual T CreateInstance <T>(string templateId)
        {
            var typeCreationContext = new SitecoreTypeCreationContext
            {
                SitecoreService = _sitecoreContext,
                RequestedType   = typeof(T),
                InferType       = true,
                IsLazy          = true,
                TemplateId      = new ID(templateId)
            };

            using (new SearchSwitcher()) return((T)_sitecoreContext.InstantiateObject(typeCreationContext));
        }