1 //---------------------------------------------------------------------
2 // <copyright file="StructuralType.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
8 //---------------------------------------------------------------------
10 using System.Collections.Generic;
11 using System.Diagnostics;
12 using System.Data.Common;
15 namespace System.Data.Metadata.Edm
18 /// Represents the Structural Type
20 public abstract class StructuralType : EdmType
24 /// Internal parameterless constructor for bootstrapping edmtypes
26 internal StructuralType()
28 _members = new MemberCollection(this);
29 _readOnlyMembers = _members.AsReadOnlyMetadataCollection();
33 /// Initializes a new instance of Structural Type with the given members
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)
43 _members = new MemberCollection(this);
44 _readOnlyMembers = _members.AsReadOnlyMetadataCollection();
49 private readonly MemberCollection _members;
50 private readonly ReadOnlyMetadataCollection<EdmMember> _readOnlyMembers;
56 /// Returns the collection of members.
58 [MetadataProperty(BuiltInTypeKind.EdmMember, true)]
59 public ReadOnlyMetadataCollection<EdmMember> Members
63 return _readOnlyMembers;
71 /// Get the declared only members of a particular type
73 internal ReadOnlyMetadataCollection<T> GetDeclaredOnlyMembers<T>() where T : EdmMember
75 return _members.GetDeclaredOnlyMembers<T>();
79 /// Validates the types and sets the readOnly property to true. Once the type is set to readOnly,
80 /// it can never be changed.
82 internal override void SetReadOnly()
87 this.Members.Source.SetReadOnly();
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.
96 /// <param name="member">The member to validate</param>
97 internal abstract void ValidateMemberForAdd(EdmMember member);
100 /// Adds a member to this type
102 /// <param name="member">The member to add</param>
103 internal void AddMember(EdmMember member)
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");
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)
113 // Do this only when you are adding the first member
114 if (_members.Count == 0)
116 this.DataSpace = member.TypeUsage.EdmType.DataSpace;
118 // We need to build types that span across more than one space. For such row types, we set the
120 else if (this.DataSpace != (DataSpace)(-1) && member.TypeUsage.EdmType.DataSpace != this.DataSpace)
122 this.DataSpace = (DataSpace)(-1);
125 this._members.Add(member);