protected virtual void SerializeModel(object model, Stream stream)
 {
     using (var sw = new StreamWriter(stream, Encoding.UTF8, 0x100, true))
         using (var lease = _serializerPool.Rent())
         {
             lease.Resource.Serialize(sw, model);
         }
 }
Пример #2
0
 /// <summary>
 /// Transform from view model to the final result using template
 /// Supported template languages are mustache and liquid
 /// </summary>
 /// <param name="model">The input view model</param>
 /// <returns>The output after applying template</returns>
 public string Transform(object model)
 {
     if (_rendererPool == null || model == null)
     {
         return(null);
     }
     using (var lease = _rendererPool.Rent())
     {
         return(lease.Resource.Render(model));
     }
 }
Пример #3
0
        public object GetOptions(object model)
        {
            if (!ContainsGetOptions)
            {
                return(null);
            }

            using var lease = _preprocessorPool.Rent();
            try
            {
                return(lease.Resource.GetOptions(model));
            }
            catch (Exception e)
            {
                throw new InvalidPreprocessorException($"Error running GetOptions function inside template preprocessor: {e.Message}");
            }
        }
Пример #4
0
        public string Render(object model)
        {
            if (model == null)
            {
                return(null);
            }

            using var lease = _rendererPool.Rent();
            return(lease.Resource?.Render(model));
        }
Пример #5
0
        private object ProcessWithJint(object model, object attrs, object global)
        {
            var argument1 = JintProcessorHelper.ConvertStrongTypeToJsValue(model);
            var argument2 = JintProcessorHelper.ConvertStrongTypeToJsValue(attrs);
            var argument3 = JintProcessorHelper.ConvertStrongTypeToJsValue(global);

            using (var lease = _enginePool.Rent())
            {
                return(lease.Resource.Invoke("transform", argument1, argument2, argument3).ToObject());
            }
        }
Пример #6
0
        public RendererWithResourcePool(Func <ITemplateRenderer> creater, int maxParallelism)
        {
            _rendererPool = ResourcePool.Create(creater, maxParallelism);

            using var lease = _rendererPool.Rent();
            var inner = lease.Resource;

            Raw          = inner.Raw;
            Dependencies = inner.Dependencies;
            Path         = inner.Path;
            Name         = inner.Name;
        }
Пример #7
0
        public Template(string name, DocumentBuildContext context, TemplateRendererResource templateResource, TemplatePreprocessorResource scriptResource, ResourceCollection resourceCollection, int maxParallelism)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            Name = name;
            var templateInfo = GetTemplateInfo(Name);

            Extension    = templateInfo.Extension;
            Type         = templateInfo.DocumentType;
            TemplateType = templateInfo.TemplateType;
            _script      = scriptResource?.Content;
            if (!string.IsNullOrWhiteSpace(_script))
            {
                ScriptName        = Name + ".js";
                _preprocessorPool = ResourcePool.Create(() => CreatePreprocessor(resourceCollection, scriptResource, context), maxParallelism);
                try
                {
                    using (var preprocessor = _preprocessorPool.Rent())
                    {
                        ContainsGetOptions          = preprocessor.Resource.GetOptionsFunc != null;
                        ContainsModelTransformation = preprocessor.Resource.TransformModelFunc != null;
                    }
                }
                catch (Exception e)
                {
                    _preprocessorPool = null;
                    Logger.LogWarning($"{ScriptName} is not a valid template preprocessor, ignored: {e.Message}");
                }
            }

            if (!string.IsNullOrEmpty(templateResource?.Content) && resourceCollection != null)
            {
                _rendererPool            = ResourcePool.Create(() => CreateRenderer(resourceCollection, templateResource), maxParallelism);
                ContainsTemplateRenderer = true;
            }

            if (!ContainsGetOptions && !ContainsModelTransformation && !ContainsTemplateRenderer)
            {
                Logger.LogWarning($"Template {name} contains neither preprocessor to process model nor template to render model. Please check if the template is correctly defined. Allowed preprocessor functions are [exports.getOptions] and [exports.transform].");
            }

            Resources = ExtractDependentResources(Name);
        }
Пример #8
0
        public Template(string name, DocumentBuildContext context, TemplateRendererResource templateResource, TemplatePreprocessorResource scriptResource, ResourceCollection resourceCollection, int maxParallelism)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            Name = name;
            var templateInfo = GetTemplateInfo(Name);
            Extension = templateInfo.Extension;
            Type = templateInfo.DocumentType;
            TemplateType = templateInfo.TemplateType;
            _script = scriptResource?.Content;
            if (!string.IsNullOrWhiteSpace(_script))
            {
                ScriptName = Name + ".js";
                _preprocessorPool = ResourcePool.Create(() => CreatePreprocessor(resourceCollection, scriptResource, context), maxParallelism);
                try
                {
                    using (var preprocessor = _preprocessorPool.Rent())
                    {
                        ContainsGetOptions = preprocessor.Resource.GetOptionsFunc != null;
                        ContainsModelTransformation = preprocessor.Resource.TransformModelFunc != null;
                    }
                }
                catch (Exception e)
                {
                    _preprocessorPool = null;
                    Logger.LogWarning($"{ScriptName} is not a valid template preprocessor, ignored: {e.Message}");
                }
            }

            if (!string.IsNullOrEmpty(templateResource?.Content) && resourceCollection != null)
            {
                _rendererPool = ResourcePool.Create(() => CreateRenderer(resourceCollection, templateResource), maxParallelism);
                ContainsTemplateRenderer = true;
            }

            if (!ContainsGetOptions && !ContainsModelTransformation && !ContainsTemplateRenderer)
            {
                Logger.LogWarning($"Template {name} contains neither preprocessor to process model nor template to render model. Please check if the template is correctly defined. Allowed preprocessor functions are [exports.getOptions] and [exports.transform].");
            }

            Resources = ExtractDependentResources(Name);
        }
Пример #9
0
 public PreprocessorWithResourcePool(Func <ITemplatePreprocessor> creater, int maxParallelism)
 {
     _preprocessorPool = ResourcePool.Create(creater, maxParallelism);
     try
     {
         using (var preprocessor = _preprocessorPool.Rent())
         {
             var inner = preprocessor.Resource;
             ContainsGetOptions          = inner.ContainsGetOptions;
             ContainsModelTransformation = inner.ContainsModelTransformation;
             Path = inner.Path;
             Name = inner.Name;
         }
     }
     catch (Exception e)
     {
         _preprocessorPool = null;
         Logger.LogWarning($"Not a valid template preprocessor, ignored: {e.Message}");
     }
 }