views:

14

answers:

2

Here's what I'd like to achieve. After mucking around with it for a day and a half, though, I've become nearly convinced that it can't be done. If someone can show me how to achieve this, I'd be quite grateful.

I have a .NET parent class Parent with a shared method Fetch(). Fetch can take a variety of parameters to help it know what particular records to fetch, but that's irrelevant at the moment.

Fetch() needs to know what table to look in to find the records its after. The table is different for each child class that inherits Parent. I'd like to keep the table name in a string that's a property of the Child class. Then each Child can provide its own table name, and Parent can handle the Fetching which works the same way for all of them.

Once it's executed the query, Fetch() should then have a DataReader. All Child classes have a constructor which can receive a DataReader as a parameter and use it to produce an instance of itself with attributes populated from the reader.

One thing I have to stress: Fetch is a Shared method. If I were designing it from scratch, I might not make it so, but as it is I'm refactoring the code of some absolute moron (who may or may not have been me 2 years ago) who went and built a handful of projects that use Fetch() as a shared method of each Child class (or rather, each class that I want to make into a child of this new Parent), and it sure would be nice not to have to change every occurrence in those projects to instances.

EDIT: What I'm running into specifically is that I cannot figure out how to get the tablename into Fetch(), nor how to determine the child class in order to call its constructor on the resulting DataReader.

So to recap:

  • Parent has Shared Fetch() method
  • Method is called as in Child.Fetch()
  • Parent gets table info from Child
  • Fetch() knows class name of Child so it can call the constructor with the DataReader it fetches.

Can it be done? Many thanks.

A: 

Parent can have a MustOverride property that Child would be required to implement, that could contain the table information. That solves the problem of how Parent knows what table to look in for each Child, but you cannot have a MustOverride Shared member, so you'd need instances of Child to work with in Parent.

Maybe you could have a generic Fetch on parent that created instances of Child of the correct type based on the type parameter. It's certainly possible given the correct constraints.

Mark
Yes, the unoverridability of Shared methods is what's tripping me up here.Creating an instance of Child inside of Parent wouldn't be bad, but I can't see how to know what class of child to instantiate an instance of. Thoughts?
Blumer
Not without altering the method signature of Fetch(), unfortunately. Either you'd have to pass in some information that gave Fetch() something to go on, or you'd have to make Fecth() generic, i.e. Fetch(Of T As {New})()
Mark
A: 

If Fetch knows the full name of the child class then it should be able to use reflection to get hold of the type of the child and from there I would expect you'll find it plain sailing.

Will A