public void Execute(ConfigurationResolverArgs args)
        {
            if (args.Result == null && args.RequestedType == typeof (ILazyModelMaker))
            {
                var scContext = args.AbstractTypeCreationContext as SitecoreTypeCreationContext;

                //get the ids for all the base templates
                var ids = scContext.Item.Template.BaseTemplates.Select(x => x.ID)
                        .Union(new[] {scContext.Item.TemplateID})
                        .Distinct();

                //find the requested interface type.
                var lazyConfig = args.Context[args.RequestedType];
                if (lazyConfig == null)
                {
                    var loader = new OnDemandLoader<SitecoreTypeConfiguration>(args.RequestedType);
                    args.Context.Load(loader);
                    lazyConfig = args.Context[args.RequestedType];
                }

                //find all the other interfaces for the base types
                var configs =
                    args.Context.TypeConfigurations.Where(x => x.Value is SitecoreTypeConfiguration).Select(x=>x.Value)
                        .Cast<SitecoreTypeConfiguration>();

                //throw all those configurations into the results list.
                args.Result = new[] {lazyConfig}.Union(
                        ids.Select(x => configs.FirstOrDefault(y => y.TemplateId == x && y.Type.IsInterface))
                        .Where(x => x != null));
            }
        }
        public void Load_StubClassConfigured_ReturnsStubClassAndProperties()
        {
            //Assign
            var loader = new OnDemandLoader <SitecoreTypeConfiguration>(typeof(StubClass));

            //Act
            var results = loader.Load();

            //Assert
            Assert.GreaterOrEqual(results.Count(), 0);

            var typeConfig = results.First(x => x.Type == typeof(StubClass));

            Assert.IsNotNull(typeConfig);

            var propertyNames = new[] { "Children", "Field", "Id", "Info", "Linked", "Node", "Parent", "Query" };

            foreach (var propertyName in propertyNames)
            {
                var propInfo = typeof(StubClass).GetProperty(propertyName);
                Assert.IsTrue(typeConfig.Properties.Any(x => x.PropertyInfo == propInfo));
            }
        }
        public void Load_StubClassConfigured_ReturnsStubClassAndProperties()
        {
            //Assign
            var loader = new OnDemandLoader<SitecoreTypeConfiguration>(typeof(StubClass));

            //Act
            var results = loader.Load();
            
            //Assert
            Assert.GreaterOrEqual(results.Count(), 0);

            var typeConfig = results.First(x => x.Type == typeof (StubClass));
            Assert.IsNotNull(typeConfig);

            var propertyNames = new[] {"Children", "Field", "Id", "Info", "Linked", "Node", "Parent", "Query"};

            foreach(var propertyName in propertyNames)
            {
                var propInfo = typeof (StubClass).GetProperty(propertyName);
                Assert.IsTrue(typeConfig.Properties.Any(x=>x.PropertyInfo == propInfo));
            }

        }
Пример #4
0
        /// <summary>
        /// Gets the object.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="db">The db.</param>
        /// <returns></returns>
        /// <exception cref="Glass.Mapper.MapperException">Failed to find context {0}.Formatted(ContextName)</exception>
        public virtual object GetObject(string model, Database db, Rendering renderingItem)
        {
            if (model.IsNullOrEmpty())
            {
                return(null);
            }

            //must be a path to a Model item
            if (model.StartsWith("/sitecore"))
            {
                var target = db.GetItem(model);
                if (target == null)
                {
                    return(null);
                }

                string newModel = target[ModelTypeField];
                return(GetObject(newModel, db, renderingItem));
            }
            //if guid must be that to Model item
            Guid targetId;

            if (Guid.TryParse(model, out targetId))
            {
                var target = db.GetItem(new ID(targetId));
                if (target == null)
                {
                    return(null);
                }

                string newModel = target[ModelTypeField];
                return(GetObject(newModel, db, renderingItem));
            }


            var type = Type.GetType(model, false);

            if (type == null || renderingModelType.IsAssignableFrom(type))
            {
                return(null);
            }

            ISitecoreContext scContext = SitecoreContextFactory.GetSitecoreContext();


            //this is really aggressive
            if (!scContext.GlassContext.TypeConfigurations.ContainsKey(type))
            {
                //if the config is null then it is probably an ondemand mapping so we have to load the ondemand part

                IConfigurationLoader loader =
                    new OnDemandLoader <SitecoreTypeConfiguration>(type);
                scContext.GlassContext.Load(loader);
            }

            if (renderingItem.DataSource.IsNotNullOrEmpty())
            {
                var item = scContext.Database.GetItem(renderingItem.DataSource);
                return(scContext.CreateType(type, item, false, false, null));
            }

            if (renderingItem.RenderingItem.DataSource.HasValue())
            {
                var item = scContext.Database.GetItem(renderingItem.RenderingItem.DataSource);
                return(scContext.CreateType(type, item, false, false, null));
            }

            /**
             * Issues #82:
             * Check Item before defaulting to the current item.
             */
            if (renderingItem.Item != null)
            {
                return(scContext.CreateType(type, renderingItem.Item, false, false, null));
            }

            return(scContext.GetCurrentItem(type));
        }
Пример #5
0
        /// <summary>
        /// Gets the object.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="db">The db.</param>
        /// <returns></returns>
        /// <exception cref="Glass.Mapper.MapperException">Failed to find context {0}.Formatted(ContextName)</exception>
        public object GetObject(string model, Database db, Rendering renderingItem)
        {

            if (model.IsNullOrEmpty())
                return null;

            //must be a path to a Model item
            if (model.StartsWith("/sitecore"))
            {
                var target = db.GetItem(model);
                if (target == null)
                    return null;

                string newModel = target[ModelTypeField];
                return GetObject(newModel, db, renderingItem);
            }
            //if guid must be that to Model item
            Guid targetId;
            if (Guid.TryParse(model, out targetId))
            {
                var target = db.GetItem(new ID(targetId));
                if (target == null)
                    return null;

                string newModel = target[ModelTypeField];
                return GetObject(newModel, db, renderingItem);
            }


            var type = Type.GetType(model, false);

            if (type == null)
                return null;

            var context = Context.Contexts.ContainsKey(ContextName) ? Context.Contexts[ContextName] : null;
            if (context == null) throw new MapperException("Failed to find context {0}".Formatted(ContextName));

            //this is really aggressive
            if (!context.TypeConfigurations.ContainsKey(type))
            {
                //if the config is null then it is probably an ondemand mapping so we have to load the ondemand part

                IConfigurationLoader loader =
                    new OnDemandLoader<SitecoreTypeConfiguration>(type);
                context.Load(loader);

            }
            ISitecoreContext scContext = new SitecoreContext(context);

            if (renderingItem.DataSource.IsNotNullOrEmpty())
            {
                var item = scContext.Database.GetItem(renderingItem.DataSource);
                return scContext.CreateType(type, item, false, false, null);
            }

            return scContext.GetCurrentItem(type);

        }
Пример #6
0
        /// <summary>
        /// Gets the object.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="db">The db.</param>
        /// <returns></returns>
        /// <exception cref="Glass.Mapper.MapperException">Failed to find context {0}.Formatted(ContextName)</exception>
        public virtual object GetObject(string model, Database db, Rendering renderingItem)
        {
            if (model.IsNullOrEmpty())
            {
                return(null);
            }

            //must be a path to a Model item
            if (model.StartsWith("/sitecore"))
            {
                var target = db.GetItem(model);
                if (target == null)
                {
                    return(null);
                }

                string newModel = target[ModelTypeField];
                return(GetObject(newModel, db, renderingItem));
            }
            //if guid must be that to Model item
            Guid targetId;

            if (Guid.TryParse(model, out targetId))
            {
                var target = db.GetItem(new ID(targetId));
                if (target == null)
                {
                    return(null);
                }

                string newModel = target[ModelTypeField];
                return(GetObject(newModel, db, renderingItem));
            }


            var type = Type.GetType(model, false);

            if (type == null || renderingModelType.IsAssignableFrom(type))
            {
                return(null);
            }

            IMvcContext mvcContext = new MvcContext(new SitecoreService(Sitecore.Context.Database));

            //this is really aggressive
            if (!mvcContext.SitecoreService.GlassContext.TypeConfigurations.ContainsKey(type))
            {
                //if the config is null then it is probably an ondemand mapping so we have to load the ondemand part

                IConfigurationLoader loader =
                    new OnDemandLoader <SitecoreTypeConfiguration>(type);
                mvcContext.SitecoreService.GlassContext.Load(loader);
            }

            if (renderingItem.DataSource.HasValue())
            {
                var getOptions = new GetItemByPathOptions()
                {
                    Path = renderingItem.DataSource,
                    Type = type
                };

                return(mvcContext.SitecoreService.GetItem(getOptions));
            }

            if (renderingItem.RenderingItem.DataSource.HasValue())
            {
                var getOptions = new GetItemByPathOptions()
                {
                    Path = renderingItem.RenderingItem.DataSource,
                    Type = type
                };
                return(mvcContext.SitecoreService.GetItem(getOptions));
            }

            /**
             * Issues #82:
             * Check Item before defaulting to the current item.
             */
            if (renderingItem.Item != null)
            {
                var getOptions = new GetItemByItemOptions()
                {
                    Item = renderingItem.Item,
                    Type = type
                };
                return(mvcContext.SitecoreService.GetItem(getOptions));
            }
            else
            {
                //TODO? shoudl we use the GetCurrentitem
                var getOptions = new GetItemByItemOptions()
                {
                    Item = mvcContext.ContextItem,
                    Type = type
                };
                return(mvcContext.SitecoreService.GetItem(getOptions));
            }
        }
Пример #7
0
        /// <summary>
        /// Gets the object.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="db">The db.</param>
        /// <returns></returns>
        /// <exception cref="Glass.Mapper.MapperException">Failed to find context {0}.Formatted(ContextName)</exception>
        public virtual object GetObject(string model, Database db, Rendering renderingItem)
        {

            if (model.IsNullOrEmpty())
                return null;

            //must be a path to a Model item
            if (model.StartsWith("/sitecore"))
            {
                var target = db.GetItem(model);
                if (target == null)
                    return null;

                string newModel = target[ModelTypeField];
                return GetObject(newModel, db, renderingItem);
            }
            //if guid must be that to Model item
            Guid targetId;
            if (Guid.TryParse(model, out targetId))
            {
                var target = db.GetItem(new ID(targetId));
                if (target == null)
                    return null;

                string newModel = target[ModelTypeField];
                return GetObject(newModel, db, renderingItem);
            }


            var type = Type.GetType(model, false);

            if (type == null || renderingModelType.IsAssignableFrom(type))
                return null;
           
            ISitecoreContext scContext = SitecoreContextFactory.GetSitecoreContext();


            //this is really aggressive
            if (!scContext.GlassContext.TypeConfigurations.ContainsKey(type))
            {
                //if the config is null then it is probably an ondemand mapping so we have to load the ondemand part

                IConfigurationLoader loader =
                    new OnDemandLoader<SitecoreTypeConfiguration>(type);
                scContext.GlassContext.Load(loader);

            }

            if (renderingItem.DataSource.IsNotNullOrEmpty())
            {
                var item = scContext.Database.GetItem(renderingItem.DataSource);
                return scContext.CreateType(type, item, false, false, null);
            }

            if (renderingItem.RenderingItem.DataSource.HasValue())
            {
                var item = scContext.Database.GetItem(renderingItem.RenderingItem.DataSource);
                return scContext.CreateType(type, item, false, false, null);
            }

            /**
             * Issues #82:
             * Check Item before defaulting to the current item.
             */
            if (renderingItem.Item != null)
            {
                return scContext.CreateType(type, renderingItem.Item, false, false, null);
            }

            return scContext.GetCurrentItem(type);

        }
Пример #8
0
        /// <summary>
        /// Gets the object.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="db">The db.</param>
        /// <returns></returns>
        /// <exception cref="Glass.Mapper.MapperException">Failed to find context {0}.Formatted(ContextName)</exception>
        public object GetObject(string model, Database db, Rendering renderingItem)
        {
            if (model.IsNullOrEmpty())
            {
                return(null);
            }

            //must be a path to a Model item
            if (model.StartsWith("/sitecore"))
            {
                var target = db.GetItem(model);
                if (target == null)
                {
                    return(null);
                }

                string newModel = target[ModelTypeField];
                return(GetObject(newModel, db, renderingItem));
            }
            //if guid must be that to Model item
            Guid targetId;

            if (Guid.TryParse(model, out targetId))
            {
                var target = db.GetItem(new ID(targetId));
                if (target == null)
                {
                    return(null);
                }

                string newModel = target[ModelTypeField];
                return(GetObject(newModel, db, renderingItem));
            }


            var type = Type.GetType(model, false);

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

            var context = Context.Contexts.ContainsKey(ContextName) ? Context.Contexts[ContextName] : null;

            if (context == null)
            {
                throw new MapperException("Failed to find context {0}".Formatted(ContextName));
            }

            //this is really aggressive
            if (!context.TypeConfigurations.ContainsKey(type))
            {
                //if the config is null then it is probably an ondemand mapping so we have to load the ondemand part

                IConfigurationLoader loader =
                    new OnDemandLoader <SitecoreTypeConfiguration>(type);
                context.Load(loader);
            }
            ISitecoreContext scContext = new SitecoreContext(context);

            if (renderingItem.DataSource.IsNotNullOrEmpty())
            {
                var item = scContext.Database.GetItem(renderingItem.DataSource);
                return(scContext.CreateType(type, item, false, false, null));
            }

            return(scContext.GetCurrentItem(type));
        }