In my asp.net application, I have the below static class to handle query strings in my app to which some classes have dependency:
public static class QueryStringUtil
{
public static int? GetStoreId()
{
return GetId(QueryStrings.StoreId);
}
public static string GetItemCode()
{
return Get(QueryStrings.ItemCode);
}
private static string Get(string queryStringName)
{
return HttpContext.Current.Request.QueryString[queryStringName];
}
private static int? GetId(string queryStringName)
{
var queryString = Get(queryStringName);
int queryStringParsed;
return int.TryParse(queryString, out queryStringParsed) ? (int?)queryStringParsed : null;
}
}
What would be the best way to make this class testable?
One way that I know would be to refactor this and create a singleton instance class instead with its interface and enabling the dependent classes to accept an interface of my singleton class. Not sure it would be my best option.
Another option would be to make my class a normal class with interface and then creating a singleton ServiceLocator class responsible to hold instances to all classes that should behave like a singleton, such as my QueryStringUtil, then allowing my dependent class to accept interface to my IQueryStringUtil.
Third option I can think of is not using my Custom Service Locator class instead using an IoC container, such as Microsoft Unity, then holding a singleton instance in the IoC container config file and injecting that to my dependent classes.
Please advise your best option and why.
Many thanks,