Skip to content

Commit

Permalink
Add some more quality members to DBRef.
Browse files Browse the repository at this point in the history
Add Serializable to DBRef.
  • Loading branch information
lanwin committed May 22, 2010
1 parent 8562b68 commit 7180367
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
2 changes: 1 addition & 1 deletion source/MongoDB.Tests/UnitTests/TestDBRef.cs
Expand Up @@ -57,7 +57,7 @@ public void TestEqualsAreSameObject()
public void TestEqualsUsingSameValues()
{
const string colname = "tests";
var id = "32312312";
const string id = "32312312";
var r = new DBRef(colname, id);
var r2 = new DBRef(colname, id);

Expand Down
60 changes: 52 additions & 8 deletions source/MongoDB/DBRef.cs
Expand Up @@ -11,7 +11,8 @@ namespace MongoDB
/// does no special handling of them. Any referential integrity must be maintained by the application
/// not the database.
/// </remarks>
public sealed class DBRef
[Serializable]
public sealed class DBRef : IEquatable<DBRef>
{
internal const string IdName = "$id";
internal const string MetaName = "metadata";
Expand Down Expand Up @@ -124,11 +125,11 @@ public sealed class DBRef
/// The <paramref name="obj"/> parameter is null.
/// </exception>
public override bool Equals(object obj){
if(obj is DBRef){
var comp = (DBRef)obj;
return comp.Id.Equals(Id) && comp.CollectionName.Equals(CollectionName);
}
return base.Equals(obj);
if(ReferenceEquals(null, obj))
return false;
if(ReferenceEquals(this, obj))
return true;
return obj.GetType() == typeof(DBRef) && Equals((DBRef)obj);
}

/// <summary>
Expand All @@ -138,8 +139,13 @@ public sealed class DBRef
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode(){
unchecked{
return ((_collectionName != null ? _collectionName.GetHashCode() : 0)*397) ^ (_id != null ? _id.GetHashCode() : 0);
unchecked
{
var result = (_document != null ? _document.GetHashCode() : 0);
result = (result*397) ^ (_collectionName != null ? _collectionName.GetHashCode() : 0);
result = (result*397) ^ (_id != null ? _id.GetHashCode() : 0);
result = (result*397) ^ (_metadata != null ? _metadata.GetHashCode() : 0);
return result;
}
}

Expand Down Expand Up @@ -179,5 +185,43 @@ public sealed class DBRef
public static explicit operator Document(DBRef dbRef){
return dbRef._document;
}

/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>
/// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
/// </returns>
public bool Equals(DBRef other)
{
if(ReferenceEquals(null, other))
return false;
if(ReferenceEquals(this, other))
return true;
return Equals(other._document, _document) && Equals(other._collectionName, _collectionName) && Equals(other._id, _id) && Equals(other._metadata, _metadata);
}

/// <summary>
/// Implements the operator ==.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(DBRef left, DBRef right)
{
return Equals(left, right);
}

/// <summary>
/// Implements the operator !=.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(DBRef left, DBRef right)
{
return !Equals(left, right);
}
}
}

0 comments on commit 7180367

Please sign in to comment.