2012-03-14 16 views
2

私は、各列が可能な値のドロップダウンリストに関連するテーブルのコントロールテンプレートを作成しようとしており、各行には「選択された値」という固有のセットがあります。それは次のようになります。継承する各クラスタイプごとに異なる継承された静的メンバーですか?

<DataTemplate x:Key="ColourCellDataTemplate"> 
    <local:ColourDictionary Key="{TemplateBinding Content}"> 
     <local:ColourDictionary.ContentTemplate> 
      <DataTemplate> 
       <TextBlock Style="{StaticResource EditableTextCell}" Text="{Binding ColourName}" /> 
      </DataTemplate> 
     </local:ColourDictionary.ContentTemplate> 
    </local:ColourDictionary> 
</DataTemplate> 

私は約10これらのクラスのを持っている、との初期化それぞれ異なるデータテーブルとソートキーが、すべての基礎となる操作やイベントが同じです。

これらの各クラス、そのクラスのすべてのインスタンス間で共有され、独自の静的キャッシュされたDataViewメンバー、持っている:あなたが見ることができるように

public class ColourDictionary : DataTableDictionary 
{ 
    private static DataView cachedView; 

    public ColourDictionary(){ } 

    protected override DataView createView() 
    { 
     if (CachedView == null) 
     { 
      CachedView = base.buildView(table:((App)App.Current).ApplicationData.Colours, 
             keyField:"ColorCode"); 
     } 
     return CachedView; 
    } 
} 

を - 基本クラスで使用するためのDataViewを作成するために行くとき継承しているさまざまなクラスがそれぞれのビューを渡すことを可能にする仮想メソッドを使用しますが、各クラスは独自の静的キャッシュを追跡する必要があります。

"CreateView()"は新しいDataViewを返すだけで済み、一度呼び出されると基本クラスがそのまま使用するように、このキャッシュが基本クラスに保持できるロジックであることを期待していました。各クラスの継承クラスのための独自のキャッシュです。


ソリューション

は、2つの非常に良い、非常に有効なアイデアをどうもありがとうございます。私はあなたに両方のupvotesを得ることを願っています。私は簡潔さのために吸盤であるので、私は後者の解決策に行きました。

public class CATCodeDictionary : DataTableDictionary<CATCodeDictionary> 
{ 
    protected override DataTable table { get { return ((App)App.Current).ApplicationData.CATCodeList; } } 
    protected override string indexKeyField { get { return "CatCode"; } } 
    public CATCodeDictionary() { } 
} 
public class CCYDictionary : DataTableDictionary<CCYDictionary> 
{ 
    protected override DataTable table { get { return ((App)App.Current).ApplicationData.CCYList; } } 
    protected override string indexKeyField { get { return "CCY"; } } 
    public CCYDictionary() { } 
} 
public class COBDictionary : DataTableDictionary<COBDictionary> 
{ 
    protected override DataTable table { get { return ((App)App.Current).ApplicationData.COBList; } } 
    protected override string indexKeyField { get { return "COB"; } } 
    public COBDictionary() { } 
}  
etc... 

基本クラス

public abstract class DataTableDictionary<T> : where T : DataTableDictionary<T> 
{ 
    private static DataView _IndexedView = null; 

    protected abstract DataTable table { get; } 
    protected abstract string indexKeyField { get; } 

    public DataTableDictionary() 
    { 
     if(_IndexedView == null) 
     { 
      _IndexedView = CreateIndexedView(table.Copy(), indexKeyField); 
     } 
    } 

    private DataView CreateIndexedView(DataTable table, string indexKey) 
    { // Create a data view sorted by ID (keyField) to quickly find a row. 
     DataView dataView = new DataView(table); 
     dataView.Sort = indexKey; 
     return dataView; 
    } 

答えて

2

あなたはジェネリックを使用することができます。

ここ
public class DataTableDictionary<T> where T: DataTableDictionary<T> 
{ 
    private static DataView cachedView; 
} 

public class ColourDictionary : DataTableDictionary<ColourDictionary> 
{ 
} 

public class XyDictionary : DataTableDictionary<XyDictionary> 
{ 
} 

各クラスは、独自の静的メンバを持っています。

+0

2つの素晴らしいソリューション。私はそれが簡潔であるためにこれが好きです。 – Alain

+0

@Alain - 編集に感謝します。 – Henrik

1

あなたがベースのキャッシュのために辞書を使用することができます実装するクラスは、要件を満たすために仮想プロパティのゲッターをオーバーライドするだけで済みますように、私はその後、さらに少し行ってきましたこのようなクラス:

public class DataTableDictionary 
{ 
    private static Dictionary<Type, DataView> cachedViews = new Dictionary<Type, DataView>(); 

    protected abstract DataView CreateView(); 

    public DataView GetView() 
    { 
     DataView result; 
     if (!cachedViews.TryGetValue(this.GetType(), out result)) 
     { 
      result = this.CreateView(); 
      cachedViews[this.GetType()] = result; 
     } 
     return result; 
    } 
} 

public class ColourDictionary : DataTableDictionary 
{ 

    protected override DataView CreateView() 
    { 
     return base.buildView(table: ((App)App.Current).ApplicationData.Colours, keyField: "ColorCode");   
    } 
} 

それとも、スレッドの安全性が必要な場合は、ConcurentDictionaryを使用する必要があります

+0

2つの優れたソリューション。私はその明快さのためにこれが好きです。 – Alain

関連する問題