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.
userName.IsEmptyOrNull("userName");
try
{
return DALayer.GetUserMenuDB(userName);
}
catch (CoreException)
{ throw; }
catch (Exception)
{ throw; }
}
More information and advanced example can be found here