1 // Copyright 2004-2008 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 System
.Reflection
;
20 using Castle
.Components
.Binder
;
23 /// The DataBind Attribute is used to indicate that an Action methods parameter
24 /// is to be intercepted and handled by the <see cref="Castle.Components.Binder.DataBinder"/>.
27 /// Allowed usage is one per method parameter, and is not inherited.
29 [AttributeUsage(AttributeTargets
.Parameter
, AllowMultiple
=false, Inherited
=false)]
30 public class DataBindAttribute
: Attribute
, IParameterBinder
32 private ParamStore
from = ParamStore
.Params
;
33 private string exclude
= string.Empty
;
34 private string allow
= string.Empty
;
35 private string prefix
;
36 private bool validate
;
39 /// Creates a <see cref="DataBindAttribute"/>
40 /// with an associated prefix. The prefix must be present
41 /// in the form data and is used to avoid name clashes.
43 /// <param name="prefix"></param>
44 public DataBindAttribute(string prefix
)
50 /// Gets or sets the property names to exclude.
52 /// <remarks>The property name should include the <i>prefix</i>.</remarks>
53 /// <value>A comma separated list
54 /// of property names to exclude from databinding.</value>
57 get { return exclude; }
58 set { exclude = value; }
62 /// Gets or sets the property names to allow.
64 /// <remarks>The property name should include the <i>prefix</i>.</remarks>
65 /// <value>A comma separated list
66 /// of property names to allow from databinding.</value>
70 set { allow = value; }
74 /// Gets or sets a value indicating whether
75 /// the target should be validate during binding.
77 /// <value><c>true</c> if should be validated; otherwise, <c>false</c>.</value>
80 get { return validate; }
81 set { validate = value; }
85 /// Gets or sets <see cref="ParamStore"/> used to
86 /// indicate where to get the values from
88 /// <value>The <see cref="ParamStore"/> type.
89 /// Typically <see cref="ParamStore.Params"/>.</value>
90 public ParamStore From
97 /// Gets the databinding prefix.
100 /// The prefix is a name followed by a
101 /// dot that prefixes the entries names
102 /// on the source http request.
104 /// <value>The databinding prefix.</value>
107 get { return prefix; }
111 /// Implementation of <see cref="IParameterBinder.CalculateParamPoints"/>
112 /// and it is used to give the method a weight when overloads are available.
114 /// <param name="context">The context.</param>
115 /// <param name="controller">The controller instance</param>
116 /// <param name="controllerContext">The controller context.</param>
117 /// <param name="parameterInfo">The parameter info</param>
119 /// Positive value if the parameter can be bound
121 public int CalculateParamPoints(IEngineContext context
, IController controller
, IControllerContext controllerContext
, ParameterInfo parameterInfo
)
123 CompositeNode node
= context
.Request
.ObtainParamsNode(From
);
125 IDataBinder binder
= CreateBinder();
127 return binder
.CanBindObject(parameterInfo
.ParameterType
, prefix
, node
) ? 10 : 0;
131 /// Implementation of <see cref="IParameterBinder.Bind"/>
132 /// and it is used to read the data available and construct the
133 /// parameter type accordingly.
135 /// <param name="context">The context.</param>
136 /// <param name="controller">The controller instance</param>
137 /// <param name="controllerContext">The controller context.</param>
138 /// <param name="parameterInfo">The parameter info</param>
139 /// <returns>The bound instance</returns>
140 public virtual object Bind(IEngineContext context
, IController controller
, IControllerContext controllerContext
, ParameterInfo parameterInfo
)
142 IDataBinder binder
= CreateBinder();
143 IValidatorAccessor validatorAccessor
= controller
as IValidatorAccessor
;
145 ConfigureValidator(validatorAccessor
, binder
);
147 CompositeNode node
= context
.Request
.ObtainParamsNode(From
);
149 object instance
= binder
.BindObject(parameterInfo
.ParameterType
, prefix
, exclude
, allow
, node
);
151 BindInstanceErrors(validatorAccessor
, binder
, instance
);
152 PopulateValidatorErrorSummary(validatorAccessor
, binder
, instance
);
158 /// Creates the binder.
160 /// <returns></returns>
161 protected virtual IDataBinder
CreateBinder()
163 return new DataBinder();
167 /// Configures the validator.
169 /// <param name="validatorExposer">The validator exposer.</param>
170 /// <param name="binder">The binder.</param>
171 protected virtual void ConfigureValidator(IValidatorAccessor validatorExposer
, IDataBinder binder
)
173 if (validate
&& validatorExposer
== null)
175 throw new MonoRailException("DataBind wants to validate, but controller does not seem to implement IValidatorAccessor interface");
180 binder
.Validator
= validatorExposer
.Validator
;
184 binder
.Validator
= null;
189 /// Populates the validator error summary.
191 /// <param name="validatorExposer">The validator exposer.</param>
192 /// <param name="binder">The binder.</param>
193 /// <param name="instance">The instance.</param>
194 protected virtual void PopulateValidatorErrorSummary(IValidatorAccessor validatorExposer
, IDataBinder binder
, object instance
)
198 validatorExposer
.PopulateValidatorErrorSummary(instance
, binder
.GetValidationSummary(instance
));
203 /// Binds the instance errors.
205 /// <param name="validatorExposer">The validator exposer.</param>
206 /// <param name="binder">The binder.</param>
207 /// <param name="instance">The instance.</param>
208 protected virtual void BindInstanceErrors(IValidatorAccessor validatorExposer
, IDataBinder binder
, object instance
)
210 if (instance
!= null)
212 validatorExposer
.BoundInstanceErrors
[instance
] = binder
.ErrorList
;