Parent-child classes and circular references

M

Matteo Migliore

Hi.

I've a problem with parent-child circular references.

Suppose have two classes, Person and Course:
--------------------------
public class Person{
public Person(string name, Course course){
this.name = name;
this.course = course;
}

string name;
public string Name { get{ return name;} }

Course course;
public Course Course { get{ return course;} }
}

public class Course{
public Course(string name, IList<Person> persons){
this.name = name;
this.persons = persons;
}

string name;
public string Name { get{ return name;} }

IList<Person> persons;
public Person Perons { get{ return persons;} }
}
--------------------------

How can I initialize a Person and a Course with their constructors?
Which class to create before?

Suppose that constructors check for not null.

Is this a correct solution (I intend for design, not for compile :) )?
 
S

Samuel R. Neff

My suggestion is to have the people list instantiated by Course
constructor and have the Person constructor assign itself to the
passed course. That reduces your calling code to two lines--one to
create course, one to create person.

HTH,

Sam


class Course {
private IList<Person> people = new List<Person>();
private string name;

public Course(string name) {
this.name = name;
}
public IList<Person> People { get { return people; } }
}

class Person {
private string name;
public Person(string name, Course course) {
this.name = name;
course.People.Add(this);
}
}
 
I

Ido Samuelson

Course[] courses = Registration.GetPersonCourses(person);
Person[] persons = Registration.GetCoursePersons(course);

Registration.AddPersonToCourse(Person, course);

this way we decoupled course and person.
 
M

Matteo Migliore

My suggestion is to have the people list instantiated by Course
constructor and have the Person constructor assign itself to the
passed course. That reduces your calling code to two lines--one to
create course, one to create person.

Thanks for your answer but the problem is that
IList<Person> Persons of class Course could be a ReadOnlyCollection<T>
in a real-world application.

And I want to check list lenght
when passed to the constructor of class Course.

So, how can I do?

Thx! ;-)
Matteo Migliore.
 
P

Peter Duniho

Matteo said:
Thanks for your answer but the problem is that
IList<Person> Persons of class Course could be a ReadOnlyCollection<T>
in a real-world application.

And I want to check list lenght
when passed to the constructor of class Course.

If you have imposed the requirement that the collection must be
completely initialized before the Course class is instantiated, then you
have no choice in the matter. You must provide a way to assign a Course
to a Person after the Person has been instantiated.

You may find it makes sense, using that design, to have the Course class
do that adding in its own constructor. So it would enumerate the list,
adding itself to each Person in the list. That at least would clean up
some of the initialization code.

That said, if you can at all do what Samuel suggests, I think you
should. Or at least something like it. It's not exactly the same, but
the Control.Controls collection is sort of similar; that is, it provides
a method for manipulating a collection that is actually encapsulated in
the class, in a way that understands the relationship between the parent
and child. The constraints you've put on the problem make for some
fairly unwieldy code IMHO.

Also, I find it a little odd that a Person can have only one Course.
Granted, I don't know the context, but in most educational settings, a
single individual can be enrolled in multiple courses, just as a single
course will generally have multiple individuals enrolled.

The other thing that I find odd is the implication in your example code
that you may modify the Course's collection from outside the Course.
This is contrary to your statement that the collection might be
read-only anyway, but even if it weren't it seems like a poor design.
The Course class really ought to be in sole control of the collection of
Persons enrolled in the Course.

Just my two cents.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top