Fixing an issue with output parameters that are of type IntPtr
[castle.git] / MonoRail / Castle.MonoRail.Framework / Attributes / DataBindAttribute.cs
blobfc9ad1f27d9f64784d49aae03a679ade4b4e91d9
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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
17 using System;
18 using System.Reflection;
20 using Castle.Components.Binder;
22 /// <summary>
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"/>.
25 /// </summary>
26 /// <remarks>
27 /// Allowed usage is one per method parameter, and is not inherited.
28 /// </remarks>
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;
38 /// <summary>
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.
42 /// </summary>
43 /// <param name="prefix"></param>
44 public DataBindAttribute(string prefix)
46 this.prefix = prefix;
49 /// <summary>
50 /// Gets or sets the property names to exclude.
51 /// </summary>
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>
55 public string Exclude
57 get { return exclude; }
58 set { exclude = value; }
61 /// <summary>
62 /// Gets or sets the property names to allow.
63 /// </summary>
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>
67 public string Allow
69 get { return allow; }
70 set { allow = value; }
73 /// <summary>
74 /// Gets or sets a value indicating whether
75 /// the target should be validate during binding.
76 /// </summary>
77 /// <value><c>true</c> if should be validated; otherwise, <c>false</c>.</value>
78 public bool Validate
80 get { return validate; }
81 set { validate = value; }
84 /// <summary>
85 /// Gets or sets <see cref="ParamStore"/> used to
86 /// indicate where to get the values from
87 /// </summary>
88 /// <value>The <see cref="ParamStore"/> type.
89 /// Typically <see cref="ParamStore.Params"/>.</value>
90 public ParamStore From
92 get { return from; }
93 set { from = value; }
96 /// <summary>
97 /// Gets the databinding prefix.
98 /// </summary>
99 /// <remarks>
100 /// The prefix is a name followed by a
101 /// dot that prefixes the entries names
102 /// on the source http request.
103 /// </remarks>
104 /// <value>The databinding prefix.</value>
105 public string Prefix
107 get { return prefix; }
110 /// <summary>
111 /// Implementation of <see cref="IParameterBinder.CalculateParamPoints"/>
112 /// and it is used to give the method a weight when overloads are available.
113 /// </summary>
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>
118 /// <returns>
119 /// Positive value if the parameter can be bound
120 /// </returns>
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;
130 /// <summary>
131 /// Implementation of <see cref="IParameterBinder.Bind"/>
132 /// and it is used to read the data available and construct the
133 /// parameter type accordingly.
134 /// </summary>
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);
154 return instance;
157 /// <summary>
158 /// Creates the binder.
159 /// </summary>
160 /// <returns></returns>
161 protected virtual IDataBinder CreateBinder()
163 return new DataBinder();
166 /// <summary>
167 /// Configures the validator.
168 /// </summary>
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");
178 if (validate)
180 binder.Validator = validatorExposer.Validator;
182 else
184 binder.Validator = null;
188 /// <summary>
189 /// Populates the validator error summary.
190 /// </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)
196 if (validate)
198 validatorExposer.PopulateValidatorErrorSummary(instance, binder.GetValidationSummary(instance));
202 /// <summary>
203 /// Binds the instance errors.
204 /// </summary>
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;