私はデータベースに保存してから読み取るために使用するクラス(以下のコード)を持っています。すべてが正常に機能しますが、辞書テーブルから取得したオブジェクト情報を最終的に出力する場合、それらをどこに配置すればよいかわかりません。(アクティブ レコード)。
クラスのコード:
class Object
{
public int id;
public int size;
public int color;
public int author;
public Object(int id, int size, int color, int author)
{
this.id = id;
this.size = size;
this.color = color;
this.author = author;
}
// add, update, delete methods
}
したがって、上記のクラスの SQL:
オブジェクトから ID、サイズ、色、作成者を選択します。
このクラスに文字列フィールドを追加して、次のようにする必要があります。
class Object
{
public int id;
public int size;
public int color;
public int author;
// String fields for dictionary
public string sizeString;
public string colorString;
public string authorString;
//
// Nr 1
public Object(int id, int size, int color, int author)
{
this.id = id;
this.size = size;
this.color = color;
this.author = author;
}
// Nr 2
public Object(int id, string size, string color, string author)
{
this.id = id;
this.size = sizeString;
this.color = colorString;
this.author = authorString;
}
// add, update, delete methods
}
SQL:
select o.id, s.size, c.color, a.name
from object o
join sizes s on o.size = s.id
join colors c on o.color = c.id
join authors a on o.author = a.id
このアプローチが正しい場合、新しいコンストラクター (Nr 2) は上記のようになります。つまり、int フィールドを空のままにするか、常に db からすべてのデータを取得する必要があります。
public Object(int id, int size, int color, int author,
string sizeString, string colorString,
string authorString)
{
this.id = id;
this.size = size;
this.color = color;
this.author = author;
this.sizeString = sizeString;
this.colorString = colorString;
this.authorString = authorString;
}
SQL:
select o.id, o.size, o.color, o.author,
s.size as sizeS, c.color as colorS, a.name as authorS
from object o
join sizes s on o.size = s.id
join colors c on o.color = c.id
join authors a on o.author = a.id
追加の文字列フィールドを追加するという全体的な考えが悪い場合は、私を正しい方向に導いてください。手伝ってくれてありがとう。