1
ジェネリッククラスに問題があり、データベースにあるデータに基づいてランタイムに問題が発生します。以下では、列の値の頻度を追跡する汎用カウンタクラスを使用して、問題の簡単な例を示してみました。データベースデータに基づいて実行時にインスタンス化されるジェネリッククラスの処理
クラスCounter
はジェネリッククラスで、クラスTable
は、2つの列を持つデータベーステーブルをシミュレートし、そしてWrapper
クラスは、問題が含まれています:)
基本的にラッパークラスは、私のデータベースへのDBMSに依存しないインタフェースを提供し、公開しますファンクショナリズムをアプリケーションに提供します。列のデータ型はわかりますが、カウンタオブジェクトへの一般的なアクセス方法を教えてください。
public class Counter<T>
{
private Dictionary<T, int> _counter = new Dictionary<T,int>();
public Counter() {}
public Counter(List<T> values) : this()
{
foreach (var item in values)
this.Add(item);
}
public void Add(T key)
{
if (!this._counter.ContainsKey(key))
this._counter[key] = 0;
this._counter[key] += 1;
}
public int GetCount(T key)
{
return this._counter[key];
}
}
public class Table
{
public List<int> IDs { get; private set; }
public List<String> Names { get; private set; }
public Table()
{
this.IDs = new List<int> { 1, 2, 3, 4, 5 };
this.Names = new List<string> { "a", "b", "a", "c", "d" };
}
}
public class Wrapper
{
private Table _table = new Table();
public ... GetCounter(string columnName)
{
if (columnName == "id")
return new Counter<..>(this._table.IDs);
else if (columnName == "name")
return new Counter<..>(this._table.Names);
}
}
おかげで...予備試験から、私はこれが動作すると思います:) – aweis