1 //---------------------------------------------------------------------
2 // <copyright file="EntitySetBaseCollection.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
11 using System.Collections;
12 using System.Collections.Generic;
13 using System.Collections.ObjectModel;
14 using System.Data.Common;
15 using System.Reflection;
17 using System.Diagnostics;
19 namespace System.Data.Metadata.Edm
22 /// Class representing a collection of entity set objects
24 internal sealed class EntitySetBaseCollection : MetadataCollection<EntitySetBase>
26 // This collection allows changes to be intercepted before and after they are passed to MetadataCollection. The interception
27 // is required to update the EntitySet's back-reference to the EntityContainer.
31 /// Default constructor for constructing an empty collection
33 /// <param name="entityContainer">The entity container that has this entity set collection</param>
34 /// <exception cref="System.ArgumentNullException">Thrown if the argument entityContainer is null</exception>
35 internal EntitySetBaseCollection(EntityContainer entityContainer)
36 : this(entityContainer, null)
41 /// The constructor for constructing the collection with the given items
43 /// <param name="entityContainer">The entity container that has this entity set collection</param>
44 /// <param name="items">The items to populate the collection</param>
45 /// <exception cref="System.ArgumentNullException">Thrown if the argument entityContainer is null</exception>
46 internal EntitySetBaseCollection(EntityContainer entityContainer, IEnumerable<EntitySetBase> items)
49 EntityUtil.GenericCheckArgumentNull(entityContainer, "entityContainer");
50 _entityContainer = entityContainer;
55 private readonly EntityContainer _entityContainer;
60 /// Gets an item from the collection with the given index
62 /// <param name="index">The index to search for</param>
63 /// <returns>An item from the collection</returns>
64 /// <exception cref="System.ArgumentOutOfRangeException">Thrown if the index is out of the range for the Collection</exception>
65 /// <exception cref="System.InvalidOperationException">Always thrown on setter</exception>
66 public override EntitySetBase this[int index]
74 throw EntityUtil.OperationOnReadOnlyCollection();
79 /// Gets an item from the collection with the given identity
81 /// <param name="identity">The identity of the item to search for</param>
82 /// <returns>An item from the collection</returns>
83 /// <exception cref="System.ArgumentNullException">Thrown if identity argument passed in is null</exception>
84 /// <exception cref="System.ArgumentException">Thrown if the Collection does not have an EntitySet with the given identity</exception>
85 /// <exception cref="System.InvalidOperationException">Always thrown on setter</exception>
86 public override EntitySetBase this[string identity]
90 return base[identity];
94 throw EntityUtil.OperationOnReadOnlyCollection();
101 /// Adds an item to the collection
103 /// <param name="item">The item to add to the list</param>
104 /// <exception cref="System.ArgumentNullException">Thrown if item argument is null</exception>
105 /// <exception cref="System.InvalidOperationException">Thrown if the item passed in or the collection itself instance is in ReadOnly state</exception>
106 /// <exception cref="System.ArgumentException">Thrown if the EntitySetBase that is being added already belongs to another EntityContainer</exception>
107 /// <exception cref="System.ArgumentException">Thrown if the EntitySetCollection already contains an EntitySet with the same identity</exception>
108 public override void Add(EntitySetBase item)
110 EntityUtil.GenericCheckArgumentNull(item, "item");
111 // Check to make sure the given entity set is not associated with another type
112 ThrowIfItHasEntityContainer(item, "item");
115 // Fix up the declaring type
116 item.ChangeEntityContainerWithoutCollectionFixup(_entityContainer);
120 /// Checks if the given entity set already has a entity container, if so, throw an exception
122 /// <param name="entitySet">The entity set to check for</param>
123 /// <param name="argumentName">The name of the argument from the caller</param>
124 private static void ThrowIfItHasEntityContainer(EntitySetBase entitySet, string argumentName)
126 EntityUtil.GenericCheckArgumentNull(entitySet, argumentName);
127 if (entitySet.EntityContainer != null)
129 throw EntityUtil.EntitySetInAnotherContainer(argumentName);