Sunday, December 7, 2008

A Rangeable, Validated Type

As of now in .Net, we have support for Nullable types. What I really wish for is a type that would support ranges or regexes. This would enforce method calls boundaries to be well defined well defined from service oriented point of view.

Having such an ability will let us remove validation code from the method body and move it to the parameter/argument level, therefore, fixing responsibility of sending validated variables to the calling method.

If the calling method passes a variable that does not support the validation rule, an invalid argument exception is thrown!

I propose a type called ValidVariable or ValidateableVar

examples:

//ensure that only 2 digit numbers are added

public int Add2DigitNumbers(ValidVariable(10,99) first,ValidVariable(10,99) second)
{
...
}


//ensure only valid emails are supported

public void SetEmail(ValidVariable(regex_for_email))
{
...
}

If only the next version of c# supported it! or maybe I will start a project on CodePlex.

No comments: