Updates referencesource to .NET 4.7
[mono-project.git] / mcs / class / referencesource / System.Web.Entity / System / Data / WebControls / EntityDataSourceWrapperCollection.cs
blob97ac5510425f23170d800cfc43c7b642c754293f
1 //---------------------------------------------------------------------
2 // <copyright file="EntityDataSourceWrapperCollection.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 // </copyright>
5 //
6 // @owner Microsoft
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
9 using System;
10 using System.Data;
11 using System.Configuration;
12 using System.Linq;
13 using System.Web;
14 using System.Web.Security;
15 using System.Web.UI;
16 using System.Web.UI.HtmlControls;
17 using System.Web.UI.WebControls;
18 using System.Web.UI.WebControls.WebParts;
19 using System.Collections;
20 using System.ComponentModel;
21 using System.Collections.Generic;
22 using System.Data.Metadata.Edm;
23 using System.Data.Mapping;
24 using System.Data.Objects;
25 using System.Reflection;
26 using System.Diagnostics;
27 using System.Data.Common;
28 using System.Data.Objects.DataClasses;
29 using System.Globalization;
30 using System.Data.EntityClient;
31 using System.Collections.ObjectModel;
34 namespace System.Web.UI.WebControls
36 /// <summary>
37 /// Summary description for EntityDataSourceWrapperCollection
38 /// </summary>
39 internal class EntityDataSourceWrapperCollection : IEnumerable, ICollection, ITypedList
41 private readonly ObjectContext _context;
42 private readonly List<EntityDataSourceWrapper> _wrapperList;
44 /// <summary>
45 /// Gets the property descriptors exposed to the user.
46 /// </summary>
47 private readonly PropertyDescriptorCollection _visiblePropertyDescriptors;
49 /// <summary>
50 /// Gets all property descriptors.
51 /// </summary>
52 internal readonly ReadOnlyCollection<EntityDataSourceWrapperPropertyDescriptor> AllPropertyDescriptors;
54 private readonly bool _isReadOnly;
55 private readonly Type _clrEntityType;
57 internal EntityDataSourceWrapperCollection(ObjectContext context, EntitySet entitySet, EntityType restrictedEntityType)
59 EntityDataSourceUtil.CheckArgumentNull(context, "context");
60 EntityDataSourceUtil.CheckArgumentNull(entitySet, "entitySet");
62 _context = context;
63 _wrapperList = new List<EntityDataSourceWrapper>();
65 // get handles on the relevant workspaces
66 MetadataWorkspace csWorkspace = ((EntityConnection)context.Connection).GetMetadataWorkspace();
67 MetadataWorkspace ocWorkspace = context.MetadataWorkspace;
69 // if no restricted type is given, we assume the entity set element type is exposed
70 EntityType entityType = restrictedEntityType ?? entitySet.ElementType;
71 _clrEntityType = EntityDataSourceUtil.GetClrType(ocWorkspace, entityType);
73 // if no restricted type is given and the set is polymorphic, make the collection readonly
74 if (null == restrictedEntityType &&
75 1 < EntityDataSourceUtil.GetTypeAndSubtypesOf(entityType, csWorkspace.GetItemCollection(DataSpace.CSpace), true).Count())
77 _isReadOnly = true;
80 // gather the properties
81 ReadOnlyCollection<EntityDataSourceColumn> columns = EntityDataSourceUtil.GetNamedColumns(csWorkspace, ocWorkspace, entitySet, entityType);
82 List<PropertyDescriptor> visiblePropertyDescriptors = new List<PropertyDescriptor>(columns.Count);
83 List<EntityDataSourceWrapperPropertyDescriptor> propertyDescriptors = new List<EntityDataSourceWrapperPropertyDescriptor>(columns.Count);
84 foreach (EntityDataSourceColumn column in columns)
86 var descriptor = new EntityDataSourceWrapperPropertyDescriptor(this, column);
87 propertyDescriptors.Add(descriptor);
89 // if the descriptor does not have a dependent, it is exposed to the user
90 if (!descriptor.Column.IsHidden)
92 visiblePropertyDescriptors.Add(descriptor);
96 _visiblePropertyDescriptors = new PropertyDescriptorCollection(visiblePropertyDescriptors.ToArray(), true);
97 AllPropertyDescriptors = propertyDescriptors.AsReadOnly();
101 internal EntityDataSourceWrapper AddWrappedEntity(object entity)
103 EntityDataSourceWrapper wrapper = new EntityDataSourceWrapper(this, entity);
104 this._wrapperList.Add(wrapper);
105 return wrapper;
108 #region IEnumerable Implementation
109 IEnumerator IEnumerable.GetEnumerator()
111 return this._wrapperList.GetEnumerator();
113 #endregion IEnumerable Implementation
115 #region ICollection Implementation
116 public int Count
118 get { return this._wrapperList.Count; }
120 public void CopyTo(Array array, int index)
122 ((ICollection)this._wrapperList).CopyTo(array, index);
124 public bool IsSynchronized
126 get { return false; }
128 public object SyncRoot
130 get { return null; }
132 #endregion
134 #region ITypedList Implementation
135 public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
137 if (null == listAccessors)
139 return this._visiblePropertyDescriptors ;
141 else
143 return null;// Implement this feature when we support traversing collections.
148 string ITypedList.GetListName(PropertyDescriptor[] listAccessors)
150 return null;
152 #endregion
154 #region Internal properties
155 /// <summary>
156 /// Gets CLR type or base type for entities exposed in this collection.
157 /// </summary>
158 internal Type ClrEntityType
160 get { return _clrEntityType; }
163 /// <summary>
164 /// Indicates whether this configuration supports modifications.
165 /// </summary>
166 internal bool IsReadOnly
168 get { return _isReadOnly; }
171 /// <summary>
172 /// Gets object context tracking the contents of this collection.
173 /// </summary>
174 internal ObjectContext Context
176 get { return _context; }
178 #endregion