Object Composition

Object Composition

Old forum URL: forums.lhotka.net/forums/t/2427.aspx


JonWright posted on Sunday, February 25, 2007

    Hi, was wondering if anybody could shed some light on the following.

Say for instance I have a student class. Student is a Editable Root. Now  a student belongs to the following.

1 School
1 Campus
1 Cohort

This is modelled in the database as a 1 : Many between the Student and the respective Schools, Campus's and Cohort's. With the primary key of School, Campus and Cohort being stored in the
Student table.

Now in my Student class does anybody know which would be considered right way of going about things.

Would the correct way be, to store a reference to each of the above so the class would look something like this.

public class Student : .....

private School school;
private Campus campus;
private Cohort cohort;

And then in the Fetch method for student, using the relevent IDs from create the objects so

private void DataPortal_Fetch(Criteria criteria) {
     
     // Access database using ado
     School school = new School(dr.GetInt32("SchoolID");
    this.school = school;
    Campus campus = new Campus(dr.GetInt32("CampusID");
    this.campus = campus;
 
    // and so on.
}

Or am I going about things in the wrong way.

Your thoughts would be much appreciated.

Regards

Jon


Skafa replied on Sunday, February 25, 2007

If you always need those 'child' objects, then yes. that is a solution.

but why not let the UI decide wether they need to query the Db for a School object?

the UI then uses something like this:

Student s = Student.GetStudent(studentId);
School sch = School.GetSchool(s.SchoolId);
Campus c = Campus.GetCampus(s.CampusId);

another way is to lazy load (so the objects are only retrieved when asked for it):

class Student {
    private School _school
    public School School {
       get
        {
          if (_school == null || _school.Id != _schoolId)
             _school = School.GetSchool(_schoolId);
          return _school;
       }
    }
}

JonWright replied on Sunday, February 25, 2007

Some excellent suggestions. Thanks very much Smile [:)]

Jon

Copyright (c) Marimer LLC