2010-06-22 63 views
2

在我的代碼中,我使用靜態字段來存儲特定的值。替代靜態變量?

public static int webServiceId; 

我已刪除並使用任何其他解決方案。但回傳後應保留該值。我們不能在這裏使用Session或ViewState。因爲我正在使用服務(在服務層)。

例子:

我得到xyz.cs文件的方法如下web服務ID:

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

} 

,然後控制進入不同的類文件的另一種方法(比如abd.cs文件)。假設在調用方法異常時它會調用我的第一個類文件(xyz.cs)的方法LogError(Exception error)。 當控件返回到我們的類文件(xyz.cs)中時,我們需要返回web服務標識。因爲我們正在使用它根據web服務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 
} 
+0

對你施加了什麼約束,意味着你不能使用靜態變量? – 2010-06-22 07:42:17

+2

@David:你絕對不應該在ASP.NET/WCF – 2010-06-22 07:47:59

+0

@Henk中使用靜態變量,而不是在ASP.NET/WCF – 2010-06-22 07:55:02

回答

0

你可以在Singleton類包裹webServiceId

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;}} 
} 

然後,您可以撥打編號:

WebServiceSettings.Current.WebServiceId; 

這個類基本上可以確保它只能構建一次(在C#中,靜態構造保證的廣告被稱爲一次)。因此,構造函數中填充WebServiceId的代碼將只運行一次。

+0

David中使用靜態變量,你能稍微向我解釋一下嗎?其實我在這裏沒有得到你想要解釋的東西。如果你可以在這裏寫一些代碼,它真的幫助我。 – 2010-06-22 08:00:55

+0

大衛,這隻有在你可以在下面顯示一個沒有靜態變量的Singleton的時候纔有用。 – 2010-06-22 08:15:27

+0

在這種情況下使用Singleton有什麼問題?當然,這個ID只需要創建一次。 – 2010-06-22 08:19:25

1

有沒有考慮實施Singleton

這樣你可以有一個「全局」類來存儲參數。

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 
    } 
    } 
} 

上面的代碼實現了一個類,當實例化(通過getInstance()這個靜態方法)返回類的唯一實例。

+0

已經建議。 – 2010-06-22 08:00:08

+0

@David Neale - 其他人可以自由地爲你提供相同的答案。 SO沒有公約禁止它。 – Oded 2010-06-22 08:03:14

+0

我正在寫它,而他張貼:) 我應該刪除它嗎? – 2010-06-22 08:05:20