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 override void Execute(ConfigurationResolverArgs args)
        {
            if (args.Parameters != null && args.Parameters.ContainsKey(MultiInterfaceTypesKey) && args.RequestedType.IsInterface)
            {
                var types = args.Parameters[MultiInterfaceTypesKey] as IEnumerable <Type>;

                if (types != null && types.All(x => x.IsInterface))
                {
                    var typeContext  = args.AbstractTypeCreationContext;
                    var originalType = typeContext.RequestedType;
                    args.Parameters.Remove(MultiInterfaceTypesKey);
                    var configuations = new List <AbstractTypeConfiguration>();
                    foreach (var type in types)
                    {
                        typeContext.RequestedType = type;
                        var result = args.Service.RunConfigurationPipeline(typeContext);
                        configuations.Add(result.Result);
                    }
                    args.Parameters[CreateMultiInferaceTask.MultiInterfaceConfigsKey] = configuations;
                    args.Parameters[MultiInterfaceTypesKey] = types;
                    typeContext.RequestedType = originalType;
                }
            }

            base.Execute(args);
        }
Exemple #3
0
        /// <summary>
        /// Executes the specified args.
        /// </summary>
        /// <param name="args">The args.</param>
        public override 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
                        }
                    }
                }
            }

            base.Execute(args);
        }
 public void Execute(ConfigurationResolverArgs args)
 {
     if (args.Result == null && args.RequestedType == ItemType)
     {
         args.Result = Config;
     }
 }
        /// <summary>
        /// Executes the specified args.
        /// </summary>
        /// <param name="args">The args.</param>
        public override 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 bestType = FindBestType(scContext.SitecoreService.Database.Templates[templateId], configs,
                                                requestedType);
                    if (bestType != null)
                    {
                        args.Result = bestType;
                        if (!_inferredCache.TryAdd(key, args.Result as SitecoreTypeConfiguration))
                        {
                            //TODO: some logging
                        }
                    }
                }
            }
            base.Execute(args);
        }
Exemple #6
0
        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);
        }
        public override void Execute(ConfigurationResolverArgs args)
        {
            if (args.Parameters != null && args.Parameters.ContainsKey(MultiInterfaceTypesKey) && args.RequestedType.IsInterface)
            {
                var types = args.Parameters[MultiInterfaceTypesKey] as IEnumerable<Type>;
               
                if (types!= null && types.All(x => x.IsInterface))
                {
                    var typeContext = args.AbstractTypeCreationContext;
                    var originalType = typeContext.RequestedType;
                    args.Parameters.Remove(MultiInterfaceTypesKey);
                    var configuations = new List<AbstractTypeConfiguration>();
                    foreach (var type in types)
                    {
                        typeContext.RequestedType = type;
                        var result = args.Service.RunConfigurationPipeline(typeContext);
                        configuations.Add(result.Result);
                    }
                    args.Parameters[CreateMultiInferaceTask.MultiInterfaceConfigsKey] = configuations;
                    args.Parameters[MultiInterfaceTypesKey] = types;
                    typeContext.RequestedType = originalType;
                }
            }

            base.Execute(args);

        }
 public override void Execute(ConfigurationResolverArgs args)
 {
     if (args.Result != null)
     {
         args.Result.GetTypeOptions(args.Options);
     }
 }
Exemple #9
0
 /// <summary>
 /// Executes the specified args.
 /// </summary>
 /// <param name="args">The args.</param>
 public void Execute(ConfigurationResolverArgs args)
 {
     if (args.Result == null)
     {
         args.Result = args.Context[args.AbstractTypeCreationContext.RequestedType];
     }
 }
Exemple #10
0
        public void Execute_RunsLoader_TypeAddedToContext()
        {
            //Arrange

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

            context.Config = new Config();
            var typeContext = new TestTypeCreationContext();

            typeContext.Options = new TestGetOptions()
            {
                Type = typeof(StubClass)
            };

            var args = new ConfigurationResolverArgs(context, typeContext, null);


            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);
        }
Exemple #11
0
        public void Execute_ResultAlreadySet_DoesNotRunLoader()
        {
            //Arrange

            var resolver    = Substitute.For <IDependencyResolver>();
            var context     = Context.Create(resolver);
            var typeContext = new TestTypeCreationContext();

            typeContext.Options = new TestGetOptions()
            {
                Type = typeof(StubClass)
            };


            var args = new ConfigurationResolverArgs(context, typeContext, 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);
        }
Exemple #12
0
        public void Execute_OnDemandDisabled_ThrowsException()
        {
            //Arrange

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

            context.Config = new Config();
            context.Config.OnDemandMappingEnabled = false;

            var typeContext = new TestTypeCreationContext();

            typeContext.Options = new TestGetOptions()
            {
                Type = typeof(StubClass)
            };

            var args = new ConfigurationResolverArgs(context, typeContext, null);


            var task = new ConfigurationOnDemandResolverTask <StubTypeConfiguration>();

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

            //Act
            Assert.Throws <MapperException>(
                () => { task.Execute(args); },
                Constants.Errors.OnDemandDisabled);
        }
Exemple #13
0
        public void Execute_SecondRequestFromCacheInferredType()
        {
            //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 args1       = new ConfigurationResolverArgs(context, typeContext, null, null);
            var args2       = new ConfigurationResolverArgs(context, typeContext, null, null);
            var task        = new TemplateInferredTypeTask();

            typeContext.InferType     = true;
            typeContext.Item          = db.GetItem(path);
            typeContext.RequestedType = typeof(IBase);
            args1.RequestedType       = typeof(IBase);
            args2.RequestedType       = typeof(IBase);



            //Act
            task.Execute(args1);
            task.Execute(args2);


            //Assert
            Assert.IsNotNull(args1.Result);
            Assert.AreEqual(typeof(StubInferred), args1.Result.Type);
            //Assert
            Assert.IsNotNull(args2.Result);
            Assert.AreEqual(typeof(StubInferred), args2.Result.Type);
        }
        /// <summary>
        /// Executes the specified args.
        /// </summary>
        /// <param name="args">The args.</param>
        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 != null ? item.TemplateID : scContext.TemplateId;

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

                    var types = configs.Where(x => x.TemplateId == templateId);
                    if (types.Any())
                    {
                        var type = types.FirstOrDefault(x => requestedType.First().IsAssignableFrom(x.Type));
                        if (type != null)
                        {
                            args.Result = new[] { type }
                        }
                        ;
                    }
                }
            }
        }

        #endregion
    }
        /// <summary>
        /// Executes the specified args.
        /// </summary>
        /// <param name="args">The args.</param>
        public void Execute(ConfigurationResolverArgs args)
        {
            if (args.Result == null)
            {
                args.Result = args.Context[args.RequestedType];
            }

        }
Exemple #16
0
 /// <summary>
 /// Executes the specified args.
 /// </summary>
 /// <param name="args">The args.</param>
 public override void Execute(ConfigurationResolverArgs args)
 {
     if (args.Result == null)
     {
         args.Result = args.Context[args.RequestedType];
     }
     base.Execute(args);
 }
        public override void Execute(ConfigurationResolverArgs args)
        {
            if (args.Result == null && args.Options.Type == ItemType)
            {
                args.Result = Config;
            }

            base.Execute(args);
        }
 /// <summary>
 /// Executes the specified args.
 /// </summary>
 /// <param name="args">The args.</param>
 public void Execute(ConfigurationResolverArgs args)
 {
     if (args.Result == null)
     {
         var loader = new OnDemandLoader <T>(args.AbstractTypeCreationContext.RequestedType);
         args.Context.Load(loader);
         args.Result = args.Context[args.AbstractTypeCreationContext.RequestedType];
     }
 }
        public ConfigurationResolverArgs RunConfigurationPipeline(AbstractTypeCreationContext abstractTypeCreationContext)
        {
            var configurationArgs = new ConfigurationResolverArgs(GlassContext, abstractTypeCreationContext, abstractTypeCreationContext.RequestedType, this);

            configurationArgs.Parameters = abstractTypeCreationContext.Parameters;
            _configurationResolver.Run(configurationArgs);

            return(configurationArgs);
        }
 /// <summary>
 /// Executes the specified args.
 /// </summary>
 /// <param name="args">The args.</param>
 public override void Execute(ConfigurationResolverArgs args)
 {
     if (args.Result == null)
     {
         var loader = new OnDemandLoader <T>(args.RequestedType);
         args.Context.Load(loader);
         args.Result = args.Context[args.RequestedType];
     }
     base.Execute(args);
 }
Exemple #21
0
        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);
        }
Exemple #22
0
        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);
        }
Exemple #23
0
        public void Execute_ResultNotNull_Returns()
        {
            //Arrange
            var args     = new ConfigurationResolverArgs(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);

            args.Result = expected;

            //Act
            task.Execute(args);

            //Arrange
            Assert.AreEqual(expected, args.Result);
        }
Exemple #25
0
        public void Execute_MultipleRequestsUnloadedType_AddsOnlyOnceToContext()
        {
            //Arrange
            var resolver    = Substitute.For <IDependencyResolver>();
            var context     = Context.Create(resolver);
            var typeContext = new TestTypeCreationContext();

            context.Config = new Config();



            var task     = new ConfigurationOnDemandResolverTask <StubTypeConfiguration>();
            var argsList = new List <ConfigurationResolverArgs>();

            Action taskFunc = () =>
            {
                typeContext.Options = new TestGetOptions()
                {
                    Type = typeof(StubClass)
                };
                var args = new ConfigurationResolverArgs(context, typeContext, 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(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_RequestedTypeItem_ConfigForItem()
        {
            //Arrange
            SitecoreTypeConfiguration expected = SitecoreItemResolverTask.Config;
            var task    = new SitecoreItemResolverTask();
            var context = new SitecoreTypeCreationContext();

            context.Options = new GetItemOptions()
            {
                Type = typeof(Item)
            };

            var args = new ConfigurationResolverArgs(null, context, null);

            //Act
            task.Execute(args);

            //Arrange
            Assert.AreEqual(expected, args.Result);
        }
Exemple #28
0
        /// <summary>
        /// Instantiates the object.
        /// </summary>
        /// <param name="abstractTypeCreationContext">The abstract type creation context.</param>
        /// <returns></returns>
        /// <exception cref="System.NullReferenceException">Configuration Resolver pipeline did not return a type. Has the type been loaded by Glass.Mapper. Type: {0}.Formatted(abstractTypeCreationContext.RequestedType.FullName)</exception>
        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);
        }