1
//---------------------------------------------------------------------
2 // <copyright file="EntityDataSourceWrapperCollection.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
11 using System
.Configuration
;
14 using System
.Web
.Security
;
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
37 /// Summary description for EntityDataSourceWrapperCollection
39 internal class EntityDataSourceWrapperCollection
: IEnumerable
, ICollection
, ITypedList
41 private readonly ObjectContext _context
;
42 private readonly List
<EntityDataSourceWrapper
> _wrapperList
;
45 /// Gets the property descriptors exposed to the user.
47 private readonly PropertyDescriptorCollection _visiblePropertyDescriptors
;
50 /// Gets all property descriptors.
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");
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())
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
);
108 #region IEnumerable Implementation
109 IEnumerator IEnumerable
.GetEnumerator()
111 return this._wrapperList
.GetEnumerator();
113 #endregion IEnumerable Implementation
115 #region ICollection Implementation
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
134 #region ITypedList Implementation
135 public PropertyDescriptorCollection
GetItemProperties(PropertyDescriptor
[] listAccessors
)
137 if (null == listAccessors
)
139 return this._visiblePropertyDescriptors
;
143 return null;// Implement this feature when we support traversing collections.
148 string ITypedList
.GetListName(PropertyDescriptor
[] listAccessors
)
154 #region Internal properties
156 /// Gets CLR type or base type for entities exposed in this collection.
158 internal Type ClrEntityType
160 get { return _clrEntityType; }
164 /// Indicates whether this configuration supports modifications.
166 internal bool IsReadOnly
168 get { return _isReadOnly; }
172 /// Gets object context tracking the contents of this collection.
174 internal ObjectContext Context
176 get { return _context; }