每个继承类类型的继承静态成员是否不同?

时间:2012-03-14 13:39:29

标签: c# xaml inheritance .net-4.0 static

我正在尝试为表创建一个控件模板,其中每列与可能值的下拉列表相关,每行都有自己唯一的一组“选定值”。它看起来像这样:

<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,并且只会被调用一次,之后基类只会使用它每个继承类类型都有自己的缓存。


解决方案

非常感谢您提供两个非常好且非常有效的想法。我希望你们都得到更多的赞成。我采用了后一种解决方案,因为我是一个简洁的傻瓜。然后我再进一步,以便实现类只需要覆盖虚拟属性getter来满足要求:

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 个答案:

答案 0 :(得分:2)

您可以使用泛型:

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

public class ColourDictionary : DataTableDictionary<ColourDictionary>
{
}

public class XyDictionary : DataTableDictionary<XyDictionary>
{
}

这里每个类都有自己的静态成员。

答案 1 :(得分:1)

您可以在基类中使用Dictionary for cache,如下所示:

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,如果你需要线程安全