public void Execute_CreatesInferredType()
        {
            //Arrange  
            using (Db database = new Db
            {
                new DbTemplate(new ID(StubInferred.TemplateId)),
                new Sitecore.FakeDb.DbItem("Target", ID.NewID, new ID(StubInferred.TemplateId))
            })
            {
                var context = Context.Create(FakeDb.Utilities.CreateStandardResolver());
                var path = "/sitecore/content/Target";

                context.Load(new AttributeTypeLoader(typeof(StubInferred)));


                var typeContext = new SitecoreTypeCreationContext();
                var args = new ConfigurationResolverArgs(context, typeContext, null, null);
                var task = new TemplateInferredTypeTask();
                typeContext.InferType = true;
                typeContext.Item = database.GetItem(path);
                typeContext.RequestedType = typeof(IBase);
                args.RequestedType = typeof(IBase);



                //Act
                task.Execute(args);


                //Assert
                Assert.IsNotNull(args.Result);
                Assert.AreEqual(typeof(StubInferred), args.Result.Type);
            }
        }
        public void Execute_FindsFirstTypeMatchedInConfigurationsList_ReturnsConfiguration()
        {
            //Assign

            Substitute.For<IGlassConfiguration>();

            var type = typeof (StubClass);
            
            var configuration = Substitute.For<AbstractTypeConfiguration>();
            configuration.Type = type;
            
            var loader = Substitute.For<IConfigurationLoader>();
            loader.Load().Returns(new [] {configuration});

            var context = Context.Create(Substitute.For<IDependencyResolver>());
            
            context.Load(loader);

            var args = new ConfigurationResolverArgs(context, new StubAbstractTypeCreationContext()
                {
                    RequestedType = type
                }, type, null);

            var task = new ConfigurationStandardResolverTask();

            //Act
            task.Execute(args);

            //Assert
            Assert.AreEqual(configuration, args.Result);


        }
        /// <summary>
        /// Executes the specified args.
        /// </summary>
        /// <param name="args">The args.</param>
        public void Execute(ConfigurationResolverArgs args)
        {
            if (args.Result == null && args.AbstractTypeCreationContext.InferType)
            {
                var scContext = args.AbstractTypeCreationContext as SitecoreTypeCreationContext;

                var requestedType = scContext.RequestedType;
                var item = scContext.Item;
                var templateId = item != null ? item.TemplateID : scContext.TemplateId;

                var key = new Tuple<Context, Type, ID>(args.Context, requestedType, templateId);
                if (_inferredCache.ContainsKey(key))
                {
                    args.Result = _inferredCache[key];
                }
                else
                {
                    var configs = args.Context.TypeConfigurations.Select(x => x.Value as SitecoreTypeConfiguration);

                    var types = configs.Where(x => x.TemplateId == templateId);
                    if (types.Any())
                    {
                        args.Result = types.FirstOrDefault(x => requestedType.IsAssignableFrom(x.Type));
                        if (!_inferredCache.TryAdd(key, args.Result as SitecoreTypeConfiguration))
                        {
                            //TODO: some logging
                        }
                    }
                }
            }
        }
        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 Execute(ConfigurationResolverArgs args)
 {
     if (args.Result == null && args.RequestedType == ItemType)
     {
         args.Result = Config;
     }
 }
        public void Execute_CreatesInferredType()
        {
            //Arrange  
            var db = Sitecore.Configuration.Factory.GetDatabase("master");
            var context = Context.Create(Utilities.CreateStandardResolver());
            var path = "/sitecore/content/Tests/Pipelines/ConfigurationResolver/TemplateInferredTypeTask/Target";

            context.Load(new AttributeTypeLoader(typeof(StubInferred)));
            

            var typeContext = new SitecoreTypeCreationContext();
            var args = new ConfigurationResolverArgs(context, typeContext, null, null);
            var task = new TemplateInferredTypeTask();
            typeContext.InferType = true;
            typeContext.Item = db.GetItem(path);
            typeContext.RequestedType = typeof (IBase);
            args.RequestedType = typeof (IBase);

            

            //Act
            task.Execute(args);


            //Assert
            Assert.IsNotNull(args.Result);
            Assert.AreEqual(typeof(StubInferred), args.Result.Type);
        }
        public void Execute_RequestedTypeItem_ConfigForItem()
        {
            //Arrange
            SitecoreTypeConfiguration expected = SitecoreItemResolverTask.Config;
            var task = new SitecoreItemResolverTask();
            var args = new ConfigurationResolverArgs(null, null, typeof(Item), null);

            //Act
            task.Execute(args);

            //Arrange
            Assert.AreEqual(expected, args.Result);
        }
        public void Execute_ResultNotNull_Returns()
        {
            //Arrange
            var args = new ConfigurationResolverArgs(null, null, null, null);
            var task = new TemplateInferredTypeTask();
            var expected = new SitecoreTypeConfiguration();
            args.Result = expected;

            //Act
            task.Execute(args);

            //Assert
            Assert.AreEqual(expected, args.Result);
        }
        public void Execute_ResultIsNotNull_DoesNothing()
        {
            //Arrange
            var expected = new SitecoreTypeConfiguration();
            var task = new SitecoreItemResolverTask();
            var args = new ConfigurationResolverArgs(null, null, null, null);
            args.Result = expected;

            //Act
            task.Execute(args);

            //Arrange
            Assert.AreEqual(expected, args.Result);
        }
        public void Execute_NotInferred_ResultNull()
        {
            //Arrange
            var args = new ConfigurationResolverArgs(null, new SitecoreTypeCreationContext(), null, null);
            var task = new TemplateInferredTypeTask();
            args.AbstractTypeCreationContext.InferType = false;

            //Act
            task.Execute(args);
            
            //Assert
            Assert.IsNull(args.Result);
            
        }
        public void Execute(ConfigurationResolverArgs args)
        {
            if (args.AbstractTypeCreationContext.InferType)
            {
                var umbContext = args.AbstractTypeCreationContext as UmbracoTypeCreationContext;
                if (umbContext != null)
                {
                    var content = umbContext.Content;

                    var configs = args.Context.TypeConfigurations.Select(config => config.Value as UmbracoTypeConfiguration);
                    var types = configs.Where(x => x.ContentTypeAlias == content.ContentType.Alias);

                    if (!types.Any()) return;
                    args.Result = types.FirstOrDefault();
                }
            }
        }
        public void Execute(ConfigurationResolverArgs args)
        {
            if (args.Result == null)
            {
                if (args.AbstractTypeCreationContext.InferType)
                {
                    var scContext = args.AbstractTypeCreationContext as SitecoreTypeCreationContext;

                    var requestedType = scContext.RequestedType;
                    var item = scContext.Item;
                    var templateId = item.TemplateID;

                    var configs = args.Context.TypeConfigurations.Select(x => x.Value as SitecoreTypeConfiguration);

                    var types = configs.Where(x => x.TemplateId == templateId);
                    args.Result = types.FirstOrDefault(x => requestedType.IsAssignableFrom(x.Type));
                }
            }
        }
        public void Execute_ResultAlreadySet_DoesNotRunLoader()
        {
            //Arrange

            var resolver = NSubstitute.Substitute.For<IDependencyResolver>();
            var context = Context.Create(resolver);
            var typeContext = NSubstitute.Substitute.For<AbstractTypeCreationContext>();
            typeContext.RequestedType = typeof (StubClass);
            var args = new ConfigurationResolverArgs(context, typeContext, typeContext.RequestedType, null);
            args.Result = new StubTypeConfiguration();

            var task = new ConfigurationOnDemandResolverTask<StubTypeConfiguration>();

            Assert.AreEqual(0, context.TypeConfigurations.Count);

            //Act
            task.Execute(args);

            //Assert
            Assert.AreEqual(0, context.TypeConfigurations.Count);
        }
        public void Execute_MultipleRequestsUnloadedType_AddsOnlyOnceToContext()
        {
            //Arrange
            var resolver = NSubstitute.Substitute.For<IDependencyResolver>();
            var context = Context.Create(resolver);
            var typeContext = NSubstitute.Substitute.For<AbstractTypeCreationContext>();
            
            var task = new ConfigurationOnDemandResolverTask<StubTypeConfiguration>();
            var argsList = new List<ConfigurationResolverArgs>();

            Action taskFunc = () =>
            {
                typeContext.RequestedType = typeof(StubClass);
                var args = new ConfigurationResolverArgs(context, typeContext, typeContext.RequestedType, null);
                
                argsList.Add(args);
                
                task.Execute(args);
            };
    

            Assert.AreEqual(0, context.TypeConfigurations.Count);

            //Act
            var tasks = new []
            {
                new Task(taskFunc),
                new Task(taskFunc),
                new Task(taskFunc),
                new Task(taskFunc),
                new Task(taskFunc),
                new Task(taskFunc),
            };
            Parallel.ForEach(tasks, x => x.Start());
            Task.WaitAll(tasks);

            //Assert
            Assert.AreEqual(1, context.TypeConfigurations.Count);
            Assert.IsTrue(argsList.All(x => x.Result.Type == typeof(StubClass)));
        }
        public void Execute_RunsLoader_TypeAddedToContext()
        {
            //Arrange

            var resolver = NSubstitute.Substitute.For<IDependencyResolver>();
            var context = Context.Create(resolver);
            var typeContext = NSubstitute.Substitute.For<AbstractTypeCreationContext>();
            var args = new ConfigurationResolverArgs(context, typeContext);
            typeContext.RequestedType = typeof(StubClass);

            var task = new ConfigurationOnDemandResolverTask<StubTypeConfiguration>();

            Assert.AreEqual(0, context.TypeConfigurations.Count);

            //Act
            task.Execute(args);

            //Assert
            Assert.AreEqual(1, context.TypeConfigurations.Count);
            Assert.IsNotNull(context.TypeConfigurations[typeof(StubClass)]);
            Assert.AreEqual(typeof(StubClass), args.Result.Type);
        }
예제 #16
0
        public ConfigurationResolverArgs RunConfigurationPipeline(AbstractTypeCreationContext abstractTypeCreationContext)
        {
            var configurationArgs = new ConfigurationResolverArgs(GlassContext, abstractTypeCreationContext, abstractTypeCreationContext.RequestedType, this);
            configurationArgs.Parameters = abstractTypeCreationContext.Parameters;
            _configurationResolver.Run(configurationArgs);

            return configurationArgs;
        }
예제 #17
0
        public object InstantiateObject(AbstractTypeCreationContext abstractTypeCreationContext)
        {
            //run the pipeline to get the configuration to load
            var configurationArgs = new ConfigurationResolverArgs(GlassContext, abstractTypeCreationContext);
            _configurationResolver.Run(configurationArgs);
            
            if (configurationArgs.Result == null)
                throw new NullReferenceException("Configuration Resolver pipeline did not return a type. Has the type been loaded by Glass.Mapper. Type: {0}".Formatted(abstractTypeCreationContext.RequestedType.FullName));

            var config = configurationArgs.Result;

            //Run the object construction
            var objectArgs = new ObjectConstructionArgs(GlassContext, abstractTypeCreationContext, config, this);
            _objectConstruction.Run(objectArgs);

            return objectArgs.Result;
        }