Skip to content

Instantly share code, notes, and snippets.

@ianfnelson
Last active August 29, 2015 13:58
Show Gist options
  • Save ianfnelson/9991359 to your computer and use it in GitHub Desktop.
Save ianfnelson/9991359 to your computer and use it in GitHub Desktop.
Snippets for 2009 blog post "Code Contracts"
/// <summary>
/// Search for a collection of customers, given a set of criteria.
/// </summary>
/// <param name="queryCriteria" />Query Object defining the search criteria.</param>
/// <returns>Collection of customers matching the specified criteria.</returns>
/// <exception cref="System.ArgumentNullException">Where queryCriteria is null</exception>
/// <exception cref="System.ArgumentException">Where no query criteria have been set (we do not permit searching for all customers)</exception>
public static CustomerCollection Find(CustomerQuery queryCriteria)
{
#region Validate parameters
// Guard clause 1 - null queryCriteria - throw exception
if ( queryCriteria == null ) throw new ArgumentNullException( "queryCriteria" );
// Guard clause 2 - no query criteria specified - throw exception
if ( !queryCriteria.IsUsed( SourceSystem.All ) ) throw new ArgumentException( ExceptionMessages.BusinessLogic_CustomerRepository_Find_NoCriteriaSpecified, "queryCriteria" );
#endregion
// More stuff here to return customers
}
// Guard clause 1 - null queryCriteria
Contract.Requires( queryCriteria!=null );
// Guard clause 2 - no query criteria specified
Contract.Requires( queryCriteria.IsUsed( SourceSystem.All ), ExceptionMessages.BusinessLogic_CustomerRepository_Find_NoCriteriaSpecified);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment