Se eu quiser salvar e recuperar um objeto, devo criar outra classe para lidar com isso, ou seria melhor fazer isso na própria classe? Ou talvez misturando os dois?
O que é recomendado de acordo com o paradigma OOD?
Por exemplo
Class Student
{
public string Name {set; get;}
....
public bool Save()
{
SqlConnection con = ...
// Save the class in the db
}
public bool Retrieve()
{
// search the db for the student and fill the attributes
}
public List<Student> RetrieveAllStudents()
{
// this is such a method I have most problem with it
// that an object returns an array of objects of its own class!
}
}
Versus. (Eu sei que o seguinte é recomendado, no entanto, isso me parece um pouco contra a coesão da Student
classe)
Class Student { /* */ }
Class DB {
public bool AddStudent(Student s)
{
}
public Student RetrieveStudent(Criteria)
{
}
public List<Student> RetrieveAllStudents()
{
}
}
Que tal misturá-los?
Class Student
{
public string Name {set; get;}
....
public bool Save()
{
/// do some business logic!
db.AddStudent(this);
}
public bool Retrieve()
{
// build the criteria
db.RetrieveStudent(criteria);
// fill the attributes
}
}