Exemple #1
0
        /// <summary>
        /// Search and compile a template with a given key
        /// </summary>
        /// <param name="key">Unique key of the template</param>
        /// <param name="compileIfNotCached">If true - it will try to get a template with a specified key and compile it</param>
        /// <returns>An instance of a template</returns>
        public async Task <ITemplatePage> CompileTemplateAsync(string key, bool isEncoding = true)
        {
            if (IsCachingEnabled)
            {
                var cacheLookupResult = Cache.RetrieveTemplate(key);
                if (cacheLookupResult.Success)
                {
                    var cacheTemplate = cacheLookupResult.Template.TemplatePageFactory();
                    cacheTemplate.DisableEncoding = !isEncoding;
                    return(cacheTemplate);
                }
            }

            CompiledTemplateDescriptor templateDescriptor = await Compiler.CompileAsync(key);

            Func <ITemplatePage> templateFactory = FactoryProvider.CreateFactory(templateDescriptor);

            if (IsCachingEnabled)
            {
                Cache.CacheTemplate(
                    key,
                    templateFactory,
                    templateDescriptor.ExpirationToken);
            }

            var template = templateFactory();

            template.DisableEncoding = !isEncoding;
            return(template);
        }
Exemple #2
0
        /// <summary>
        /// Search and compile a template with a given key
        /// </summary>
        /// <param name="key">Unique key of the template</param>
        /// <param name="compileIfNotCached">If true - it will try to get a template with a specified key and compile it</param>
        /// <returns>An instance of a template</returns>
        public async Task <ITemplatePage> CompileTemplateAsync(string key)
        {
            if (IsCachingEnabled)
            {
                var cacheLookupResult = TemplateCache.RetrieveTemplate(key);
                if (cacheLookupResult.Success)
                {
                    return(cacheLookupResult.Template.TemplatePageFactory());
                }
            }

            CompiledTemplateDescriptor templateDescriptor = await TemplateCompiler.CompileAsync(key);

            Func <ITemplatePage> templateFactory = TemplateFactoryProvider.CreateFactory(templateDescriptor);

            if (IsCachingEnabled)
            {
                TemplateCache.CacheTemplate(
                    key,
                    templateFactory,
                    templateDescriptor.ExpirationToken);
            }

            return(templateFactory());
        }
Exemple #3
0
        /// <summary>
        /// Search and compile a template with a given key
        /// </summary>
        /// <param name="key">Unique key of the template</param>
        /// <returns>An instance of a template</returns>
        public async Task <ITemplatePage> CompileTemplateAsync(string key)
        {
            ITemplatePage templatePage = null;

            if (IsCachingEnabled)
            {
                var cacheLookupResult = Cache.RetrieveTemplate(key);
                if (cacheLookupResult.Success)
                {
                    templatePage = cacheLookupResult.Template.TemplatePageFactory();
                }
            }

            if (templatePage == null)
            {
                CompiledTemplateDescriptor templateDescriptor = await Compiler.CompileAsync(key);

                Func <ITemplatePage> templateFactory = FactoryProvider.CreateFactory(templateDescriptor);

                if (IsCachingEnabled)
                {
                    Cache.CacheTemplate(
                        key,
                        templateFactory,
                        templateDescriptor.ExpirationToken);
                }

                templatePage = templateFactory();
            }

            templatePage.DisableEncoding = Options.DisableEncoding ?? false;
            return(templatePage);
        }
Exemple #4
0
        public void Ensure_FactoryReturnsValidTemplateType()
        {
            string templateKey = "testKey";

            var templateFactoryProvider = new TemplateFactoryProvider();
            var descriptor = new CompiledTemplateDescriptor
            {
                TemplateAttribute = new RazorLightTemplateAttribute(templateKey, typeof(TestFactoryClass)),
                TemplateKey       = templateKey
            };

            Func <ITemplatePage> result = templateFactoryProvider.CreateFactory(descriptor);

            Assert.NotNull(result);
            Assert.IsAssignableFrom <TemplatePage>(result());
        }
        public async Task Compiler_Takes_Result_From_Cache_OnCompileAsync()
        {
            string templateKey    = "key";
            var    descriptor     = new CompiledTemplateDescriptor();
            var    descriptorTask = Task.FromResult(descriptor);

            var compiler = TestRazorTemplateCompiler.Create();

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            compiler.Cache.Set(templateKey, descriptorTask);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

            CompiledTemplateDescriptor result = await compiler.CompileAsync(templateKey);

            Assert.NotNull(result);
            Assert.Same(descriptor, result);
        }
        public async Task Compiler_Searches_WithNormalizedKey_IfNotFound()
        {
            string templateKey    = "key";
            var    descriptor     = new CompiledTemplateDescriptor();
            var    descriptorTask = Task.FromResult(descriptor);

            var project  = new FileSystemRazorProject("/");
            var compiler = TestRazorTemplateCompiler.Create(project: project);

            string normalizedKey = compiler.GetNormalizedKey(templateKey);

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            compiler.Cache.Set(normalizedKey, descriptorTask);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

            CompiledTemplateDescriptor result = await compiler.CompileAsync(templateKey);

            Assert.NotNull(result);
            Assert.Same(descriptor, result);
        }
 public Func <ITemplatePage> CreateFactory(CompiledTemplateDescriptor templateDescriptor)
 {
     throw new NotImplementedException();
 }