@@ -30,7 +30,7 @@ namespace MongoDB.Driver
30
30
/// <summary>
31
31
/// Represents a DBRef (a convenient way to refer to a document).
32
32
/// </summary>
33
- public class MongoDBRef
33
+ public class MongoDBRef : IEquatable < MongoDBRef >
34
34
{
35
35
// private fields
36
36
private string _databaseName ;
@@ -91,6 +91,69 @@ public BsonValue Id
91
91
{
92
92
get { return _id ; }
93
93
}
94
+
95
+ // public operators
96
+ /// <summary>
97
+ /// Compares two MongoDBRefs.
98
+ /// </summary>
99
+ /// <param name="lhs">The first MongoDBRef.</param>
100
+ /// <param name="rhs">The other MongoDBRef.</param>
101
+ /// <returns>True if the two MongoDBRefs are not equal (or one is null and the other is not).</returns>
102
+ public static bool operator != ( MongoDBRef lhs , MongoDBRef rhs )
103
+ {
104
+ return ! ( lhs == rhs ) ;
105
+ }
106
+
107
+ /// <summary>
108
+ /// Compares two MongoDBRefs.
109
+ /// </summary>
110
+ /// <param name="lhs">The first MongoDBRef.</param>
111
+ /// <param name="rhs">The other MongoDBRef.</param>
112
+ /// <returns>True if the two MongoDBRefs are equal (or both null).</returns>
113
+ public static bool operator == ( MongoDBRef lhs , MongoDBRef rhs )
114
+ {
115
+ return object . Equals ( lhs , rhs ) ;
116
+ }
117
+
118
+ // public methods
119
+ /// <summary>
120
+ /// Compares this MongoDBRef to another one.
121
+ /// </summary>
122
+ /// <param name="rhs">The other MongoDBRef.</param>
123
+ /// <returns>True if the two MongoDBRefs are equal.</returns>
124
+ public bool Equals ( MongoDBRef rhs )
125
+ {
126
+ if ( object . ReferenceEquals ( rhs , null ) || GetType ( ) != rhs . GetType ( ) )
127
+ {
128
+ return false ;
129
+ }
130
+ // note: _databaseName can be null
131
+ return object . Equals ( _databaseName , rhs . _databaseName ) && _collectionName . Equals ( rhs . _collectionName ) && _id . Equals ( rhs . _id ) ;
132
+ }
133
+
134
+ /// <summary>
135
+ /// Compares this MongoDBRef to another object.
136
+ /// </summary>
137
+ /// <param name="obj">The other object.</param>
138
+ /// <returns>True if the other objects is a MongoDBRef and is equal to this one.</returns>
139
+ public override bool Equals ( object obj )
140
+ {
141
+ return Equals ( obj as MongoDBRef ) ; // works even if obj is null or of a different type
142
+ }
143
+
144
+ /// <summary>
145
+ /// Gets the hash code.
146
+ /// </summary>
147
+ /// <returns>The hash code.</returns>
148
+ public override int GetHashCode ( )
149
+ {
150
+ // see Effective Java by Joshua Bloch
151
+ int hash = 17 ;
152
+ hash = 37 * hash + ( ( _databaseName == null ) ? 0 : _databaseName . GetHashCode ( ) ) ;
153
+ hash = 37 * hash + _collectionName . GetHashCode ( ) ;
154
+ hash = 37 * hash + _id . GetHashCode ( ) ;
155
+ return hash ;
156
+ }
94
157
}
95
158
96
159
/// <summary>
0 commit comments