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
|
//---------------------------------------------------------------------
// <copyright file="EntitySetBaseCollection.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.Collections.ObjectModel;
using System.Data.Common;
using System.Reflection;
using System.Text;
using System.Diagnostics;
namespace System.Data.Metadata.Edm
{
/// <summary>
/// Class representing a collection of entity set objects
/// </summary>
internal sealed class EntitySetBaseCollection : MetadataCollection<EntitySetBase>
{
// This collection allows changes to be intercepted before and after they are passed to MetadataCollection. The interception
// is required to update the EntitySet's back-reference to the EntityContainer.
#region Constructors
/// <summary>
/// Default constructor for constructing an empty collection
/// </summary>
/// <param name="entityContainer">The entity container that has this entity set collection</param>
/// <exception cref="System.ArgumentNullException">Thrown if the argument entityContainer is null</exception>
internal EntitySetBaseCollection(EntityContainer entityContainer)
: this(entityContainer, null)
{
}
/// <summary>
/// The constructor for constructing the collection with the given items
/// </summary>
/// <param name="entityContainer">The entity container that has this entity set collection</param>
/// <param name="items">The items to populate the collection</param>
/// <exception cref="System.ArgumentNullException">Thrown if the argument entityContainer is null</exception>
internal EntitySetBaseCollection(EntityContainer entityContainer, IEnumerable<EntitySetBase> items)
: base(items)
{
EntityUtil.GenericCheckArgumentNull(entityContainer, "entityContainer");
_entityContainer = entityContainer;
}
#endregion
#region Fields
private readonly EntityContainer _entityContainer;
#endregion
#region Properties
/// <summary>
/// Gets an item from the collection with the given index
/// </summary>
/// <param name="index">The index to search for</param>
/// <returns>An item from the collection</returns>
/// <exception cref="System.ArgumentOutOfRangeException">Thrown if the index is out of the range for the Collection</exception>
/// <exception cref="System.InvalidOperationException">Always thrown on setter</exception>
public override EntitySetBase this[int index]
{
get
{
return base[index];
}
set
{
throw EntityUtil.OperationOnReadOnlyCollection();
}
}
/// <summary>
/// Gets an item from the collection with the given identity
/// </summary>
/// <param name="identity">The identity of the item to search for</param>
/// <returns>An item from the collection</returns>
/// <exception cref="System.ArgumentNullException">Thrown if identity argument passed in is null</exception>
/// <exception cref="System.ArgumentException">Thrown if the Collection does not have an EntitySet with the given identity</exception>
/// <exception cref="System.InvalidOperationException">Always thrown on setter</exception>
public override EntitySetBase this[string identity]
{
get
{
return base[identity];
}
set
{
throw EntityUtil.OperationOnReadOnlyCollection();
}
}
#endregion
#region Methods
/// <summary>
/// Adds an item to the collection
/// </summary>
/// <param name="item">The item to add to the list</param>
/// <exception cref="System.ArgumentNullException">Thrown if item argument is null</exception>
/// <exception cref="System.InvalidOperationException">Thrown if the item passed in or the collection itself instance is in ReadOnly state</exception>
/// <exception cref="System.ArgumentException">Thrown if the EntitySetBase that is being added already belongs to another EntityContainer</exception>
/// <exception cref="System.ArgumentException">Thrown if the EntitySetCollection already contains an EntitySet with the same identity</exception>
public override void Add(EntitySetBase item)
{
EntityUtil.GenericCheckArgumentNull(item, "item");
// Check to make sure the given entity set is not associated with another type
ThrowIfItHasEntityContainer(item, "item");
base.Add(item);
// Fix up the declaring type
item.ChangeEntityContainerWithoutCollectionFixup(_entityContainer);
}
/// <summary>
/// Checks if the given entity set already has a entity container, if so, throw an exception
/// </summary>
/// <param name="entitySet">The entity set to check for</param>
/// <param name="argumentName">The name of the argument from the caller</param>
private static void ThrowIfItHasEntityContainer(EntitySetBase entitySet, string argumentName)
{
EntityUtil.GenericCheckArgumentNull(entitySet, argumentName);
if (entitySet.EntityContainer != null)
{
throw EntityUtil.EntitySetInAnotherContainer(argumentName);
}
}
#endregion
}
}
|