Browse Source

Added doc comments to IdGenerators.

pull/50/head
rstam 15 years ago
parent
commit
166664ea2c
  1. 105
      Bson/Serialization/IdGenerators.cs

105
Bson/Serialization/IdGenerators.cs

@ -24,23 +24,36 @@ using System.Text.RegularExpressions;
using MongoDB.Bson.IO; using MongoDB.Bson.IO;
namespace MongoDB.Bson.Serialization { namespace MongoDB.Bson.Serialization {
/// <summary>
/// Represents an Id generator for Guids using the COMB algorithm.
/// </summary>
public class CombGuidGenerator : IIdGenerator { public class CombGuidGenerator : IIdGenerator {
#region private static fields #region private static fields
private static CombGuidGenerator instance = new CombGuidGenerator(); private static CombGuidGenerator instance = new CombGuidGenerator();
#endregion #endregion
#region constructors #region constructors
/// <summary>
/// Initializes a new instance of the CombGuidGenerator class.
/// </summary>
public CombGuidGenerator() { public CombGuidGenerator() {
} }
#endregion #endregion
#region public static properties #region public static properties
/// <summary>
/// Gets an instance of CombGuidGenerator.
/// </summary>
public static CombGuidGenerator Instance { public static CombGuidGenerator Instance {
get { return instance; } get { return instance; }
} }
#endregion #endregion
#region public methods #region public methods
/// <summary>
/// Generates an Id.
/// </summary>
/// <returns>An Id.</returns>
public object GenerateId() { public object GenerateId() {
var guidArray = Guid.NewGuid().ToByteArray(); var guidArray = Guid.NewGuid().ToByteArray();
@ -62,6 +75,11 @@ namespace MongoDB.Bson.Serialization {
return new Guid(guidArray); return new Guid(guidArray);
} }
/// <summary>
/// Tests whether an Id is empty.
/// </summary>
/// <param name="id">The Id.</param>
/// <returns>True if the Id is empty.</returns>
public bool IsEmpty( public bool IsEmpty(
object id object id
) { ) {
@ -70,27 +88,45 @@ namespace MongoDB.Bson.Serialization {
#endregion #endregion
} }
/// <summary>
/// Represents an Id generator for Guids.
/// </summary>
public class GuidGenerator : IIdGenerator { public class GuidGenerator : IIdGenerator {
#region private static fields #region private static fields
private static GuidGenerator instance = new GuidGenerator(); private static GuidGenerator instance = new GuidGenerator();
#endregion #endregion
#region constructors #region constructors
/// <summary>
/// Initializes a new instance of the GuidGenerator class.
/// </summary>
public GuidGenerator() { public GuidGenerator() {
} }
#endregion #endregion
#region public static properties #region public static properties
/// <summary>
/// Gets an instance of GuidGenerator.
/// </summary>
public static GuidGenerator Instance { public static GuidGenerator Instance {
get { return instance; } get { return instance; }
} }
#endregion #endregion
#region public methods #region public methods
/// <summary>
/// Generates an Id.
/// </summary>
/// <returns>An Id.</returns>
public object GenerateId() { public object GenerateId() {
return Guid.NewGuid(); return Guid.NewGuid();
} }
/// <summary>
/// Tests whether an Id is empty.
/// </summary>
/// <param name="id">The Id.</param>
/// <returns>True if the Id is empty.</returns>
public bool IsEmpty( public bool IsEmpty(
object id object id
) { ) {
@ -99,27 +135,45 @@ namespace MongoDB.Bson.Serialization {
#endregion #endregion
} }
/// <summary>
/// Represents an Id generator that only checks that the Id is not null.
/// </summary>
public class NullIdChecker : IIdGenerator { public class NullIdChecker : IIdGenerator {
#region private static fields #region private static fields
private static NullIdChecker instance = new NullIdChecker(); private static NullIdChecker instance = new NullIdChecker();
#endregion #endregion
#region constructors #region constructors
/// <summary>
/// Initializes a new instance of the NullIdChecker class.
/// </summary>
public NullIdChecker() { public NullIdChecker() {
} }
#endregion #endregion
#region public static properties #region public static properties
/// <summary>
/// Gets an instance of NullIdChecker.
/// </summary>
public static NullIdChecker Instance { public static NullIdChecker Instance {
get { return instance; } get { return instance; }
} }
#endregion #endregion
#region public methods #region public methods
/// <summary>
/// Throws an exception because we can't generate an Id.
/// </summary>
/// <returns>Nothing.</returns>
public object GenerateId() { public object GenerateId() {
throw new InvalidOperationException("Id cannot be null"); throw new InvalidOperationException("Id cannot be null");
} }
/// <summary>
/// Tests whether an Id is empty.
/// </summary>
/// <param name="id">The Id.</param>
/// <returns>True if the Id is empty.</returns>
public bool IsEmpty( public bool IsEmpty(
object id object id
) { ) {
@ -128,27 +182,45 @@ namespace MongoDB.Bson.Serialization {
#endregion #endregion
} }
/// <summary>
/// Represents an Id generator for ObjectIds.
/// </summary>
public class ObjectIdGenerator : IIdGenerator { public class ObjectIdGenerator : IIdGenerator {
#region private static fields #region private static fields
private static ObjectIdGenerator instance = new ObjectIdGenerator(); private static ObjectIdGenerator instance = new ObjectIdGenerator();
#endregion #endregion
#region constructors #region constructors
/// <summary>
/// Initializes a new instance of the ObjectIdGenerator class.
/// </summary>
public ObjectIdGenerator() { public ObjectIdGenerator() {
} }
#endregion #endregion
#region public static properties #region public static properties
/// <summary>
/// Gets an instance of ObjectIdGenerator.
/// </summary>
public static ObjectIdGenerator Instance { public static ObjectIdGenerator Instance {
get { return instance; } get { return instance; }
} }
#endregion #endregion
#region public methods #region public methods
/// <summary>
/// Generates an Id.
/// </summary>
/// <returns>An Id.</returns>
public object GenerateId() { public object GenerateId() {
return ObjectId.GenerateNewId(); return ObjectId.GenerateNewId();
} }
/// <summary>
/// Tests whether an Id is empty.
/// </summary>
/// <param name="id">The Id.</param>
/// <returns>True if the Id is empty.</returns>
public bool IsEmpty( public bool IsEmpty(
object id object id
) { ) {
@ -157,27 +229,45 @@ namespace MongoDB.Bson.Serialization {
#endregion #endregion
} }
/// <summary>
/// Represents an Id generator for ObjectIds represented internally as strings.
/// </summary>
public class StringObjectIdGenerator : IIdGenerator { public class StringObjectIdGenerator : IIdGenerator {
#region private static fields #region private static fields
private static StringObjectIdGenerator instance = new StringObjectIdGenerator(); private static StringObjectIdGenerator instance = new StringObjectIdGenerator();
#endregion #endregion
#region constructors #region constructors
/// <summary>
/// Initializes a new instance of the StringObjectIdGenerator class.
/// </summary>
public StringObjectIdGenerator() { public StringObjectIdGenerator() {
} }
#endregion #endregion
#region public static properties #region public static properties
/// <summary>
/// Gets an instance of StringObjectIdGenerator.
/// </summary>
public static StringObjectIdGenerator Instance { public static StringObjectIdGenerator Instance {
get { return instance; } get { return instance; }
} }
#endregion #endregion
#region public methods #region public methods
/// <summary>
/// Generates an Id.
/// </summary>
/// <returns>An Id.</returns>
public object GenerateId() { public object GenerateId() {
return ObjectId.GenerateNewId().ToString(); return ObjectId.GenerateNewId().ToString();
} }
/// <summary>
/// Tests whether an Id is empty.
/// </summary>
/// <param name="id">The Id.</param>
/// <returns>True if the Id is empty.</returns>
public bool IsEmpty( public bool IsEmpty(
object id object id
) { ) {
@ -186,18 +276,33 @@ namespace MongoDB.Bson.Serialization {
#endregion #endregion
} }
/// <summary>
/// Represents an Id generator that only checks that the Id is not all zeros.
/// </summary>
// TODO: is it worth trying to remove the dependency on IEquatable<T>? // TODO: is it worth trying to remove the dependency on IEquatable<T>?
public class ZeroIdChecker<T> : IIdGenerator where T : struct, IEquatable<T> { public class ZeroIdChecker<T> : IIdGenerator where T : struct, IEquatable<T> {
#region constructors #region constructors
/// <summary>
/// Initializes a new instance of the ZeroIdChecker class.
/// </summary>
public ZeroIdChecker() { public ZeroIdChecker() {
} }
#endregion #endregion
#region public methods #region public methods
/// <summary>
/// Throws an exception because we can't generate an Id.
/// </summary>
/// <returns>An Id.</returns>
public object GenerateId() { public object GenerateId() {
throw new InvalidOperationException("Id cannot be default value (all zeros)"); throw new InvalidOperationException("Id cannot be default value (all zeros)");
} }
/// <summary>
/// Tests whether an Id is empty.
/// </summary>
/// <param name="id">The Id.</param>
/// <returns>True if the Id is empty.</returns>
public bool IsEmpty( public bool IsEmpty(
object id object id
) { ) {

Loading…
Cancel
Save