2012-05-08 64 views
0

如何將全局變量傳遞給引用程序集?如何將全局變量傳遞給引用程序集?

我正在修改一個asp.net應用程序。需要記錄所有員工(網站的當前用戶)操作,例如保存新客戶或更新發票數據。 UI層正在調用引用的程序集BLL.dll。

我想將當前的Emplyee傳遞給引用的程序集。通過的員工應該是共享該dll的所有靜態方法。它應該是線程安全的,因爲員工可以根據請求進行更改。

我無法在BLL中公開靜態字段,因爲Employee存儲在會話狀態中。

我需要的東西不是靜態的,全局的,可以通過兩個程序集(UI層和BLL.dll)訪問,並且線程安全。

我想使用存儲在當前線程對象中的一些變量。但我不知道我應該怎麼做?

任何workarrounds?

謝謝

回答

2

基本上你需要一些東西在你的BLL中可以得到參考。您可以通過界面使用策略模式。

// IN BLL.dll 

public interface IEmployeeContextImplementation 
{ 
    Employee Current { get; } 
} 

public static EmployeeContext 
{ 
    private static readonly object ImplementationLock = new object(); 
    private static IEmployeeContextImplementation Implementation; 

    public static void SetImplementation(IEmployeeContextImplementation impl) 
    { 
     lock(ImplementationLock) 
     { 
     Implementation = impl; 
     } 
    } 
    public static Employee Current { get { return Implementation.Current; } 
} 

然後在你的web應用程序,實現IEmployeeContextImplementation與會話狀態和應用程序啓動調用SetImplementation只有一次。

但是,會話狀態僅適用於請求的上下文內。如果你需要它去一個不同的線程,你將不得不明確地將它傳遞給另一個線程。

+0

不錯的解決方案,它應該工作。我認爲我們不再需要鎖,因爲會話是線程安全的。 – Costa

+1

鎖是否有其他事情正在嘗試設置實施。 –

相關問題