开发者

Alternatives to static variables?

In my code I'm using static field for storing a particular value.

public static int webServiceId;

I have remove that and use any other solution. But the value should retain after postback. We can't use Session or ViewState here. Coz I'm working with services (in Service layer).

Example:

I get webservice id in the below method of xyz.cs file:

public int SetWebServiceInformation(int? webServiceID, string webServiceName) 
{ 
    context.WebService_InsertUpdate(ref webServiceID, webServiceName);
         webServiceId = webServiceID.Value;
         return webServiceID.Value;

}

and then controls goes to another method of different class file (say abd.cs file).And suppose in calling method exception comes it will call method LogError(Exception error) of my first class file (xyz.cs). And when the control comes back in our class file (xyz.cs) we need the webservice Id back. Coz we are using that to store exception info into the database according to the webservice Id.

protected void LogError(Exception error)
{
    ----//some logic to get errorLogID//---

    if (errorLogID > 0)
          WebServiceErrorLogging(errorLogID, webServiceId);
    //here we have webServiceId is a 开发者_运维知识库static variable
}


Session variable in WCF application

Maybe this will help, look at RandomNoob's answer.


Have wou considered implementing a Singleton?

That way you can have a "global" class for storing the parameters.

using System.Runtime.CompilerServices;

public class Singleton
{
    private static Singleton Instance = null;
    static readonly object padlock = new object();


    // The private constructor doesnt allos a default public constructor
    private Singleton() {}

    // Synchronized "constructor" to make it thread-safe
    [MethodImpl(MethodImplOptions.Synchronized)]
    private static void CreateInstance()
    {
        lock(padlock)
        {
             if (Instance == null)
             { 
                  Instance = new Singleton();
             }
        }
    }

    public static Singleton GetInstance()
    {
        if (Instance == null) CreateInstance();
        return Instance;
    }

    public int webServiceId {get; set;}


}

// Test class
public class Prueba
{
   private static void Main(string[] args)
   {
     //Singleton s0 = new Singleton();  //Error
     Singleton s1 = Singleton.GetInstance();
     Singleton s2 = Singleton.GetInstance();
     if(s1==s2)
     {
       // Misma instancia
     }
   }
}

The code above implements a class that when instanced (via the GetInstance() static method) returns an unique instance of the class.


You could wrap the webServiceId in a Singleton class.

public sealed class WebServiceSettings
{
    private static readonly WebServiceSettings instance=new WebServiceSettings();
    private static int webServiceId;

    static WebServiceSettings()
    {
         webServiceId = //set webServiceId
    }

    private WebServiceSettings(){}

    public static WebServiceSettings Current
    {
        get
        {
            return instance;
        }
    }

    public int WebServiceId {get{return webServiceId;}}
}

You can then call the Id:

WebServiceSettings.Current.WebServiceId;

This class essentially ensures that it is only constructed once (in C#, static constructors are guarenteed to be called once). Therefore, the code in the constructor to populate the WebServiceId will only run once.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜