Giter Site home page Giter Site logo

cuttingedge.conditions's Introduction

WARNING: This project is not actively maintained.

Welcome to CuttingEdge.Conditions

CuttingEdge.Conditions is a library that helps developers to write pre- and postcondition validations in their C# 3.0 and VB.NET 9.0 code base. Writing these validations is easy and it improves the readability and maintainability of code.

Overview

CuttingEdge.Conditions is build up upon the new C# 3.0 and VB.NET 9.0 extension method mechanism and it allows you to validate arguments in a simple and fluent way. The following example gives a quick overview on the way you could write your pre- and postconditions.

C# example

public ICollection GetData(Nullable<int> id, string xml, IEnumerable<int> col)
{
    // Check all preconditions:
    Condition.Requires(id, nameof(id))
        .IsNotNull()          // throws ArgumentNullException on failure
        .IsInRange(1, 999)    // ArgumentOutOfRangeException on failure
        .IsNotEqualTo(128);   // throws ArgumentException on failure

    Condition.Requires(xml, nameof(xml))
        .StartsWith("<data>") // throws ArgumentException on failure
        .EndsWith("</data>")  // throws ArgumentException on failure
        .Evaluate(xml.Contains("abc") || xml.Contains("cba")); // arg ex

    Condition.Requires(col, nameof(col))
        .IsNotNull()          // throws ArgumentNullException on failure
        .IsEmpty()            // throws ArgumentException on failure
        .Evaluate(c => c.Contains(id.Value) || c.Contains(0)); // arg ex

    // Do some work

    // Example: Call a method that should not return null
    object result = BuildResults(xml, col);

    // Check all postconditions:
    Condition.Ensures(result, nameof(result))
        .IsOfType(typeof(ICollection)); // throws PostconditionException on failure

    return (ICollection)result;
}
    
public static int[] Multiply(int[] left, int[] right)
{
    Condition.Requires(left, nameof(left)).IsNotNull();
    
    // You can add an optional description to each check
    Condition.Requires(right, nameof(right))
        .IsNotNull()
        .HasLength(left.Length, "left and right should have the same length");
    
    // Do multiplication
}

VB.NET example

Public Function GetData(ByVal id As Integer?, ByVal xml As String, _
    ByVal col As ICollection) As ICollection
    ' Check all preconditions:
    Condition.Requires(id, nameof(id)).IsNotNull().IsInRange(1, 999).IsNotEqualTo(128)
    
    Condition.Requires(xml, nameof(xml)).StartsWith("<data>").EndsWith("</data>")
    
    Condition.Requires(col, nameof(col)).IsNotNull().IsEmpty()
    
    ' Do some work
   
    ' Example: Call a method that should not return null
    Dim result = BuildResults(xml, col)
    
    ' Check all postconditions:
    Condition.Ensures(result, "result").IsOfType(GetType(ICollection))
    
    Return result
End Function
    
Public Shared Function Multiply(left As Integer(), right As Integer()) As Integer()
    left.Requires("left").IsNotNull()
    
    ' You can add an optional description to each check
    right.Requires("right").IsNotNull() _
        .HasLength(left.Length, "left and right should have the same length")
    
    ' Do multiplication
End Function

The previous example showed some important features of the library. The example showed the library's ability to:

  • do precondition checks by writing Condition.Requires;
  • do postcondition checks by writing Condition.Ensures;
  • fluently chain calls to the validation methods, just separated by a dot;
  • work with nullable data types;
  • validate string objects;
  • validate collections;
  • do a type check on the variable.

Note: A particular validation is executed immediately when it's method is called, and therefore all checks are executed in the order in which they are written.

Getting started

CuttingEdge Conditions is available as NuGet package. If you're not using NuGet, please follow the steps below, to start using CuttingEdge.Conditions:

  • Go to the Downloads tab and download the latest runtime library;
  • Unpack the downloaded .zip file;
  • Add the CuttingEdge.Conditions.dll to your C# 3.0 or VB.NET 9.0 project by right-clicking on the project in the Visual Studio solution explorer and selecting 'Add Reference...'. Note that you can safely use CuttingEdge.Conditions in .NET 2.0 projects, as long as you use C# 3.0 or VB.NET 9.0.
  • Optionally (but advisable), you can copy the contents of the unpacked 'Code Snippets' folder to your "{MY DOCUMENTS}\Visual Studio 2008\Code Snippets" folder.
  • You are ready to validate! Just add the CuttingEdge.Conditions namespace at the top of a source file and use the 'requires' and 'ensures' code snippets to start writing pre- and postconditions within your code.

More information

For more information on this project please visit the following blog post or wiki page:

cuttingedge.conditions's People

Contributors

dotnetjunkie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.