This project has moved and is read-only. For the latest updates, please go here.

Offer a default value

Dec 17, 2010 at 10:07 AM

I start to use this library in a small project, and i love it so much ;) Now i use in every project as default, i fell the code now is more strong and easy to understand.

So thank you for share ! ;)

I m looking the best way to offer a default value when the precondition or postcondition fail. 

What's the best practice code ?

Dec 17, 2010 at 1:30 PM

I'm happy you like it.

I'm not sure I follow you. What do you mean by 'offer a default value'?


Dec 17, 2010 at 2:29 PM
Edited Dec 17, 2010 at 2:30 PM

When a condition is not satisfied, in many cases is possible to avoid to throw a exception but assign a value as default.


Condition.Requires(value, "value")

So instead of return with th exception the value is set on 50, will a valid default.



I know isn't a good practice to write code, but sometime is very useful when the data is not under your direct control ;)

See ya !

Dec 17, 2010 at 3:12 PM
Edited Dec 17, 2010 at 5:56 PM

This is not what Conditions is designed for. Conditions helps you with checking preconditions and will always 'fail fast'. What you want is to filter your input.

But besides that I don't really think it's a good thing to do, the current API will not allow you to do this. While it is possible to write a 'T Default<T>(this ConditionValidator<T>, T)` extension method, it will not work, because a condition (such as `IsGreaterThan`) is always checked right away. This means that when such a condition fails, that method will throw an exception. Your default method (which always must be the last method in the chain) will never be called.