/// <summary> /// Initializes a new instance of the <see cref="ViewComponentDescriptor"/> class. /// </summary> /// <param name="isCacheable">if set to <c>true</c> [is cacheable].</param> /// <param name="cacheStrategy">The cache strategy.</param> /// <param name="cacheKeyGenerator">The cache key generator.</param> public ViewComponentDescriptor(bool isCacheable, ViewComponentCache cacheStrategy, IViewComponentCacheKeyGenerator cacheKeyGenerator) { this.isCacheable = isCacheable; this.cacheStrategy = cacheStrategy; this.cacheKeyGenerator = cacheKeyGenerator; }
/// <summary> /// Creates a <see cref="ViewComponentDescriptor"/> by inspecting the /// specified view component type. /// </summary> /// <param name="viewComponentType">Type of the view component.</param> /// <returns></returns> public ViewComponentDescriptor Collect(Type viewComponentType) { if (viewComponentType == null) { throw new ArgumentNullException("viewComponentType"); } locker.AcquireReaderLock(-1); try { if (type2Desc.ContainsKey(viewComponentType)) { return(type2Desc[viewComponentType]); } } finally { locker.ReleaseReaderLock(); } var attrs = viewComponentType.GetCustomAttributes(typeof(ViewComponentDetailsAttribute), true); ViewComponentDescriptor descriptor; if (attrs.Length == 0) { descriptor = ViewComponentDescriptor.Empty; } else { var details = (ViewComponentDetailsAttribute)attrs[0]; IViewComponentCacheKeyGenerator generator = null; if (details.Cache == ViewComponentCache.Always) { generator = new AlwaysCacheKeyGenerator(); } else if (details.CacheKeyFactory != null) { try { generator = (IViewComponentCacheKeyGenerator) Activator.CreateInstance(details.CacheKeyFactory); } catch (Exception ex) { throw new MonoRailException( "Could not instantiate IViewComponentCacheKeyGenerator implementation or " + "it does not implement this interface. Type: " + details.CacheKeyFactory.FullName, ex); } } descriptor = new ViewComponentDescriptor(details.Cache != ViewComponentCache.Disabled, details.Cache, generator); } locker.UpgradeToWriterLock(-1); try { type2Desc[viewComponentType] = descriptor; } finally { locker.ReleaseWriterLock(); } return(descriptor); }