Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ModelBinderDictionary.cs « System.Web.Mvc « src - github.com/mono/aspnetwebstack.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1c808ece777aba04aed469ea9951f35032e23eb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Web.Mvc.Properties;

namespace System.Web.Mvc
{
    public class ModelBinderDictionary : IDictionary<Type, IModelBinder>
    {
        private readonly Dictionary<Type, IModelBinder> _innerDictionary = new Dictionary<Type, IModelBinder>();
        private IModelBinder _defaultBinder;
        private ModelBinderProviderCollection _modelBinderProviders;

        public ModelBinderDictionary()
            : this(ModelBinderProviders.BinderProviders)
        {
        }

        internal ModelBinderDictionary(ModelBinderProviderCollection modelBinderProviders)
        {
            _modelBinderProviders = modelBinderProviders;
        }

        public int Count
        {
            get { return _innerDictionary.Count; }
        }

        public IModelBinder DefaultBinder
        {
            get
            {
                if (_defaultBinder == null)
                {
                    _defaultBinder = new DefaultModelBinder();
                }
                return _defaultBinder;
            }
            set { _defaultBinder = value; }
        }

        public bool IsReadOnly
        {
            get { return ((IDictionary<Type, IModelBinder>)_innerDictionary).IsReadOnly; }
        }

        public ICollection<Type> Keys
        {
            get { return _innerDictionary.Keys; }
        }

        public ICollection<IModelBinder> Values
        {
            get { return _innerDictionary.Values; }
        }

        public IModelBinder this[Type key]
        {
            get
            {
                IModelBinder binder;
                _innerDictionary.TryGetValue(key, out binder);
                return binder;
            }
            set { _innerDictionary[key] = value; }
        }

        public void Add(KeyValuePair<Type, IModelBinder> item)
        {
            ((IDictionary<Type, IModelBinder>)_innerDictionary).Add(item);
        }

        public void Add(Type key, IModelBinder value)
        {
            _innerDictionary.Add(key, value);
        }

        public void Clear()
        {
            _innerDictionary.Clear();
        }

        public bool Contains(KeyValuePair<Type, IModelBinder> item)
        {
            return ((IDictionary<Type, IModelBinder>)_innerDictionary).Contains(item);
        }

        public bool ContainsKey(Type key)
        {
            return _innerDictionary.ContainsKey(key);
        }

        public void CopyTo(KeyValuePair<Type, IModelBinder>[] array, int arrayIndex)
        {
            ((IDictionary<Type, IModelBinder>)_innerDictionary).CopyTo(array, arrayIndex);
        }

        public IModelBinder GetBinder(Type modelType)
        {
            return GetBinder(modelType, true /* fallbackToDefault */);
        }

        public virtual IModelBinder GetBinder(Type modelType, bool fallbackToDefault)
        {
            if (modelType == null)
            {
                throw new ArgumentNullException("modelType");
            }

            return GetBinder(modelType, (fallbackToDefault) ? DefaultBinder : null);
        }

        private IModelBinder GetBinder(Type modelType, IModelBinder fallbackBinder)
        {
            // Try to look up a binder for this type. We use this order of precedence:
            // 1. Binder returned from provider
            // 2. Binder registered in the global table
            // 3. Binder attribute defined on the type
            // 4. Supplied fallback binder

            IModelBinder binder = _modelBinderProviders.GetBinder(modelType);
            if (binder != null)
            {
                return binder;
            }

            if (_innerDictionary.TryGetValue(modelType, out binder))
            {
                return binder;
            }

            binder = ModelBinders.GetBinderFromAttributes(modelType,
                                                          () => String.Format(CultureInfo.CurrentCulture, MvcResources.ModelBinderDictionary_MultipleAttributes, modelType.FullName));

            return binder ?? fallbackBinder;
        }

        public IEnumerator<KeyValuePair<Type, IModelBinder>> GetEnumerator()
        {
            return _innerDictionary.GetEnumerator();
        }

        public bool Remove(KeyValuePair<Type, IModelBinder> item)
        {
            return ((IDictionary<Type, IModelBinder>)_innerDictionary).Remove(item);
        }

        public bool Remove(Type key)
        {
            return _innerDictionary.Remove(key);
        }

        public bool TryGetValue(Type key, out IModelBinder value)
        {
            return _innerDictionary.TryGetValue(key, out value);
        }

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable)_innerDictionary).GetEnumerator();
        }

        #endregion
    }
}