Posted by: dotnetninja | May 14, 2008

Extension Methods and Validation

In my previous post I was using Extension Method to “reuse” code when validating string arguments and throwing custom exception if string was null or empty.
Here is that example:

namespace UnityTest.Core
    public static class ArgumentValidation
        public static void IsEmptyOrNull(this string value, string parameterName)
            if (String.IsNullOrEmpty(value))
                throw new CoreException(String.Format("{0} is null or empty",parameterName));

To use it in you class you have to set
using UnityTest.Core;

public List<UnityTest.Models.Menu> GetUserMenuBL(string userName)
            //will check for null or string.empty and
            // throw custom exception if that is true.
                return DALayer.GetUserMenuDB(userName);
            catch (CoreException)
            { throw; }
            catch (Exception)
            { throw; }

More information and advanced example can be found here


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: