Datasheet

8
x
CHAPTER 1 PRIMER
if (s.FirstName == first && s.LastName == last)
{
// ... Do something
}
}
This feels sloppy somehow after some deeper thought, several things emerge as “wrong.
First, an obvious bottleneck emerges if this list becomes large; for a school of a half-dozen
Instructors and a few dozen Students, this simple one-at-a-time comparison works well enough,
but if the system grows to incorporate campuses all across the world and millions of
Students, this
is going to break down quickly.
Second, when it comes time to select an
Instructor for a class, similar kinds of search needs
to happen, and simply repeating the foreach loops over and over again is a violation of the DRY
(Don’t Repeat Yourself) principle there’s no reusability. This problem will only magnify itself
if the searches are somehow optimized, because the optimizations will need to happen for each
search.
It may seem tempting to brush these concerns off as irrelevant, because if the
data is stored in a relational database, the performance and scalability concerns
become issues of SQL and database tuning. Stay with the point for now, and
trust in that this is merely the tip of the iceberg.
The obvious object-oriented solution to the problem, then, is to create custom data structures for
storing the
Students and Instructors:
class InstructorDatabase
{
private List<Instructor> data = new List<Instructor>();
public InstructorDatabase() { }
public void Add(Instructor i) { data.Add(i); }
public Instructor Find(string first, string last)
{
foreach (Instructor i in data)
{
if (i.FirstName == first && i.LastName == last)
return i;
}
return null;
}
}
class StudentDatabase
{
private List<Student> data = new List<Student>();
public StudentDatabase() { }
public void Add(Student i) { data.Add(i); }
c01.indd 8c01.indd 8 10/1/2010 3:20:36 PM10/1/2010 3:20:36 PM