Exemple #1
0
        /// <summary>
        /// Compiles the specified template.
        /// </summary>
        /// <param name="razorTemplate">The string template.</param>
        /// <param name="modelType">The model type.</param>
        /// <param name="cacheName">The name of the template type in the cache.</param>
        public void Compile(string razorTemplate, Type modelType, string cacheName)
        {
            Contract.Requires(razorTemplate != null);
            Contract.Requires(cacheName != null);

            int hashCode = razorTemplate.GetHashCode();

            Type type = CreateTemplateType(razorTemplate, modelType);
            var  item = new CachedTemplateItem(hashCode, type);

            _cache.AddOrUpdate(cacheName, item, (n, i) => item);
        }
Exemple #2
0
        /// <summary>
        /// Compiles the specified template.
        /// </summary>
        /// <param name="razorTemplate">The string template.</param>
        /// <param name="modelType">The model type.</param>
        /// <param name="name">The name of the template.</param>
        public void Compile(string razorTemplate, Type modelType, string name, bool flag)
        {
            //需重新加载或内存中没有加载程序集时
            if (flag || !_cache.ContainsKey(name))
            {
                int hashCode = razorTemplate.GetHashCode();

                Type type = CreateTemplateType(razorTemplate, modelType, name, flag);
                var  item = new CachedTemplateItem(hashCode, type);

                _cache.AddOrUpdate(name, item, (n, i) => item);
            }
        }
Exemple #3
0
        /// <summary>
        /// Gets an instance of the template using the cached compiled type, or compiles the template type
        /// if it does not exist in the cache.
        /// </summary>
        /// <param name="razorTemplate">The string template.</param>
        /// <param name="name">The name of the template type in the cache.</param>
        /// <returns>An instance of <see cref="ITemplate"/>.</returns>
        public virtual ITemplate GetTemplate(string razorTemplate, string name)
        {
            if (razorTemplate == null)
            {
                throw new ArgumentNullException("razorTemplate");
            }

            int hashCode = razorTemplate.GetHashCode();

            CachedTemplateItem item;

            if (!(_cache.TryGetValue(name, out item) && item.CachedHashCode == hashCode))
            {
                Type type = CreateTemplateType(razorTemplate);
                item = new CachedTemplateItem(hashCode, type);

                _cache.AddOrUpdate(name, item, (n, i) => item);
            }

            return(CreateTemplate(item.TemplateType));
        }
Exemple #4
0
        /// <summary>
        /// Gets an instance of the template using the cached compiled type, or compiles the template type
        /// if it does not exist in the cache.
        /// </summary>
        /// <typeparam name="T">Type of the model</typeparam>
        /// <param name="razorTemplate">The string template.</param>
        /// <param name="model">The model instance or NULL if there is no model for this template.</param>
        /// <param name="cacheName">The name of the template type in the cache.</param>
        /// <returns>An instance of <see cref="ITemplate{T}"/>.</returns>
        private ITemplate GetTemplate <T>(string razorTemplate, object model, string cacheName)
        {
            if (razorTemplate == null)
            {
                throw new ArgumentNullException("razorTemplate");
            }

            int hashCode = razorTemplate.GetHashCode();

            CachedTemplateItem item;

            if (!(_cache.TryGetValue(cacheName, out item) && item.CachedHashCode == hashCode))
            {
                Type type = CreateTemplateType(razorTemplate, (model == null) ? typeof(T) : model.GetType());
                item = new CachedTemplateItem(hashCode, type);

                _cache.AddOrUpdate(cacheName, item, (n, i) => item);
            }

            var instance = CreateTemplate(null, item.TemplateType, model);

            return(instance);
        }