Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ghuntley/conditions
If you are just starting out on a new project I recommend using https://github.com/adamralph/liteguard instead of conditions. Especially if using in a mobile application, less allocations and better performance. This project is deprecated and needs a new maintainer. Contact me if you want to take over.
https://github.com/ghuntley/conditions
Last synced: 4 months ago
JSON representation
If you are just starting out on a new project I recommend using https://github.com/adamralph/liteguard instead of conditions. Especially if using in a mobile application, less allocations and better performance. This project is deprecated and needs a new maintainer. Contact me if you want to take over.
- Host: GitHub
- URL: https://github.com/ghuntley/conditions
- Owner: ghuntley
- License: mit
- Created: 2014-12-04T00:38:21.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2020-01-18T22:38:44.000Z (about 5 years ago)
- Last Synced: 2024-10-11T23:44:40.281Z (4 months ago)
- Language: C#
- Homepage:
- Size: 5.06 MB
- Stars: 56
- Watchers: 5
- Forks: 10
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-xamarin - Conditions ★48 - Library that helps developers write pre- and postcondition validations in a fluent manner. (Architecture)
README

## Conditions [](https://ci.appveyor.com/project/ghuntley/conditions)Conditions is a cross platform portable class library that helps developers write pre- and postcondition validations in a fluent manner. Writing these validations is easy and it improves the readability and maintainability of code.
## Supported Platforms
* .NET 4.5 (We are now a Portable Class Library)
* Mono
* Xamarin.iOS
* Xamarin.Android
* Xamarin.Mac## Installation
Installation is done via NuGet:
Install-Package Conditions
## Usage
```csharp
public ICollection GetData(Nullable 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 failureCondition.Requires(xml, "xml")
.StartsWith("") // throws ArgumentException on failure
.EndsWith("") // throws ArgumentException on failure
.Evaluate(xml.Contains("abc") || xml.Contains("cba")); // arg exCondition.Requires(col, "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, "result")
.IsOfType(typeof(ICollection)); // throws PostconditionException on failurereturn (ICollection)result;
}
public static int[] Multiply(int[] left, int[] right)
{
Condition.Requires(left, "left").IsNotNull();
// You can add an optional description to each check
Condition.Requires(right, "right")
.IsNotNull()
.HasLength(left.Length, "left and right should have the same length");
// Do multiplication
}
```
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:## C# 6
C# 6 compiler provides easier way for accessing extension methods. With `using static Condition;` you have no longer to prefix `Requried` and `Ensures` methods with name of `Condition` static class.For example:
```csharp
namespace Foo
{
using static Condition;
public class Bar
{
public void Buzz(object arg)
{
Required(arg).IsNotNull();
}
}
}
```You can add C#6 features to your project by installing [Microsoft.Net.Compilers](https://www.nuget.org/packages/Microsoft.Net.Compilers/) nuget.
## With thanks to
* The icon "Tornado" designed by Adam Whitcroft from The Noun Project.
* With thanks to ForgeDoc for providing an open source license which is used to generate the class documentation.
* S. van Deursen who is the original author of "CuttingEdge.Conditions" from which this project was forked from.