Пример #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);
        }
Пример #2
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);
        }