对于每个继承类类型,继承的静态成员是不同的?
我想为每个列与可能值的下拉列表相关的表格制作一个控制模板,并且每一行都有它自己独特的一组“选定值”。它看起来像这样:对于每个继承类类型,继承的静态成员是不同的?
<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;
}
}
正如你可以看到 - 当基类的推移创建使用的数据视图字典中,它使用了一种虚拟方法,允许不同的继承类在他们自己的视图上传递 - 但每个类都需要跟踪他们自己的静态缓存。
我希望这个缓存是逻辑我可以保留在基类中,这样“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;
}
您可以使用泛型:
public class DataTableDictionary<T> where T: DataTableDictionary<T>
{
private static DataView cachedView;
}
public class ColourDictionary : DataTableDictionary<ColourDictionary>
{
}
public class XyDictionary : DataTableDictionary<XyDictionary>
{
}
这里每个类都有自己的静态成员。
您可以在基本使用字典高速缓存类是这样的:
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,如果你需要线程安全
两个很棒的解决方案。为了清晰起见,我喜欢这个。 – Alain 2012-03-14 14:15:44
两个很棒的解决方案。我喜欢这个,因为它很简洁。 – Alain 2012-03-14 14:15:30
@Alain - 感谢您的编辑。 – Henrik 2012-03-15 07:27:19