C# Object Oriented programming dilemma

Trips

Well-known member
Joined
Aug 7, 2010
Messages
2,788
Im trying to figure out the correct Object Oriented approach for dealing with a scenario I have come across in C# recently.
As an example lets say that you have a student class. A student has various data stored in a database that is accessed via a student_id. The student class provides methods for constructing a student (passing it a student id), loading the data for that student
from the database, and performing some actions on the data. The process for loading the data is broken up into to separate methods. The first method calls a stored procedure that returns a recordset of data for the student. For each record in the recordset
we will then call the second method which takes the data and stores it in the appropriate member variables.
Now lets say that for whatever reason we will need to create 200,000 students. We are going to go ahead and decide that it would be a bad idea to make 200,000 separate calls to our stored procedure. So a class is added that can make dealing with large numbers
of students more efficient. This class would have a method that could take in a large number of student ids and return all the data necessary to build the students in a single stored procedure call (or it might be broken up into to groups depending on the
memory available and the size of data per student). Now that we have the data, we realize that we already have a method (method 2 above) in our student class that does exactly what we want to do in order to load our students. We just need to handle breaking
our large chunk of data up into the individual students to which it belongs on our end.
Now here is my moral dilemma. I have defined the method that loads the internal data from a resultset for the student class as private. And in fact it should be a private method. Its purpose is to load the internal data and it should always be driven by
a datareader that contains the data for the student in its entirety. It would not make sense for someone to make an individual call to this method. My class that I added for efficiently dealing with mass student creation would not have access to this private
method. It would not make sense to have any inheritance (many students is not a student)...and (a student is not many students).
What I could do is create another private method that does the exact same thing for my mass operations class and then pass the prepared data to my students constructor. I would not be very satisfied with having the same block of code twice here though.
I feel certain that I am just missing some important concept here.
Could somebody point me in the right direction...What is the correct way to handle this in C#? Is there some kind of relationship that my student class can have to the class that is performing the mass operations on the students that would give it access
to this internal method?

View the full article
 
Back
Top