私のアプリケーションは複数のデータベース(すべてのデータベースは同じスキーマを持つ)に接続でき、ユーザーが選択した現在のDBをセッションおよびカプセル化アクセスに静的プロパティは次のようになります。セッションに格納されているASP.NET、スレッド、接続文字列
public class DataBase
{
public static string CurrentDB
{
get
{
return HttpContext.Current.Session["CurrentDB"].ToString();
}
set
{
HttpContext.Current.Session["CurrentDB"] = value;
}
}
}
その他のコードは静的なCurrentDBにアクセスして、どのDBを使用するかを決定します。
いくつかのアクションはスレッド内でバックグラウンドプロセスを開始し、いくつかの処理を行うにはCurrentDBにアクセスする必要があります。
[ThreadStatic]
private static string _threadSafeCurrentDB;
public static string CurrentDB
{
get
{
if (HttpContext.Current == null)
return _threadSafeCurrentDB;
return HttpContext.Current.Session["CurrentDB"].ToString();
}
set
{
if (HttpContext.Current == null)
_threadSafeCurrentDB = value;
else
HttpContext.Current.Session["CurrentDB"] = value;
}
}
とスタート糸のように:
public class MyThread
{
private string _currentDB;
private thread _thread;
public MyThread (string currentDB)
{
_currentDB = currentDB;
_thread = new Thread(DoWork);
}
public DoWork()
{
DataBase.CurrentDB = _currentDB;
... //Do the work
}
}
これは悪い習慣です、私はこのようなものを使用して考えていますか?
私はスレッドから継承したクラスが好きです。私にお勧めします。私は現在の単一のDBを使用しています。 –
@alvarogonzalezの場合、Threadから継承したクラスを使用したいので、現在のDBが特異なのに対し、db設定をリアルタイムで取得/設定できます。私の答えがどの練習に最適かを判断するのに役立ったら、それを受け入れることを検討するかもしれません。 –