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
18 using Castle
.MonoRail
.Framework
.Descriptors
;
21 /// Decorates a controller associating a <see cref="IFilter"/>
22 /// implementation with it. More than one can be associated.
25 /// If more than one filter is associate with an action (or controller and
26 /// action), the order of execution cannot be predicted. In this case
27 /// use <see cref="ExecutionOrder"/> to define the order of execution.
29 [AttributeUsage(AttributeTargets
.Class
, AllowMultiple
=true, Inherited
=true), Serializable
]
30 public class FilterAttribute
: Attribute
, IFilterDescriptorBuilder
32 private readonly Type filterType
;
33 private readonly ExecuteEnum when
;
34 private int executionOrder
= Int32
.MaxValue
;
37 /// Constructs a FilterAttribute associating
38 /// the filter type and when the filter should be invoked.
40 /// <param name="when">When to execute the filter</param>
41 /// <param name="filterType">The filter implementation</param>
42 public FilterAttribute(ExecuteEnum when
, Type filterType
)
44 if (!typeof(IFilter
).IsAssignableFrom(filterType
))
46 throw new ArgumentException("The specified type does not implement IFilter");
49 this.filterType
= filterType
;
54 /// Gets the filter implementation type
56 public Type FilterType
58 get { return filterType; }
62 /// Gets when to run the filter
64 public ExecuteEnum When
70 /// Gets or sets the filter execution order.
71 /// The lower the value, the higher the priority
73 public int ExecutionOrder
75 get { return executionOrder; }
76 set { executionOrder = value; }
80 /// Implementation of <see cref="IFilterDescriptorBuilder"/>.
81 /// Returns the descriptor for this filter association.
83 /// <returns></returns>
84 public FilterDescriptor
[] BuildFilterDescriptors()
86 return new FilterDescriptor
[] { new FilterDescriptor(FilterType, when, executionOrder, this) }
;