It recently came to my attention that its best practice to avoid database calls in constructors. I feel like this means you end up repeating unnecessary code, thus the code is less DRY?
For example, this approach means the list of available courses is available in the Semester object and the db call exists once.
public class Semester{
public int SelectedCourse {get; set;}
public IEnumerable<Course> AvailableCourses {get; set;}
public Semester()
{
AvailableCourses = dbAcessor.GetAllCourses();
}
}
public class Course{
public string Name {get; set;}
public int Id {get; set;}
}
var semester = new Semester();
var semester2 = new Semester();
Whereas this approach would require the db call to exist every time the Semester object was initialized.
public class Semester{
public int SelectedCourse {get; set;}
public IEnumerable<Course> AvailableCourses {get; set;}
public Semester()
{
AvailableCourses = new List<Course>();
}
}
public class Course{
public string Name {get; set;}
public int Id {get; set;}
}
Public class SchoolYearModel{
public List<Semester> Semesters {get; set;}
}
var semester = new Semester{ AvailableCourses = dbAcessor.GetAllCourses();}
var semester2 = new Semester{ AvailableCourses = dbAcessor.GetAllCourses();}
What am I missing, or is it just a tradeoff?