File: DataSourceHelper.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (199 lines) | stat: -rw-r--r-- 9,730 bytes parent folder | download | duplicates (7)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
namespace System.Web.UI.WebControls {
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Web;
    using System.Linq;
    using System.Web.Compilation;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.Resources;
    using System.Globalization;

    internal static class DataSourceHelper {
        public static object SaveViewState(ParameterCollection parameters) {
            if (parameters != null) {
                return ((IStateManager)parameters).SaveViewState();
            }
            return null;
        }

        public static void TrackViewState(ParameterCollection parameters) {
            if (parameters != null) {
                ((IStateManager)parameters).TrackViewState();
            }
        }        

        public static IDictionary<string, object> ToDictionary(this ParameterCollection parameters, HttpContext context, Control control) {
            return ToDictionary(parameters.GetValues(context, control));
        }

        internal static IDictionary<string, object> ToDictionary(this IOrderedDictionary parameterValues) {
            Dictionary<string, object> values = new Dictionary<string, object>(parameterValues.Count, StringComparer.OrdinalIgnoreCase);

            foreach (DictionaryEntry entry in parameterValues) {
                values[(string)entry.Key] = entry.Value;
            }

            return values;
        }     

        public static IOrderedDictionary ToCaseInsensitiveDictionary(this IDictionary dictionary) {
            if (dictionary != null) {
                IOrderedDictionary destination = new OrderedDictionary(dictionary.Count, StringComparer.OrdinalIgnoreCase);

                foreach (DictionaryEntry de in dictionary) {
                    destination[de.Key] = de.Value;
                }

                return destination;
            }
            return null;
        }

        internal static object CreateObjectInstance(Type type) {
            // FastCreatePublicInstance is faster than Activator.CreateInstance since it caches the type factories.
            return HttpRuntime.FastCreatePublicInstance(type);
        }

        public static bool MergeDictionaries(object dataObjectType, ParameterCollection referenceValues, IDictionary source,
                               IDictionary destination, IDictionary<string, Exception> validationErrors) {
            return MergeDictionaries(dataObjectType, referenceValues, source, destination, null, validationErrors);
        }

        public static bool MergeDictionaries(object dataObjectType, ParameterCollection reference, IDictionary source,
                                              IDictionary destination, IDictionary destinationCopy, IDictionary<string, Exception> validationErrors) {            
            if (source != null) {
                foreach (DictionaryEntry de in source) {
                    object value = de.Value;
                    // search for a parameter that corresponds to this dictionary entry.
                    Parameter referenceParameter = null;
                    string parameterName = (string)de.Key;
                    foreach (Parameter p in reference) {
                        if (String.Equals(p.Name, parameterName, StringComparison.OrdinalIgnoreCase)) {
                            referenceParameter = p;
                            break;
                        }
                    }
                    // use the parameter for type conversion, default value and/or converting empty string to null.
                    if (referenceParameter != null) {
                        try {
                            value = referenceParameter.GetValue(value, true);
                        }
                        catch (Exception e) {
                            // catch conversion exceptions so they can be handled. Note that conversion throws various
                            // types of exceptions like InvalidCastException, FormatException, OverflowException, etc.
                            validationErrors[referenceParameter.Name] = e;
                        }
                    }
                    // save the value to the merged dictionaries.
                    destination[parameterName] = value;
                    if (destinationCopy != null) {
                        destinationCopy[parameterName] = value;
                    }
                }
            }
            return validationErrors.Count == 0;
        }

        public static Type GetType(string typeName) {
            return BuildManager.GetType(typeName, true /* throwOnError */, true /* ignoreCase */);
        }

        private static object ConvertType(object value, Type type, string paramName) {
            // NOTE: This method came from ObjectDataSource with no changes made.
            string s = value as string;
            if (s != null) {
                // Get the type converter for the destination type
                TypeConverter converter = TypeDescriptor.GetConverter(type);
                if (converter != null) {
                    // Perform the conversion
                    try {
                        value = converter.ConvertFromString(s);
                    }
                    catch (NotSupportedException) {
                        throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture,
                            AtlasWeb.LinqDataSourceView_CannotConvertType, paramName, typeof(string).FullName,
                            type.FullName));
                    }
                    catch (FormatException) {
                        throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture,
                            AtlasWeb.LinqDataSourceView_CannotConvertType, paramName, typeof(string).FullName,
                            type.FullName));
                    }
                }
            }
            return value;
        }

        public static object BuildDataObject(Type dataObjectType, IDictionary inputParameters, IDictionary<string, Exception> validationErrors) {
            object dataObject = CreateObjectInstance(dataObjectType);

            PropertyDescriptorCollection props = TypeDescriptor.GetProperties(dataObject);
            foreach (DictionaryEntry de in inputParameters) {
                string propName = (de.Key == null ? String.Empty : de.Key.ToString());
                PropertyDescriptor property = props.Find(propName, /*ignoreCase*/true);
                // NOTE: No longer throws when a property is not found or is read only.  This makes
                // Delete, Insert and Update operations more optimistic, allowing scenarios such as:
                // 1) Deletes and Updates after projecting data in the Selecting event.
                // 2) Deletes and Updates after selecting children of the data object type in the
                //    Selecting event.
                if ((property != null) && (!property.IsReadOnly)) {
                    try {
                        object value = BuildObjectValue(de.Value, property.PropertyType, propName);
                        property.SetValue(dataObject, value);
                    }
                    catch (Exception e) {
                        validationErrors[property.Name] = e;
                    }
                }
            }

            if (validationErrors.Any()) {
                return null;
            }

            return dataObject;
        }

        internal static object BuildObjectValue(object value, Type destinationType, string paramName) {
            // NOTE: This method came from ObjectDataSource with no changes made.
            // Only consider converting the type if the value is non-null and the types don't match
            if ((value != null) && (!destinationType.IsInstanceOfType(value))) {
                Type innerDestinationType = destinationType;
                bool isNullable = false;
                if (destinationType.IsGenericType &&
                    (destinationType.GetGenericTypeDefinition() == typeof(Nullable<>))) {
                    innerDestinationType = destinationType.GetGenericArguments()[0];
                    isNullable = true;
                }
                else {
                    if (destinationType.IsByRef) {
                        innerDestinationType = destinationType.GetElementType();
                    }
                }

                // Try to convert from for example string to DateTime, so that
                // afterwards we can convert DateTime to Nullable<DateTime>

                // If the value is a string, we attempt to use a TypeConverter to convert it
                value = ConvertType(value, innerDestinationType, paramName);

                // Special-case the value when the destination is Nullable<T>
                if (isNullable) {
                    Type paramValueType = value.GetType();
                    if (innerDestinationType != paramValueType) {
                        // Throw if for example, we are trying to convert from int to Nullable<bool>
                        throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture,
                            AtlasWeb.LinqDataSourceView_CannotConvertType, paramName, paramValueType.FullName,
                            String.Format(CultureInfo.InvariantCulture, "Nullable<{0}>",
                            destinationType.GetGenericArguments()[0].FullName)));
                    }
                }
            }
            return value;
        }
    }
}