One particular idea is to use strongly typed id field instead of generic ObjectId type which will lead to checking correct id type at a compile-time and not in a run-time. For example, using id of this type you can ensure that you pass id of a product to a GetProductById method. Here is how it can be implemented (requires MongoDb c# driver) :
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Id<T> : IEquatable<IId<T>> | |
{ | |
public ObjectId Value { get;private set; } | |
public override string ToString() | |
{ | |
return Value.ToString(); | |
} | |
public Id(ObjectId value) | |
{ | |
Value = value; | |
} | |
public static Id<T> GetNew() | |
{ | |
return new Id<T>(ObjectId.GenerateNewId()); | |
} | |
#region generated | |
public bool Equals(Id<T> other) | |
{ | |
if (ReferenceEquals(null, other)) return false; | |
if (ReferenceEquals(this, other)) return true; | |
return other.Value.Equals(Value); | |
} | |
public override bool Equals(object obj) | |
{ | |
if (ReferenceEquals(null, obj)) return false; | |
if (ReferenceEquals(this, obj)) return true; | |
if (obj.GetType() != typeof(Id<T>)) return false; | |
return Equals((Id<T>)obj); | |
} | |
public override int GetHashCode() | |
{ | |
return Value.GetHashCode(); | |
} | |
public static bool operator ==(Id<T> left, Id<T> right) | |
{ | |
return Equals(left, right); | |
} | |
public static bool operator !=(Id<T> left, Id<T> right) | |
{ | |
return !Equals(left, right); | |
} | |
public bool Equals(IId<T> other) | |
{ | |
return Equals((object)other); | |
} | |
#endregion | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class IdGenerator<T> : IIdGenerator | |
{ | |
public object GenerateId(object container, object document) | |
{ | |
return Id<T>.GetNew(); | |
} | |
public bool IsEmpty(object id) | |
{ | |
var casted = id as Id<T>; | |
return casted == null || casted.Value == ObjectId.Empty; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Product | |
{ | |
[BsonId(IdGenerator = typeof(IdGenerator<Product>))] | |
public Id<Product> Id { get; private set; } | |
public string Name { get; set; } | |
public Decimal Price { get; set; } | |
} |
No comments:
Post a Comment