2010-06-21 Marek Habersack <mhabersack@novell.com>
[mcs.git] / class / System.Web.Mvc / System.Web.Mvc / ModelBinderDictionary.cs
blob0a1a4f5337d204fb7eb77835202f4710be64e9fb
1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation. All rights reserved.
5 * This software is subject to the Microsoft Public License (Ms-PL).
6 * A copy of the license can be found in the license.htm file included
7 * in this distribution.
9 * You must not remove this notice, or any other, from this software.
11 * ***************************************************************************/
13 namespace System.Web.Mvc {
14 using System;
15 using System.Collections;
16 using System.Collections.Generic;
17 using System.Diagnostics.CodeAnalysis;
18 using System.Globalization;
19 using System.Web.Mvc.Resources;
21 public class ModelBinderDictionary : IDictionary<Type, IModelBinder> {
23 private IModelBinder _defaultBinder;
24 private readonly Dictionary<Type, IModelBinder> _innerDictionary = new Dictionary<Type, IModelBinder>();
26 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
27 public int Count {
28 get {
29 return _innerDictionary.Count;
33 public IModelBinder DefaultBinder {
34 get {
35 if (_defaultBinder == null) {
36 _defaultBinder = new DefaultModelBinder();
38 return _defaultBinder;
40 set {
41 _defaultBinder = value;
45 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
46 public bool IsReadOnly {
47 get {
48 return ((IDictionary<Type, IModelBinder>)_innerDictionary).IsReadOnly;
52 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
53 public ICollection<Type> Keys {
54 get {
55 return _innerDictionary.Keys;
59 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
60 public IModelBinder this[Type key] {
61 get {
62 IModelBinder binder;
63 _innerDictionary.TryGetValue(key, out binder);
64 return binder;
66 set {
67 _innerDictionary[key] = value;
71 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
72 public ICollection<IModelBinder> Values {
73 get {
74 return _innerDictionary.Values;
78 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
79 public void Add(KeyValuePair<Type, IModelBinder> item) {
80 ((IDictionary<Type, IModelBinder>)_innerDictionary).Add(item);
83 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
84 public void Add(Type key, IModelBinder value) {
85 _innerDictionary.Add(key, value);
88 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
89 public void Clear() {
90 _innerDictionary.Clear();
93 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
94 public bool Contains(KeyValuePair<Type, IModelBinder> item) {
95 return ((IDictionary<Type, IModelBinder>)_innerDictionary).Contains(item);
98 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
99 public bool ContainsKey(Type key) {
100 return _innerDictionary.ContainsKey(key);
103 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
104 public void CopyTo(KeyValuePair<Type, IModelBinder>[] array, int arrayIndex) {
105 ((IDictionary<Type, IModelBinder>)_innerDictionary).CopyTo(array, arrayIndex);
108 public IModelBinder GetBinder(Type modelType) {
109 return GetBinder(modelType, true /* fallbackToDefault */);
112 public virtual IModelBinder GetBinder(Type modelType, bool fallbackToDefault) {
113 if (modelType == null) {
114 throw new ArgumentNullException("modelType");
117 return GetBinder(modelType, (fallbackToDefault) ? DefaultBinder : null);
120 private IModelBinder GetBinder(Type modelType, IModelBinder fallbackBinder) {
121 // Try to look up a binder for this type. We use this order of precedence:
122 // 1. Binder registered in the global table
123 // 2. Binder attribute defined on the type
124 // 3. Supplied fallback binder
126 IModelBinder binder;
127 if (_innerDictionary.TryGetValue(modelType, out binder)) {
128 return binder;
131 binder = ModelBinders.GetBinderFromAttributes(modelType,
132 () => String.Format(CultureInfo.CurrentUICulture, MvcResources.ModelBinderDictionary_MultipleAttributes, modelType.FullName));
134 return binder ?? fallbackBinder;
137 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
138 public IEnumerator<KeyValuePair<Type, IModelBinder>> GetEnumerator() {
139 return _innerDictionary.GetEnumerator();
142 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
143 public bool Remove(KeyValuePair<Type, IModelBinder> item) {
144 return ((IDictionary<Type, IModelBinder>)_innerDictionary).Remove(item);
147 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
148 public bool Remove(Type key) {
149 return _innerDictionary.Remove(key);
152 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
153 public bool TryGetValue(Type key, out IModelBinder value) {
154 return _innerDictionary.TryGetValue(key, out value);
157 #region IEnumerable Members
158 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
159 IEnumerator IEnumerable.GetEnumerator() {
160 return ((IEnumerable)_innerDictionary).GetEnumerator();
162 #endregion