我有一个类(下面的代码),用于保存到数据库并从数据库中读取。一切正常,但是当最终打印从字典表中获取的一些对象信息时,我真的不知道将它们放在哪里。(活动记录)。
班级代码:
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
如果添加附加字符串字段的整个想法不好,请引导我朝着正确的方向前进。感谢帮助。