How to Populate Base + Child Class Fields

J

Jordan

Suppose I have a system that keeps track of 5 different types of "People".

My intent is to have a base Person class, then 5 derived classes for each of
the specific person types (e.g., Patient, Doctor, Employee, Contractor,
etc).

Now, at runtime I need to retrieve all the property values from a database
and populate all of the fields of both the base and derived classes. Not all
fields will need to be exposed via public properties as they will be used
internally (to the class).

I would like to have one single method that talks to the DAL and populates
all fields.

What is the standard (or at least an acceptable) way to make this happen?
Should I put some "PopulateMe()" method (which calls the DAL) in the derived
class AND have it populate the base class fields? Something just doesn't
seem right about doing it that way... but the alternative would apparently
be to have some sort of "PopulateMe()" method in both the base class and
derived class - each one populating its respective fields. But that seems
sort of wrong as well.

Thoughts? Opinions? Perspective? Recommendations?

Thanks!
 
G

Guest

What I would do in the abstract base class is create a private Method
"PopulateBase()" that populates the base properties. Then I would create an
abstract method called "PopulateMe()". Each derived class's "PopulateMe()"
method would call base.PopulateBase(), and then populate it's specific
properties.

public abstract People
{
private void PopulateBase()
{
//Populate the base properties.
}
abstract public PopulateMe();
}

public Doctor : People
{
override public PopulateMe()
{
base.PopulateBase();
//Start populating the Doctor Properties.
}
}

Just an idea.....
 
G

Guest

I'd like to correct myself. Declare PopulateBase() as protected.

public abstract class People
{
protected int m_baseValue;

protected void PopulateBase()
{
this.m_baseValue = 10;
}

abstract public void PopulateMe();

}

public class Doctor : People
{
private int m_doctorValue;

public override void PopulateMe()
{
base.PopulateBase();
this.m_doctorValue = 20;

}
}
 
J

Joanna Carter [TeamB]

"Jordan" <[email protected]> a écrit dans le message de %[email protected]...

| Suppose I have a system that keeps track of 5 different types of "People".
|
| My intent is to have a base Person class, then 5 derived classes for each
of
| the specific person types (e.g., Patient, Doctor, Employee, Contractor,
| etc).

As well as what rmacias says, you might like to consider using the Visitor
pattern to do the populating, then you don't have to include the populating
code in the business classes; what is more, you could change what the
visitor does if you decide to change storage medium.

You might also like to consider what would happen if a Person could be both
a Doctor and a Patient, depending on how sick they were :)

In this situation, you should not use inheritance but composition to express
the concept of a Role.

You need a base class Role from which you derive Patient, Doctor, Employee,
Contractor, etc; then you add a list of 0..n Roles in the Person class.

This then allows a Person to have no Roles, through to multiple simultaneous
Roles.

Joanna
 

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