继承的静态成员';s对于每个继承类类型都不同

本文关键字:继承 类型 于每个 静态成员 | 更新日期: 2023-09-27 18:27:58

我试图为一个表制作一个控制模板,其中每列都与可能值的下拉列表相关,每行都有自己唯一的一组"选定值"。它看起来像这样:

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

继承的静态成员';s对于每个继承类类型都不同

您可以使用泛型:

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

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

您可以在基类中使用Dictionary进行缓存,如下所示:

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