1 //---------------------------------------------------------------------
2 // <copyright file="RelationshipWrapper.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
10 using System.Collections.Generic;
11 using System.Diagnostics;
12 using System.Data.Metadata.Edm;
14 namespace System.Data.Objects
16 internal sealed class RelationshipWrapper : IEquatable<RelationshipWrapper>
18 internal readonly AssociationSet AssociationSet;
19 internal readonly EntityKey Key0;
20 internal readonly EntityKey Key1;
22 internal RelationshipWrapper(AssociationSet extent, EntityKey key)
24 Debug.Assert(null != extent, "null RelationshipWrapper");
25 Debug.Assert(null != (object)key, "null key");
27 this.AssociationSet = extent;
32 internal RelationshipWrapper(RelationshipWrapper wrapper, int ordinal, EntityKey key)
34 Debug.Assert(null != wrapper, "null RelationshipWrapper");
35 Debug.Assert((uint)ordinal <= 1u, "ordinal out of range");
36 Debug.Assert(null != (object)key, "null key2");
38 this.AssociationSet = wrapper.AssociationSet;
39 this.Key0 = (0 == ordinal) ? key : wrapper.Key0;
40 this.Key1 = (0 == ordinal) ? wrapper.Key1 : key;
43 internal RelationshipWrapper(AssociationSet extent,
44 KeyValuePair<string, EntityKey> roleAndKey1,
45 KeyValuePair<string, EntityKey> roleAndKey2)
46 : this(extent, roleAndKey1.Key, roleAndKey1.Value, roleAndKey2.Key, roleAndKey2.Value)
50 internal RelationshipWrapper(AssociationSet extent,
51 string role0, EntityKey key0,
52 string role1, EntityKey key1)
54 Debug.Assert(null != extent, "null AssociationSet");
55 Debug.Assert(null != (object)key0, "null key0");
56 Debug.Assert(null != (object)key1, "null key1");
58 AssociationSet = extent;
59 Debug.Assert(extent.ElementType.AssociationEndMembers.Count == 2, "only 2 ends are supported");
61 // this assert is explictly commented out to show that the two are similar but different
62 // we should always use AssociationEndMembers, never CorrespondingAssociationEndMember
63 //Debug.Assert(AssociationSet.AssociationSetEnds.Count == 2, "only 2 set ends supported");
64 //Debug.Assert(extent.ElementType.AssociationEndMembers[0] == AssociationSet.AssociationSetEnds[0].CorrespondingAssociationEndMember, "should be same end member");
65 //Debug.Assert(extent.ElementType.AssociationEndMembers[1] == AssociationSet.AssociationSetEnds[1].CorrespondingAssociationEndMember, "should be same end member");
67 if (extent.ElementType.AssociationEndMembers[0].Name == role0)
69 Debug.Assert(extent.ElementType.AssociationEndMembers[1].Name == role1, "a)roleAndKey1 Name differs");
75 Debug.Assert(extent.ElementType.AssociationEndMembers[0].Name == role1, "b)roleAndKey1 Name differs");
76 Debug.Assert(extent.ElementType.AssociationEndMembers[1].Name == role0, "b)roleAndKey0 Name differs");
82 internal ReadOnlyMetadataCollection<AssociationEndMember> AssociationEndMembers
84 get { return this.AssociationSet.ElementType.AssociationEndMembers; }
87 internal AssociationEndMember GetAssociationEndMember(EntityKey key)
89 Debug.Assert(Key0 == key || Key1 == key, "didn't match a key");
90 return AssociationEndMembers[(Key0 != key) ? 1 : 0];
93 internal EntityKey GetOtherEntityKey(EntityKey key)
95 return ((Key0 == key) ? Key1 : ((Key1 == key) ? Key0 : null));
98 internal EntityKey GetEntityKey(int ordinal)
107 throw EntityUtil.ArgumentOutOfRange("ordinal");
111 public override int GetHashCode()
113 return this.AssociationSet.Name.GetHashCode() ^ (this.Key0.GetHashCode() + this.Key1.GetHashCode());
116 public override bool Equals(object obj)
118 return Equals(obj as RelationshipWrapper);
121 public bool Equals(RelationshipWrapper wrapper)
123 return (Object.ReferenceEquals(this, wrapper) ||
124 ((null != wrapper) &&
125 Object.ReferenceEquals(this.AssociationSet, wrapper.AssociationSet) &&
126 this.Key0.Equals(wrapper.Key0) &&
127 this.Key1.Equals(wrapper.Key1)));