File: ObjectViewFactory.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (360 lines) | stat: -rw-r--r-- 18,518 bytes parent folder | download | duplicates (8)
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//---------------------------------------------------------------------
// <copyright file="ObjectViewFactory.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//
// @owner       Microsoft
// @backupOwner Microsoft
//---------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Common;
using System.Data.Metadata;
using System.Data.Metadata.Edm;
using System.Data.Objects.DataClasses;
using System.Data.Objects.Internal;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace System.Data.Objects
{
    /// <summary>
    /// Creates instances of ObjectView that provide a binding list for ObjectQuery results and EntityCollections.
    /// </summary>
    /// <remarks>
    /// The factory methods construct an ObjectView whose generic type parameter (and typed of elements in the binding list)
    /// is of the same type or a more specific derived type of the generic type of the ObjectQuery or EntityCollection.
    /// The EDM type of the query results or EntityType or the EntityCollection is examined to determine 
    /// the appropriate type to be used.
    /// For example, if you have an ObjectQuery whose generic type is "object", but the EDM result type of the Query maps
    /// to the CLR type "Customer", then the ObjectView returned will specify a generic type of "Customer", and not "object".
    /// </remarks>
    internal static class ObjectViewFactory
    {
        // References to commonly-used generic type definitions.
        private static readonly Type genericObjectViewType = typeof(ObjectView<>);

        private static readonly Type genericObjectViewDataInterfaceType = typeof(IObjectViewData<>);
        private static readonly Type genericObjectViewQueryResultDataType = typeof(ObjectViewQueryResultData<>);
        private static readonly Type genericObjectViewEntityCollectionDataType = typeof(ObjectViewEntityCollectionData<,>);

        /// <summary>
        /// Return a list suitable for data binding using the supplied query results.
        /// </summary>
        /// <typeparam name="TElement">
        /// CLR type of query result elements declared by the caller.
        /// </typeparam>
        /// <param name="elementEdmTypeUsage">
        /// The EDM type of the query results, used as the primary means of determining the 
        /// CLR type of list returned by this method.
        /// </param>
        /// <param name="queryResults">
        /// IEnumerable used to enumerate query results used to populate binding list.
        /// Must not be null.
        /// </param>
        /// <param name="objectContext">
        /// <see cref="ObjectContext"/> associated with the query from which results were obtained.
        /// Must not be null.
        /// </param>
        /// <param name="forceReadOnly">
        /// <b>True</b> to prevent modifications to the binding list built from the query result; otherwise <b>false</b>.
        /// Note that other conditions may prevent the binding list from being modified, so a value of <b>false</b>
        /// supplied for this parameter doesn't necessarily mean that the list will be writable.
        /// </param>
        /// <param name="singleEntitySet">
        /// If the query results are composed of entities that only exist in a single <see cref="EntitySet"/>, 
        /// the value of this parameter is the single EntitySet.
        /// Otherwise the value of this parameter should be null.
        /// </param>
        /// <returns>
        /// <see cref="IBindingList"/> that is suitable for data binding.
        /// </returns>
        [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
        internal static IBindingList CreateViewForQuery<TElement>(TypeUsage elementEdmTypeUsage, IEnumerable<TElement> queryResults, ObjectContext objectContext, bool forceReadOnly, EntitySet singleEntitySet)
        {
            EntityUtil.CheckArgumentNull(queryResults, "queryResults");
            EntityUtil.CheckArgumentNull(objectContext, "objectContext");

            Type clrElementType = null;
            TypeUsage ospaceElementTypeUsage = GetOSpaceTypeUsage(elementEdmTypeUsage, objectContext);

            // Map the O-Space EDM type to a CLR type.
            // If the mapping is unsuccessful, fallback to TElement type.
            if (ospaceElementTypeUsage == null)
            {
                clrElementType = typeof(TElement);
            }
            {
                clrElementType = GetClrType<TElement>(ospaceElementTypeUsage.EdmType);
            }

            IBindingList objectView;
            object eventDataSource = objectContext.ObjectStateManager;

            // If the clrElementType matches the declared TElement type, optimize the construction of the ObjectView
            // by avoiding a reflection-based instantiation.
            if (clrElementType == typeof(TElement))
            {
                ObjectViewQueryResultData<TElement> viewData = new ObjectViewQueryResultData<TElement>((IEnumerable)queryResults, objectContext, forceReadOnly, singleEntitySet);

                objectView = new ObjectView<TElement>(viewData, eventDataSource);
            }
            else if (clrElementType == null)
            {
                ObjectViewQueryResultData<DbDataRecord> viewData = new ObjectViewQueryResultData<DbDataRecord>((IEnumerable)queryResults, objectContext, true, null);
                objectView = new DataRecordObjectView(viewData, eventDataSource, (RowType)ospaceElementTypeUsage.EdmType, typeof(TElement));
            }
            else
            {
                if (!typeof(TElement).IsAssignableFrom(clrElementType))
                {
                    throw EntityUtil.ValueInvalidCast(clrElementType, typeof(TElement));
                }

                // Use reflection to create an instance of the generic ObjectView and ObjectViewQueryResultData classes, 
                // using clrElementType as the value of TElement generic type parameter for both classes.

                Type objectViewDataType = genericObjectViewQueryResultDataType.MakeGenericType(clrElementType);

                ConstructorInfo viewDataConstructor = objectViewDataType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic,
                                                                                        null,
                                                                                        new Type[] { typeof(IEnumerable), typeof(ObjectContext), typeof(bool), typeof(EntitySet) },
                                                                                        null);

                Debug.Assert(viewDataConstructor != null, "ObjectViewQueryResultData constructor not found. Please ensure constructor signature is correct.");

                // Create ObjectViewQueryResultData instance
                object viewData = viewDataConstructor.Invoke(new object[] { queryResults, objectContext, forceReadOnly, singleEntitySet });

                // Create ObjectView instance
                objectView = CreateObjectView(clrElementType, objectViewDataType, viewData, eventDataSource);
            }

            return objectView;
        }

        /// <summary>
        /// Return a list suitable for data binding using the supplied EntityCollection
        /// </summary>
        /// <typeparam name="TElement">
        /// CLR type of the elements of the EntityCollection.
        /// </typeparam>
        /// <param name="entityType">
        /// The EntityType of the elements in the collection.
        /// This should either be the same as the EntityType that corresponds to the CLR TElement type,
        /// or a EntityType derived from the declared EntityCollection element type.
        /// </param>
        /// <param name="entityCollection">
        /// The EntityCollection from which a binding list is created.
        /// </param>
        /// <returns>
        /// <see cref="IBindingList"/> that is suitable for data binding.
        /// </returns>
        [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
        internal static IBindingList CreateViewForEntityCollection<TElement>(EntityType entityType, EntityCollection<TElement> entityCollection)
            where TElement : class
        {
            Type clrElementType = null;
            TypeUsage entityTypeUsage = entityType == null ? null : TypeUsage.Create(entityType);
            TypeUsage ospaceElementTypeUsage = GetOSpaceTypeUsage(entityTypeUsage, entityCollection.ObjectContext);

            // Map the O-Space EDM type to a CLR type.
            // If the mapping is unsuccessful, fallback to TElement type.
            if (ospaceElementTypeUsage == null)
            {
                clrElementType = typeof(TElement);
            }
            else
            {
                clrElementType = GetClrType<TElement>(ospaceElementTypeUsage.EdmType);

                // A null clrElementType is returned by GetClrType if the EDM type is a RowType with no specific CLR type mapping.
                // This should not happen when working with EntityCollections, but if it does, fallback to TEntityRef type.
                Debug.Assert(clrElementType != null, "clrElementType has unexpected value of null.");

                if (clrElementType == null)
                {
                    clrElementType = typeof(TElement);
                }
            }

            IBindingList objectView;

            // If the clrElementType matches the declared TElement type, optimize the construction of the ObjectView
            // by avoiding a reflection-based instantiation.
            if (clrElementType == typeof(TElement))
            {
                ObjectViewEntityCollectionData<TElement, TElement> viewData = new ObjectViewEntityCollectionData<TElement, TElement>(entityCollection);
                objectView = new ObjectView<TElement>(viewData, entityCollection);
            }
            else
            {
                if (!typeof(TElement).IsAssignableFrom(clrElementType))
                {
                    throw EntityUtil.ValueInvalidCast(clrElementType, typeof(TElement));
                }

                // Use reflection to create an instance of the generic ObjectView and ObjectViewEntityCollectionData classes, 
                // using clrElementType as the value of TElement generic type parameter for both classes.

                Type objectViewDataType = genericObjectViewEntityCollectionDataType.MakeGenericType(clrElementType, typeof(TElement));

                ConstructorInfo viewDataConstructor = objectViewDataType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic,
                                                                                        null,
                                                                                        new Type[] { typeof(EntityCollection<TElement>) },
                                                                                        null);

                Debug.Assert(viewDataConstructor != null, "ObjectViewEntityCollectionData constructor not found. Please ensure constructor signature is correct.");

                // Create ObjectViewEntityCollectionData instance
                object viewData = viewDataConstructor.Invoke(new object[] { entityCollection });

                // Create ObjectView instance
                objectView = CreateObjectView(clrElementType, objectViewDataType, viewData, entityCollection);
            }

            return objectView;
        }

        /// <summary>
        /// Create an ObjectView using reflection.
        /// </summary>
        /// <param name="clrElementType">Type to be used for the ObjectView's generic type parameter.</param>
        /// <param name="objectViewDataType">The type of class that implements the IObjectViewData to be used by the ObjectView.</param>
        /// <param name="viewData">The IObjectViewData to be used by the ObjectView to access the binding list.</param>
        /// <param name="eventDataSource">Event source used by ObjectView for entity and membership changes.</param>
        /// <returns></returns>
        [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
        private static IBindingList CreateObjectView(Type clrElementType, Type objectViewDataType, object viewData, object eventDataSource)
        {
            Type objectViewType = genericObjectViewType.MakeGenericType(clrElementType);

            Type[] viewDataInterfaces = objectViewDataType.FindInterfaces((Type type, object unusedFilter) => type.Name == genericObjectViewDataInterfaceType.Name, null);
            Debug.Assert(viewDataInterfaces.Length == 1, "Could not find IObjectViewData<T> interface definition for ObjectViewQueryResultData<T>.");

            ConstructorInfo viewConstructor = objectViewType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic,
                                                                            null,
                                                                            new Type[] { viewDataInterfaces[0], typeof(object) },
                                                                            null);

            Debug.Assert(viewConstructor != null, "ObjectView constructor not found. Please ensure constructor signature is correct.");

            // Create ObjectView instance
            return (IBindingList)viewConstructor.Invoke(new object[] { viewData, eventDataSource });
        }

        /// <summary>
        /// Map the supplied TypeUsage to O-Space.
        /// </summary>
        /// <param name="typeUsage">
        /// The TypeUsage to be mapped to O-Space.  Should either be associated with C-Space or O-Space.
        /// </param>
        /// <param name="objectContext">
        /// ObjectContext used to perform type mapping.
        /// </param>
        /// <returns></returns>
        private static TypeUsage GetOSpaceTypeUsage(TypeUsage typeUsage, ObjectContext objectContext)
        {
            TypeUsage ospaceTypeUsage;

            if (typeUsage == null || typeUsage.EdmType == null)
            {
                ospaceTypeUsage = null;
            }
            else
            {
                if (typeUsage.EdmType.DataSpace == DataSpace.OSpace)
                {
                    ospaceTypeUsage = typeUsage;
                }
                else
                {
                    Debug.Assert(typeUsage.EdmType.DataSpace == DataSpace.CSpace, String.Format(System.Globalization.CultureInfo.InvariantCulture, "Expected EdmType.DataSpace to be C-Space, but instead it is {0}.", typeUsage.EdmType.DataSpace.ToString()));

                    // The ObjectContext is needed to map the EDM TypeUsage from C-Space to O-Space.
                    if (objectContext == null)
                    {
                        ospaceTypeUsage = null;
                    }
                    else
                    {
                        objectContext.EnsureMetadata();
                        ospaceTypeUsage = objectContext.Perspective.MetadataWorkspace.GetOSpaceTypeUsage(typeUsage);
                    }
                }
            }

            return ospaceTypeUsage;
        }

        /// <summary>
        /// Determine CLR Type to be exposed for data binding using the supplied EDM item type.
        /// </summary>
        /// <typeparam name="TElement">
        /// CLR element type declared by the caller.
        /// 
        /// There is no requirement that this method return the same type, or a type compatible with the declared type;
        /// it is merely a suggestion as to which type might be used.
        /// </typeparam>
        /// <param name="ospaceEdmType">
        /// The EDM O-Space type of the items in a particular query result.
        /// </param>
        /// <returns>
        /// <see cref="Type"/> instance that represents the CLR type that corresponds to the supplied EDM item type;
        /// or null if the EDM type does not map to a CLR type.
        /// Null is returned in the case where <paramref name="ospaceEdmType"/> is a <see cref="RowType"/>, 
        /// and no CLR type mapping is specified in the RowType metadata.
        /// </returns>
        private static Type GetClrType<TElement>(EdmType ospaceEdmType)
        {
            Type clrType;

            // EDM RowTypes are generally represented by CLR MaterializedDataRecord types
            // that need special handling to properly expose the properties available for binding (using ICustomTypeDescriptor and ITypedList implementations, for example).
            //
            // However, if the RowType has InitializerMetadata with a non-null CLR Type, 
            // that CLR type should be used to determine the properties available for binding.
            if (ospaceEdmType.BuiltInTypeKind == BuiltInTypeKind.RowType)
            {
                RowType itemRowType = (RowType)ospaceEdmType;

                if (itemRowType.InitializerMetadata != null && itemRowType.InitializerMetadata.ClrType != null)
                {
                    clrType = itemRowType.InitializerMetadata.ClrType;
                }
                else
                {
                    // If the generic parameter TElement is not exactly a data record type or object type,
                    // use it as the CLR type.
                    Type elementType = typeof(TElement);

                    if (typeof(IDataRecord).IsAssignableFrom(elementType) || elementType == typeof(object))
                    {
                        // No CLR type mapping exists for this RowType.
                        clrType = null;
                    }
                    else
                    {
                        clrType = typeof(TElement);
                    }
                }
            }
            else
            {
                clrType = ospaceEdmType.ClrType;

                // If the CLR type cannot be determined from the EDM type,
                // fallback to the element type declared by the caller.
                if (clrType == null)
                {
                    clrType = typeof(TElement);
                }
            }

            return clrType;
        }
    }
}