e0a884a84555a7ce9d1326db5270dbca2bd4d48e
[mono.git] / mcs / class / referencesource / System.Data.Entity / System / Data / Metadata / Edm / EntitySetBaseCollection.cs
1 //---------------------------------------------------------------------
2 // <copyright file="EntitySetBaseCollection.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //
6 // @owner       Microsoft
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
9
10 using System;
11 using System.Collections;
12 using System.Collections.Generic;
13 using System.Collections.ObjectModel;
14 using System.Data.Common;
15 using System.Reflection;
16 using System.Text;
17 using System.Diagnostics;
18
19 namespace System.Data.Metadata.Edm
20 {
21     /// <summary>
22     /// Class representing a collection of entity set objects
23     /// </summary>
24     internal sealed class EntitySetBaseCollection : MetadataCollection<EntitySetBase>
25     {
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.
28
29         #region Constructors
30         /// <summary>
31         /// Default constructor for constructing an empty collection
32         /// </summary>
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)
37         {
38         }
39
40         /// <summary>
41         /// The constructor for constructing the collection with the given items
42         /// </summary>
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)
47             : base(items)
48         {
49             EntityUtil.GenericCheckArgumentNull(entityContainer, "entityContainer");
50             _entityContainer = entityContainer;
51         }
52         #endregion
53
54         #region Fields
55         private readonly EntityContainer _entityContainer;
56         #endregion
57
58         #region Properties
59         /// <summary>
60         /// Gets an item from the collection with the given index
61         /// </summary>
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]
67         {
68             get
69             {
70                 return base[index];
71             }
72             set
73             {
74                 throw EntityUtil.OperationOnReadOnlyCollection();
75             }
76         }
77
78         /// <summary>
79         /// Gets an item from the collection with the given identity
80         /// </summary>
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]
87         {
88             get
89             {
90                 return base[identity];
91             }
92             set
93             {
94                 throw EntityUtil.OperationOnReadOnlyCollection();
95             }
96         }
97         #endregion
98
99         #region Methods
100         /// <summary>
101         /// Adds an item to the collection 
102         /// </summary>
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)
109         {
110             EntityUtil.GenericCheckArgumentNull(item, "item");
111             // Check to make sure the given entity set is not associated with another type
112             ThrowIfItHasEntityContainer(item, "item");
113             base.Add(item);
114
115             // Fix up the declaring type
116             item.ChangeEntityContainerWithoutCollectionFixup(_entityContainer);
117         }
118
119         /// <summary>
120         /// Checks if the given entity set already has a entity container, if so, throw an exception
121         /// </summary>
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)
125         {
126             EntityUtil.GenericCheckArgumentNull(entitySet, argumentName);
127             if (entitySet.EntityContainer != null)
128             {
129                 throw EntityUtil.EntitySetInAnotherContainer(argumentName);
130             }
131         }
132         #endregion
133     }
134 }