Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / System.Data.Entity / System / Data / Metadata / Edm / StructuralType.cs
1 //---------------------------------------------------------------------
2 // <copyright file="StructuralType.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //
6 // @owner       [....]
7 // @backupOwner [....]
8 //---------------------------------------------------------------------
9 using System;
10 using System.Collections.Generic;
11 using System.Diagnostics;
12 using System.Data.Common;
13 using System.Text;
14
15 namespace System.Data.Metadata.Edm
16 {
17     /// <summary>
18     /// Represents the Structural Type
19     /// </summary>
20     public abstract class StructuralType : EdmType
21     {
22         #region Constructors
23         /// <summary>
24         /// Internal parameterless constructor for bootstrapping edmtypes
25         /// </summary>
26         internal StructuralType()
27         {
28             _members = new MemberCollection(this);
29             _readOnlyMembers = _members.AsReadOnlyMetadataCollection();
30         }
31
32         /// <summary>
33         /// Initializes a new instance of Structural Type with the given members
34         /// </summary>
35         /// <param name="name">name of the structural type</param>
36         /// <param name="namespaceName">namespace of the structural type</param>
37         /// <param name="version">version of the structural type</param>
38         /// <param name="dataSpace">dataSpace in which this edmtype belongs to</param>
39         /// <exception cref="System.ArgumentNullException">Thrown if either name, namespace or version arguments are null</exception>
40         internal StructuralType(string name, string namespaceName, DataSpace dataSpace)
41             : base(name, namespaceName, dataSpace)
42         {
43             _members = new MemberCollection(this);
44             _readOnlyMembers = _members.AsReadOnlyMetadataCollection();
45         }
46         #endregion
47
48         #region Fields
49         private readonly MemberCollection _members;
50         private readonly ReadOnlyMetadataCollection<EdmMember> _readOnlyMembers;
51         #endregion
52
53         #region Properties
54
55         /// <summary>
56         /// Returns the collection of members. 
57         /// </summary>
58         [MetadataProperty(BuiltInTypeKind.EdmMember, true)]
59         public ReadOnlyMetadataCollection<EdmMember> Members
60         {
61             get
62             {
63                 return _readOnlyMembers;
64             }
65         }
66         #endregion
67
68         #region Methods
69
70         /// <summary>
71         /// Get the declared only members of a particular type
72         /// </summary>
73         internal ReadOnlyMetadataCollection<T> GetDeclaredOnlyMembers<T>() where T : EdmMember
74         {
75             return _members.GetDeclaredOnlyMembers<T>();
76         }
77
78         /// <summary>
79         /// Validates the types and sets the readOnly property to true. Once the type is set to readOnly,
80         /// it can never be changed. 
81         /// </summary>
82         internal override void SetReadOnly()
83         {
84             if (!IsReadOnly)
85             {
86                 base.SetReadOnly();
87                 this.Members.Source.SetReadOnly();
88             }
89         }
90
91         /// <summary>
92         /// Validates a EdmMember object to determine if it can be added to this type's 
93         /// Members collection. If this method returns without throwing, it is assumed
94         /// the member is valid.
95         /// </summary>
96         /// <param name="member">The member to validate</param>
97         internal abstract void ValidateMemberForAdd(EdmMember member);
98
99         /// <summary>
100         /// Adds a member to this type
101         /// </summary>
102         /// <param name="member">The member to add</param>
103         internal void AddMember(EdmMember member)
104         {
105             EntityUtil.GenericCheckArgumentNull(member, "member");
106             Util.ThrowIfReadOnly(this);
107             Debug.Assert(this.DataSpace == member.TypeUsage.EdmType.DataSpace || this.BuiltInTypeKind == BuiltInTypeKind.RowType, "Wrong member type getting added in structural type");
108
109             //Since we set the DataSpace of the RowType to be -1 in the constructor, we need to initialize it
110             //as and when we add members to it
111             if (BuiltInTypeKind.RowType == this.BuiltInTypeKind)
112             {
113                 // Do this only when you are adding the first member
114                 if (_members.Count == 0)
115                 {
116                     this.DataSpace = member.TypeUsage.EdmType.DataSpace;
117                 }
118                 // We need to build types that span across more than one space. For such row types, we set the 
119                 // DataSpace to -1
120                 else if (this.DataSpace != (DataSpace)(-1) && member.TypeUsage.EdmType.DataSpace != this.DataSpace)
121                 {
122                     this.DataSpace = (DataSpace)(-1);
123                 }
124             }
125             this._members.Add(member);
126         }
127         #endregion
128     }
129 }