1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.MonoRail
.Framework
20 /// Identifies the cache strategy associated with a view component
22 public enum ViewComponentCache
29 /// Always cache the view component output, no varying.
33 /// Uses a custom key generator that should implement the vary algorithm.
35 UseCustomCacheKeyGenerator
39 /// Decorates a <see cref="ViewComponent"/> to associate a custom name with it.
42 /// Decorates a <see cref="ViewComponent"/> to associate a custom name with it.
44 /// Optionally you can associate the section names supported by the
45 /// <see cref="ViewComponent"/>.
49 /// In the code below, the class MyHeaderViewConponent will be referenced as just <c>Header</c>,
50 /// and it will support the subsections <c>header</c> and <c>footer</c>.
52 /// [ViewComponentDetails("Header", Sections="header,footer")
53 /// public class MyHeaderViewComponent : ViewComponent
61 /// <seealso cref="ViewComponent"/>
62 /// <seealso cref="ViewComponentParamAttribute"/>
63 [AttributeUsage(AttributeTargets
.Class
), Serializable
]
64 public class ViewComponentDetailsAttribute
: Attribute
66 private readonly string name
;
67 private string sections
;
68 private ViewComponentCache cache
= ViewComponentCache
.Disabled
;
69 private Type cacheKeyFactory
;
70 private string[] sectionsFromAttribute
;
73 /// Initializes a new instance of the <see cref="ViewComponentDetailsAttribute"/> class.
75 /// <param name="name">The specified ViewComponent's Name</param>
76 public ViewComponentDetailsAttribute(String name
)
82 /// The component's name
90 /// Sets the nested sections that this <see cref="ViewComponent"/> supports.
92 /// <value>The nested sections names, comma separated.</value>
93 public string Sections
95 get { return sections; }
99 if (!string.IsNullOrEmpty(sections
))
101 sectionsFromAttribute
= sections
.Split(new char[] {',', ' ', '|'}
, StringSplitOptions
.RemoveEmptyEntries
);
104 if (sectionsFromAttribute
== null)
106 sectionsFromAttribute
= new string[0];
112 /// Sets the cache strategy.
114 /// <value>The cache.</value>
115 public ViewComponentCache Cache
117 get { return cache; }
118 set { cache = value; }
122 /// Sets the cache key factory.
124 /// <value>The cache key factory.</value>
125 public Type CacheKeyFactory
127 get { return cacheKeyFactory; }
132 cache
= ViewComponentCache
.UseCustomCacheKeyGenerator
;
134 cacheKeyFactory
= value;
139 /// Returns true if the section name specified is present on the list of sections.
141 /// <param name="name">The section name.</param>
142 /// <returns></returns>
143 public bool SupportsSection(string name
)
145 return Array
.FindIndex(sectionsFromAttribute
,
146 delegate(string item
) { return string.Equals(item, name, StringComparison.InvariantCultureIgnoreCase); }
) != -1;