[代码] 模板生成器,依赖上面的文件编译器。也是,整个Razor编译器的入口。

using System;
using System.Collections;
using Skight.Arch.Domain.Containers;

namespace Skight.Arch.Presentation.Web.Core.ViewEngins.TemplateProvider
{
    [RegisterInContainer(LifeCycle.singleton)]
    public class TemplateGenerator
    {
        private CachedFileCompiler internal_compiler;

        public TemplateGenerator(CachedFileCompiler internalCompiler)
        {
            internal_compiler = internalCompiler;
        }

        public TemplateBase<T> generate<T>(T model, IDictionary context, string path)
        {
            var type= internal_compiler.compile_template<T>(path);
            var instance = (TemplateBase<T>)Activator.CreateInstance(type);
            instance.Path = path;
            instance.Model = model;
            instance.Context = context;
            instance.Url = new UrlHelper(context);
            instance.Decoder = new DecodingHelper(context);
            return instance;
        }

        public TemplateBase generate(IDictionary context, string path) {
            var type = internal_compiler.compile_template(path);
            var instance = (TemplateBase)Activator.CreateInstance(type);
            instance.Path = path;
            instance.Context = context;
            instance.Url = new UrlHelper(context);
            instance.Decoder = new DecodingHelper(context);
            return instance;
        }
    }
}