My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for

Assembly: MongoDB.Driver.dll
Namespace: MongoDB.Driver.Conditions
Type Name: ConditionValidator_1 (generic)

Fields

Methods:

T_MongoDB_Driver_Conditions_ConditionValidator_1  
API documentation for the ConditionValidator`1 type
WikiDoc, Type
Updated Jun 8, 2010 by ee.devf...@gmail.com

Example

The following example shows how to use MongoDB.Driver.Conditions. using System.Collections; using MongoDB.Driver.Conditions; public class ExampleClass { private enum StateType { Uninitialized = 0, Initialized }; private StateType currentState; public ICollection GetData(int? id, string xml, IEnumerable col) { // Check all preconditions: Condition.Requires(id, "id") .IsNotNull() // throws ArgumentNullException on failure .IsInRange(1, 999) // ArgumentOutOfRangeException on failure .IsNotEqualTo(128); // throws ArgumentException on failure Condition.Requires(xml, "xml") .StartsWith("

<data>
") // throws ArgumentException on failure .EndsWith("
</data>
"); // throws ArgumentException on failure Condition.Requires(col, "col") .IsNotNull() // throws ArgumentNullException on failure .IsEmpty(); // throws ArgumentException on failure // Do some work // Example: Call a method that should return a not null ICollection object result = BuildResults(xml, col); // Check all postconditions: // A PostconditionException will be thrown at failure. Condition.Ensures(result, "result") .IsNotNull() .IsOfType(typeof(ICollection)); return result as ICollection; } } The following code examples shows how to extend the library with your own 'Invariant' entry point method. The first example shows a class with an Add method that validates the class state (the class invariants) before adding the Person object to the internal array and that code should throw an . using MongoDB.Driver.Conditions; public class Person { } public class PersonCollection { public PersonCollection(int capacity) { this.Capacity = capicity; } public void Add(Person person) { // Throws a ArgumentNullException when person == null Condition.Requires(person, "person").IsNotNull(); // Throws an InvalidOperationException on failure Invariants.Invariant(this.Count, "Count").IsLessOrEqual(this.Capacity); this.AddInternal(person); } public int Count { get; private set; } public int Capacity { get; private set; } private void AddInternal(Person person) { // some logic here } public bool Contains(Person person) { // some logic here return false; } } The following code example will show the implementation of the Invariants class. using System; using MongoDB.Driver.Conditions; namespace MyCompanyRootNamespace { public static class Invariants { public static ConditionValidator
<t>
Invariant
<t>
(T value) { return new InvariantValidator
<t>
("value", value); } public static ConditionValidator
<t>
Invariant
<t>
(T value, string argumentName) { return new InvariantValidator
<t>
(argumentName, value); } // Internal class that inherits from ConditionValidator
<t>
sealed class InvariantValidator
<t>
: ConditionValidator
<t>
{ public InvariantValidator(string argumentName, T value) : base(argumentName, value) { } protected override void ThrowExceptionCore(string condition, string additionalMessage, ConstraintViolationType type) { string exceptionMessage = string.Format("Invariant '{0}' failed.", condition); if (!String.IsNullOrEmpty(additionalMessage)) { exceptionMessage += " " + additionalMessage; } // Optionally, the 'type' parameter can be used, but never throw an exception // when the value of 'type' is unknown or unvalid. throw new InvalidOperationException(exceptionMessage); } } } }

Members

Fields

Name Literal Comments
Value Gets the value of the argument.

Methods

Equals

||===== Returns ===== true if the specified System.Object is equal to the current System.Object; otherwise, false.
bool Equals(object obj)

GetHashCode

||===== Returns ===== The hash code of the current instance.
int GetHashCode()

GetType

||===== Returns ===== The Type instance that represents the exact runtime type of the current instance.
Type GetType()

ThrowException

ThrowException

ToString

||===== Returns ===== A String that represents the ConditionValidator`1 .
void ThrowException(string condition)
void ThrowException(string condition, string additionalMessage, ConstraintViolationType type)
void ThrowException(string condition)
void ThrowException(string condition, string additionalMessage, ConstraintViolationType type)
string ToString()


Sign in to add a comment
Powered by Google Project Hosting