Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

If you are just starting out on a new project I recommend usinghttps://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.

License

NotificationsYou must be signed in to change notification settings

ghuntley/conditions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

89 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Icon

ConditionsBuild status

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

publicICollectionGetData(Nullable<int>id,stringxml,IEnumerable<int>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("<data>")// throws ArgumentException on failure.EndsWith("</data>")// 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 nullobjectresult=BuildResults(xml,col);// Check all postconditions:Condition.Ensures(result,"result").IsOfType(typeof(ICollection));// throws PostconditionException on failurereturn(ICollection)result;}publicstaticint[]Multiply(int[]left,int[]right){Condition.Requires(left,"left").IsNotNull();// You can add an optional description to each checkCondition.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. Withusing static Condition; you have no longer to prefixRequried andEnsures methods with name ofCondition static class.

For example:

namespaceFoo{usingstaticCondition;publicclassBar{publicvoidBuzz(objectarg){Required(arg).IsNotNull();}}}

You can add C#6 features to your project by installingMicrosoft.Net.Compilers nuget.

With thanks to

About

If you are just starting out on a new project I recommend usinghttps://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.

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp