在循环中创建类是好的编码方法吗?也就是说,我正在调用数据库以检索所有联系人,在此循环中,我基于联系人ID创建了联系人类(该类再次调用DB以基于ID检索联系人详细信息)。这是智能编码还是浪费资源?
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read()) {
Contact contact = new Contact(Convert.ToInt32(reader["contactid"]));
userString = userString + contact.getSurname();
}
public Contact(int id) {
MySqlConnection connection = new MySqlConnection(Global.conn);
connection.Open();
MySqlCommand command = connection.CreateCommand();
string sql = "SELECT *
FROM contact
WHERE contactid=@contactid";
command.CommandText = sql;
command.Parameters.AddWithValue("@contactid", id);
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read()) {
FirstName = reader["name"].ToString();
Surname = reader["Surname"].ToString();
}
}