Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / MonoRail / Castle.MonoRail.Framework / Attributes / JSONBinderAttribute.cs
blobb292d0b60b5937eaea2f910c3dc5292ef34ca98b
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;
19 using Castle.MonoRail.Framework;
20 using Castle.MonoRail.Framework.Services;
22 /// <summary>
23 /// Enables binding of JSON formatted values on POCO objects.
24 /// </summary>
25 /// <example>
26 /// <para>
27 /// The following demonstrates how to bind a JSON querystring value representing a Car object instance
28 /// to a POCO Car object instance:
29 /// </para>
30 /// The querystring:
31 /// <code>
32 /// car={Wheels=4,Year=2007,Model='Cheap'}
33 /// </code>
34 /// And you want to bind those values to a instance of yours Car class, which looks like this:
35 /// <code>
36 /// public class Car
37 /// {
38 /// private int wheels, year;
39 /// private string model;
40 ///
41 /// public int Wheels
42 /// {
43 /// get { return wheels; }
44 /// set { wheels = value; }
45 /// }
46 ///
47 /// public int Year
48 /// {
49 /// get { return year; }
50 /// set { year = value; }
51 /// }
52 ///
53 /// public string Model
54 /// {
55 /// get { return model; }
56 /// set { model = value; }
57 /// }
58 /// }
59 /// </code>
60 /// <para>Using the <see cref="JSONBinderAttribute"/> and the <see cref="SmartDispatcherController"/>, all you have to
61 /// do is to mark the method parameter with the attribute, like the following example:</para>
62 /// <code>
63 /// public void MyAction([JSONBinder("car")] Car car)
64 /// </code>
65 /// </example>
66 [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
67 public class JSONBinderAttribute : Attribute, IParameterBinder
69 private string entryKey;
71 /// <summary>
72 /// Initializes a new instance of the <see cref="JSONBinderAttribute"/> class.
73 /// </summary>
74 public JSONBinderAttribute()
78 /// <summary>
79 /// Initializes a new instance of the <see cref="JSONBinderAttribute"/> class.
80 /// </summary>
81 /// <param name="entryKey">The entry key, which is the form or
82 /// querystring key that identifies the JSON persisted content</param>
83 public JSONBinderAttribute(string entryKey)
85 if (entryKey == null) throw new ArgumentNullException("entryKey");
87 this.entryKey = entryKey;
90 /// <summary>
91 /// Calculates the param points. Implementors should return value equals or greater than
92 /// zero indicating whether the parameter can be bound successfully. The greater the value (points)
93 /// the more successful the implementation indicates to the framework
94 /// </summary>
95 /// <param name="context">The context.</param>
96 /// <param name="controller">The controller.</param>
97 /// <param name="controllerContext">The controller context.</param>
98 /// <param name="parameterInfo">The parameter info.</param>
99 /// <returns></returns>
100 public int CalculateParamPoints(IEngineContext context, IController controller,
101 IControllerContext controllerContext, ParameterInfo parameterInfo)
103 EnsureValidEntryKey(parameterInfo.Name);
105 return context.Request.Params[entryKey] != null ? 1 : 0;
108 /// <summary>
109 /// Binds the specified parameter for the action.
110 /// </summary>
111 /// <param name="context">The context.</param>
112 /// <param name="controller">The controller.</param>
113 /// <param name="controllerContext">The controller context.</param>
114 /// <param name="parameterInfo">The parameter info.</param>
115 /// <returns>
116 /// A instance based on the JSON values present in the <c>EntryKey</c> or the parameter name.
117 /// </returns>
118 public object Bind(IEngineContext context, IController controller, IControllerContext controllerContext, ParameterInfo parameterInfo)
120 EnsureValidEntryKey(parameterInfo.Name);
122 string entryValue = context.Request.Params[entryKey];
124 IJSONSerializer serializer = context.Services.JSONSerializer;
126 return serializer.Deserialize(entryValue, parameterInfo.ParameterType);
129 private void EnsureValidEntryKey(string name)
131 if (entryKey == null)
133 entryKey = name;