Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is one of those problems that I wrestle with all the time. The most elegant approach that I've come up with (so far) is to simply create properties for the objects that handle all this for me. </p> <p>Ultimately it comes down to what situations you're using these classes in. For example in a gradebook application you'd drill-down through classes and into the student records. On the other hand, if you're doing a class registration application you'd have students that you add to classes, and drill down from the students into the classes they have registered to.</p> <p>Since we have no real scope of the application you're discussing, lets just roll generic style, shall we? Here's a class' classic Class class. (Ahh, that was fun.) The same ideas would apply to the Student class, however.</p> <pre><code>public class Class { IList&lt;Student&gt; _students; public IList&lt;Student&gt; Students { get { if(_students == null) { _students = new IList&lt;Student&gt;(); PopulateStudents(); // just for simplicity } } // no need for a setter usually... Use Students.Add(Student s) } } </code></pre> <p>What this allows you to do is on-demand loading of the students for this particular class. Supposing this is for web development, if your application is looking at information about a class, but doesn't need the list of students right now, why pull it from the database? <code>PopulateStudents()</code> can be as complex as you like. At minimum you'll put the database calls in there, but it wouldn't be a bad idea to throw some caching in there as well. </p> <p>If you do it properly, you won't really have to worry about what order you load the objects. They all get loaded when you want/need them, and require no thought on your part in your consumption code. When you call</p> <pre><code>Class c = new Class(id); // You do need a class to get its students... IList&lt;Students&gt; studentList = c.Students; </code></pre> <p>You don't really know if your data was already pulled, or still needs to be pulled, or what - and what do you care? Let the class itself handle the gritty details.</p> <p>Naturally there are considerations to make that you need to work out. For example, what about the 'new' Class (in other words, the class that isn't in the database <em>yet</em>). With some thorough programming, however, the end result is the same.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload