New tests.
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / ModelBinderDictionary.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\r
5  * This software is subject to the Microsoft Public License (Ms-PL). \r
6  * A copy of the license can be found in the license.htm file included \r
7  * in this distribution.\r
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15     using System.Collections;\r
16     using System.Collections.Generic;\r
17     using System.Globalization;\r
18     using System.Web.Mvc.Resources;\r
19 \r
20     public class ModelBinderDictionary : IDictionary<Type, IModelBinder> {\r
21 \r
22         private IModelBinder _defaultBinder;\r
23         private readonly Dictionary<Type, IModelBinder> _innerDictionary = new Dictionary<Type, IModelBinder>();\r
24 \r
25         public int Count {\r
26             get {\r
27                 return _innerDictionary.Count;\r
28             }\r
29         }\r
30 \r
31         public IModelBinder DefaultBinder {\r
32             get {\r
33                 if (_defaultBinder == null) {\r
34                     _defaultBinder = new DefaultModelBinder();\r
35                 }\r
36                 return _defaultBinder;\r
37             }\r
38             set {\r
39                 _defaultBinder = value;\r
40             }\r
41         }\r
42 \r
43         public bool IsReadOnly {\r
44             get {\r
45                 return ((IDictionary<Type, IModelBinder>)_innerDictionary).IsReadOnly;\r
46             }\r
47         }\r
48 \r
49         public ICollection<Type> Keys {\r
50             get {\r
51                 return _innerDictionary.Keys;\r
52             }\r
53         }\r
54 \r
55         public IModelBinder this[Type key] {\r
56             get {\r
57                 IModelBinder binder;\r
58                 _innerDictionary.TryGetValue(key, out binder);\r
59                 return binder;\r
60             }\r
61             set {\r
62                 _innerDictionary[key] = value;\r
63             }\r
64         }\r
65 \r
66         public ICollection<IModelBinder> Values {\r
67             get {\r
68                 return _innerDictionary.Values;\r
69             }\r
70         }\r
71 \r
72         public void Add(KeyValuePair<Type, IModelBinder> item) {\r
73             ((IDictionary<Type, IModelBinder>)_innerDictionary).Add(item);\r
74         }\r
75 \r
76         public void Add(Type key, IModelBinder value) {\r
77             _innerDictionary.Add(key, value);\r
78         }\r
79 \r
80         public void Clear() {\r
81             _innerDictionary.Clear();\r
82         }\r
83 \r
84         public bool Contains(KeyValuePair<Type, IModelBinder> item) {\r
85             return ((IDictionary<Type, IModelBinder>)_innerDictionary).Contains(item);\r
86         }\r
87 \r
88         public bool ContainsKey(Type key) {\r
89             return _innerDictionary.ContainsKey(key);\r
90         }\r
91 \r
92         public void CopyTo(KeyValuePair<Type, IModelBinder>[] array, int arrayIndex) {\r
93             ((IDictionary<Type, IModelBinder>)_innerDictionary).CopyTo(array, arrayIndex);\r
94         }\r
95 \r
96         public IModelBinder GetBinder(Type modelType) {\r
97             return GetBinder(modelType, true /* fallbackToDefault */);\r
98         }\r
99 \r
100         public virtual IModelBinder GetBinder(Type modelType, bool fallbackToDefault) {\r
101             if (modelType == null) {\r
102                 throw new ArgumentNullException("modelType");\r
103             }\r
104 \r
105             return GetBinder(modelType, (fallbackToDefault) ? DefaultBinder : null);\r
106         }\r
107 \r
108         private IModelBinder GetBinder(Type modelType, IModelBinder fallbackBinder) {\r
109             // Try to look up a binder for this type. We use this order of precedence:\r
110             // 1. Binder registered in the global table\r
111             // 2. Binder attribute defined on the type\r
112             // 3. Supplied fallback binder\r
113 \r
114             IModelBinder binder;\r
115             if (_innerDictionary.TryGetValue(modelType, out binder)) {\r
116                 return binder;\r
117             }\r
118 \r
119             binder = ModelBinders.GetBinderFromAttributes(modelType,\r
120                 () => String.Format(CultureInfo.CurrentUICulture, MvcResources.ModelBinderDictionary_MultipleAttributes, modelType.FullName));\r
121 \r
122             return binder ?? fallbackBinder;\r
123         }\r
124 \r
125         public IEnumerator<KeyValuePair<Type, IModelBinder>> GetEnumerator() {\r
126             return _innerDictionary.GetEnumerator();\r
127         }\r
128 \r
129         public bool Remove(KeyValuePair<Type, IModelBinder> item) {\r
130             return ((IDictionary<Type, IModelBinder>)_innerDictionary).Remove(item);\r
131         }\r
132 \r
133         public bool Remove(Type key) {\r
134             return _innerDictionary.Remove(key);\r
135         }\r
136 \r
137         public bool TryGetValue(Type key, out IModelBinder value) {\r
138             return _innerDictionary.TryGetValue(key, out value);\r
139         }\r
140 \r
141         #region IEnumerable Members\r
142         IEnumerator IEnumerable.GetEnumerator() {\r
143             return ((IEnumerable)_innerDictionary).GetEnumerator();\r
144         }\r
145         #endregion\r
146 \r
147     }\r
148 }\r