Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nicer way of parameter checking?

We use the .NET 2.0 framework with C# 3.0 (I think it's the last version of C# which can run on the 2.0 version of the framework, correct me if I am wrong).

Is there something built into C# which can make this type of parameter validation more convenient?

public ConnectionSettings(string url, string username, string password,
                          bool checkPermissions)
{
    if (username == null) {
        throw new ArgumentNullException("username");
    }

    if (password == null) {
        throw new ArgumentNullException("password");
    }

    if (String.IsNullOrEmpty(url)) {
        throw new ArgumentException("Must not be null or empty, it was " +
            (url == null ? url : "empty"), "url");
    }

    this.url = url;
    this.username = username;
    this.password = password;
    this.checkPermissions = checkPermissions;
}

That sort of parameter validation becomes a common pattern and results in a lot of "almost boilerplate" code to wade through in our public methods.

If there is nothing built in. Are there any great free libraries which we could use?

like image 357
Deleted Avatar asked Aug 03 '10 12:08

Deleted


People also ask

What are the two different types of values for a parameter?

Integer value is exported as a variable. Boolean is exported as variable with either value true or value false . String value is available as a variable of type System.

What are method parameters explain the different types?

Default Parameters or Optional Arguments (C# 4.0 and above) Dynamic parameter (dynamic keyword). Value parameter or Passing Value Types by Value (normal C# method param are value parameter) Params (params)

How do you validate an argument in C#?

1) Check one each parameter and throw an exception when it is wrong: public void Method(object parameter1, object parameter2) { if (parameter1 == null) { throw new ArgumentNullException("parameter1"); } if (parameter2 == null) { throw new ArgumentNullException("parameter2"); } ... }


2 Answers

I normally create static helper methods...

E.g.

public static void CheckNotNull(object value, string parameterName)
{
   if(value == null) { throw new ArgumentNullException(parameterName); }
}

Means you can condense your code down to something similar to below and just makes it a little tidier.

CheckNotNull(username, "username");
CheckNotNull(password, "password"); 

Or you can wrap it up as an extension method:

public static void CheckNotNull<T>(this T value, string parameterName)
{
   if(value == null) { throw new ArgumentNullException(parameterName); }
}

And use like:

username.CheckNotNull("username");
password.CheckNotNull("password");

And if you're feeling really fancy, you could probably interrogate the parameter names by using reflection. Reflection's kinda slow, so you'd only do this if you were going to throw an exception, but it saves you typing the literal parameter name all the time.

like image 145
Ian Avatar answered Sep 23 '22 15:09

Ian


You could use an il weaver like Post Sharp, keep in mind that compiler as a service in C# 5 will make this kind of stuff built in.

Personally I would not recommend this approach unless the problem is huge and must be tackled. Usually a few asserts and checking preconditions as you described above is a best practice.

EG:

public ConnectionSettings(
   [NotNullOrEmpty] string url, 
   [NotNull] string username, 
   [NotNull] string password,
   bool checkPermissions)
{
    this.url = url;
    this.username = username;
    this.password = password;
    this.checkPermissions = checkPermissions;
}

You could also integrate this kind of stuff with code contracts which would allow you to perform some rich static analysis.

like image 22
Sam Saffron Avatar answered Sep 23 '22 15:09

Sam Saffron