migrating to List<T> from ArrayList

D

David C

In my business layer, I have Person, and Patient which derives from
Person.

//base class for all single classes
public class BaseItem{}

public class Person:BaseItem{}

public class Patient:persons{}

Then I have a collection class for each of the above. Notice that the
pattern of inheritance follows that of the single classes. Just as
Person inherits from BaseItem, Persons inherits from BaseItems, and
just as Patient inherits from Person, Patients inherits from Persons.

public class BaseItems:ArrayList{}

public class Persons:BaseItems{}

public class Patients : Persons{}

This design pattern worked pretty well with 1.1. The collection class
Patients only had instances of Patient, but obviously only the naming
convention kept me from adding to Patients instances of other
classes. There is no type safety.

So enter List<T>. With this, I would like Persons to have instances
of only Person, and Patients only instances of Patient. This is what
I have so far.

public class BaseItems<T>:List<T> where T:BaseItem
{}

public class Persons:BaseItems<Person>
{}

So far so good. I am stomped at Patients. I want it to inherit from
Persons (I don't want to get into why.), but in Patients, I would like
to allow only instances of Patient. Thanks in advance.

(Addendum) If you must know the justification behind my inheritance
pattern of collection classes, please read on. Someone from another
forum asked why not just just use plain List<Person> and
List<Patient> ?

Here is my current design pattern. While not type safe, it has worked
pretty well.

BaseItems:ArrayList - This is the base class to all Collection
classes. It has all the data classes necessary to retrieve rows of
data necessary to populate collections.

Persons:BaseItems - This is a base class to Patients, Doctors, and
Nurses (I am making this up at this point, but the pattern is similar
my project). That is because the same table stores patients, doctors
and nurses. The Persons class knows all that.

Patients:persons
Doctors:persons
Nurses:persons

This is what my client code looks like

Patients patients = new Patients();
//get all patients and only patients in California
patients.FilterState = 'CA';
patients.Retrieve();

//get all persons (patients, doctors, nurses) in California
Persons persons = new Persons();
persons.FilterState = 'CA';
persons.Retrieve();
 
A

Andy

Try using

public class Persons<T> : ItemsBase<T>
where T : Persons<T> { }

Generics and inheritance don't mix too well, but this should get you
what you want.

Andy
 
J

Jeremy Shovan

Will this work for you?

public class BaseItem {}

public class Person : BaseItem {}

public class Patient : Person {}

public class BaseItems<T> : List<T> where T : BaseItem {}

public class Persons<T> : BaseItems<T> where T : Person {}

public class Patients : Persons<Patient> {}

Jeremy Shovan
 
G

gerry

Off the top ....

public class BaseItems<T>:List<T> where T:BaseItem
{}

public class BasePersons<T>:BaseItems<T> where T:person
{}

public class Persons:BasePersons<Person>
{}

public class Patients:BasePersons<Patient>
{}
 
D

David C

It works, but now the problem is this.

If I want to have another class to inherit Patients, then I would have
to change Patients to

public class Patients<T> : Persons<T> where T : Patient{}

Then obviously I have to go through all the client code for Patients
and add <Patient>.

Not that I plan on going crazy with inheritance, but it seems to be a
major undertaking to derive from a class.
 
J

Jeremy Shovan

Why is it that you need a class hierarchy for your collections anyways
unless you need custom code in your collections?

Why not just do this?

public class BaseItem { }

public class Person : BaseItem { }

public class Patient : Person { }

public class Test
{
public static void DoSomething()
{
IList<Person> personList = new List<Person>();
// do somethign with person list here.

IList<Patient> patientList = new List<Patient>();
// do somethign with patient list here.
}
}


Or if you really do need your own collection type to do some sort of
operation on your entities just do this.

public class BaseItem { }

public class Person : BaseItem { }

public class Patient : Person { }

public class ItemList<T> : List<T> where T : BaseItem
{
public void SpecialOpperation()
{
// do stuff here.
}
}

public class Test
{
public static void DoSomething()
{
ItemList<Person> personList = new ItemList<Person>();
ItemList<Patient> patientList = new ItemList<Patient>();

}
}

If you need to do some special process for each collection type you could
abstract the functionality into separate processor classes for each type
like the following. This will allow you to manipulate each collection type
differently but save you from creating an entire hierarchy of collection
types.

public class BaseItem { }

public class Person : BaseItem { }

public class Patient : Person { }

public class ItemList<T> : List<T> where T : BaseItem
{
public void SpecialOpperation()
{
// do stuff here.
}
}

public class PersonListProcessor
{
public static void DoSomething(IList<Person> personList)
{
// ...
}
}

public class PatientListProcessor
{
public static void DoSomething(IList<Patient> personList)
{
// ...
}
}

public class Test
{
public static void DoSomething()
{
ItemList<Person> personList = new ItemList<Person>();
PersonListProcessor.DoSomething(personList);

ItemList<Patient> patientList = new ItemList<Patient>();
PatientListProcessor.DoSomething(patientList);
}
}

Jeremy Shovan
 

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