2

I have a class like this

#region Properties
    private static string inputURL;
    public static string InputURL
    {
        get { return inputURL; }
        set { inputURL = value; }
    }
    private static string outputURL;
    private static string ffBaseURL = "format=xml&";
    public static string FFBaseURL
    {
        get { return ffBaseURL; }
        set { ffBaseURL = value; }
    }
    private static string excludeParam = "fullurl,log";
    public static string ExcludeParam
    {
        get { return excludeParam; }
        set { excludeParam = value; }
    }
    private static string currentCategoryID = "234";
    public static string CurrentCategoryID
    {
        get { return currentCategoryID; }
        set { currentCategoryID = value; }
    }
    private static string navigationParameters = "query=*&log=navigation&filterCategoryId=" + currentCategoryID;
    public static string NavigationParameters
    {
        get { return navigationParameters; }
        set { navigationParameters = value; }
    } 
    #endregion

    #region Methods
    public static string NavigationCall()
    {

        List<string> excludeParams = new List<string>(excludeParam.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
        foreach (string key in HttpContext.Current.Request.QueryString.Keys)
        {
            if (!excludeParams.Contains(key))
            {
                FFBaseURL += key + "=" + HttpContext.Current.Request[key] + "&";
            }
        }
        FFBaseURL += NavigationParameters;


        if (Common.IsInternalIP())
        {
            FFBaseURL += "&log=internal";
        }
        outputURL = ffBaseURL;
        return outputURL;
    } 
    #endregion

As you can see I have a static function called NavigationCall() ,it is mandatory that this function remains static.And when I calls this function from my website the function returns wrong values in each function call because of the static properties i declared.We all know static properties will retain their values after the exection of the programe.

So lets say when i call these function first time I gets a result "tesresult1",second time when i reloads my webpage it gives me a result "testresult1testresult1".I think you got the problem now.

  1. I Have tried to solve this issue by declaring static variable values again ,but it does not looks like a good way to programe things.

  2. I tried to make the properties non static .but it returns error as NavigationCall() is a static function i can't call non static properties inside it.

Now I am searching for a correct way to resolve this issue, I think this problem came to me because of the wrong understanding of OOPS concept.Can any one lend a hand here to solve the case or if the issue is vast point to some resources where i can understand how to find a solution?

4

3 回答 3

4

您可以将所有参数传递给静态方法,而不是使用静态属性。

public static string NavigationCall(
     string inputURL,
     string ffBaseURL,
     string excludeParam,
     string currentCategoryID,
     string navigationParameters
)
{
     // the body of your method
}
于 2013-10-04T06:54:24.493 回答
3

您还可以将所有属性捆绑到自定义对象中并将其传递给方法。此外,对于任何解决方案,您都必须使 NavigationCall 线程安全。静态方法线程安全吗?

public static string NavigationCall(CustomNavigation objCustomNavigation)

//Custom object.
public class CustomNavigation
{
  public string InputURL {get;set;}
  public string FBaseURL{get;set;}
  public string ExcludeParam{get;set;}
  public string CurrentCategoryID {get;set;}
  public string NavigationParameters{get;set;}
}
于 2013-10-04T07:18:31.813 回答
2

我建议引入一个参数对象(如@mit 建议的那样),并利用这个机会将您的一些逻辑封装在那里。这应该会立即简化您的方法。也许您可以将其中一些属性设为私有,因为它们仅在封装在参数对象中的逻辑中需要。

    //Your static method
    public static string NavigationCall(CustomNavigation customNavigation)

    //Disclaimer: I have no idea, whether this is an appropriate name. 
    //It really depends on what you want to do with his class
    class CustomNavigation
    {
        public string InputURL { get; private set; }
        public string FFBaseURL { get; private set; }
        public IEnumerable<string> ExcludeParams { get; private set; }
        public string CurrentCategoryID { get; private set; }
        public string NavigationParameters { get; private set; }

        public CustomNavigation(string inputUrl, string excludeParam, string fBaseUrl, string currentCategoryID, string navigationParameters)
        {
            // various guard clauses here...

            NavigationParameters = navigationParameters;
            CurrentCategoryID = currentCategoryID;
            FFBaseURL = fBaseUrl;
            InputURL = inputUrl;

            // Parse string here -> Makes your method simpler
            ExcludeParams = new List<string>(excludeParam.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
        }

        //Example for encapsulating logic in param object
        public void AddKeys(HttpContext currentContext)
        {
            var keys = currentContext.Request.QueryString.Keys
                        .Cast<string>()
                        .Where(key => !ExcludeParams.Contains(key));

            foreach (var key in keys)
                FFBaseURL += key + "=" + currentContext.Request[key] + "&";
        }
    }
于 2013-10-04T12:23:02.353 回答