Interface question

  • Thread starter Thread starter Patrick
  • Start date Start date
P

Patrick

Hello all,
I have a bunch of classes that implement the same static methods (same
name and same parameters).
However, their return types are different because they return an IList of
different types. I'm looking for a way to generically call these methods
without knowing the Type at runtime. Could someone please help with some
advice?

Here's my current work around:

IList Results = null;

switch (DataObject.Name)

{

case "AccountHistories":

Results = AccountHistories.RetrieveQuery(whereClause);

break;

case "ApplicationFiles":

Results = ApplicationFiles.RetrieveQuery(whereClause);

break;

case "Applications":

Results = Applications.RetrieveQuery(whereClause);

break;

case "ApplicationSettings":

Results = ApplicationSettings.RetrieveQuery(whereClause);

break;
 
Patrick said:
Hello all,
I have a bunch of classes that implement the same static methods (same
name and same parameters).
However, their return types are different because they return an IList of
different types. I'm looking for a way to generically call these methods
without knowing the Type at runtime. Could someone please help with some
advice?

Here's my current work around:

IList Results = null;

switch (DataObject.Name)

{

case "AccountHistories":

Results = AccountHistories.RetrieveQuery(whereClause);

break;

case "ApplicationFiles":

Results = ApplicationFiles.RetrieveQuery(whereClause);

break;

case "Applications":

Results = Applications.RetrieveQuery(whereClause);

break;

case "ApplicationSettings":

Results = ApplicationSettings.RetrieveQuery(whereClause);

break;
Are you looking for something like this:

interface IApplication
{
IList RetrieveQuery(whereClause);
}

Then , all your class (AccountHistories ...) can implement that
interface, then you should be able to call that method from all those
classes.
 
Patrick said:
Thanks John for the response. Does it matter if those methods are
static?



Yes, it matters a lot that they are static methods.

I don't know that this is the *best* approach, but, since all the
methods take the same parameter type (I assume a string) and they all
return an IList, you could declare a delegate and then put instances of
the delegate into a Hashtable, keyed on DataObject.Name. I'm thinking
something like this:

delegate IList QueryMethods(string query);
Hashtable methodTable = new Hashtable();

// Load up the methods...
methodTable.Add("AccountHistories", new
QueryMethods(AccountHistories.RetrieveQuery));
methodTable.Add("ApplicationFiles", new
QueryMethods(ApplicationFiles.RetrieveQuery));
// ...

Then, when you need to call a method, instead of your switch()
statement you do this:

IList r = ((QueryMethods)methodTable[DataObject.Name])(whereClause);
 
"Patrick" <cparker@noharvesting> a écrit dans le message de (e-mail address removed)...

| I have a bunch of classes that implement the same static methods (same
| name and same parameters).
| However, their return types are different because they return an IList of
| different types. I'm looking for a way to generically call these methods
| without knowing the Type at runtime. Could someone please help with some
| advice?
|
| Here's my current work around:
|
| IList Results = null;
|
| switch (DataObject.Name)
|
| {
|
| case "AccountHistories":
|
| Results = AccountHistories.RetrieveQuery(whereClause);
|
| break;
|
| case "ApplicationFiles":
|
| Results = ApplicationFiles.RetrieveQuery(whereClause);
|
| break;
|
| case "Applications":
|
| Results = Applications.RetrieveQuery(whereClause);
|
| break;
|
| case "ApplicationSettings":
|
| Results = ApplicationSettings.RetrieveQuery(whereClause);
|
| break;

Unless you are using .NET 2.0, you will have to use the Class Factory
pattern. But that can be implemented by slightly modifying your getter
method like this :

{
public IList RetrieveQuery(Type itemType, string whereClause)
{
if (typeof(AccountHistories) == itemType)
return ....
else
...
}
}

Joanna
 
Excellent Tony. Thanks a lot.
--PW

Tony LaPaso said:
Patrick said:
Thanks John for the response. Does it matter if those methods are static?



Yes, it matters a lot that they are static methods.

I don't know that this is the *best* approach, but, since all the methods
take the same parameter type (I assume a string) and they all return an
IList, you could declare a delegate and then put instances of the delegate
into a Hashtable, keyed on DataObject.Name. I'm thinking something like
this:

delegate IList QueryMethods(string query);
Hashtable methodTable = new Hashtable();

// Load up the methods...
methodTable.Add("AccountHistories", new
QueryMethods(AccountHistories.RetrieveQuery));
methodTable.Add("ApplicationFiles", new
QueryMethods(ApplicationFiles.RetrieveQuery));
// ...

Then, when you need to call a method, instead of your switch() statement
you do this:

IList r = ((QueryMethods)methodTable[DataObject.Name])(whereClause);
 
Hi Joanne,
ahhh. Duh, ok I'm learning... I am using dotNet V2. Is there a better way
available with V2?
 
"Patrick" <cparker@noharvesting> a écrit dans le message de (e-mail address removed)...

| ahhh. Duh, ok I'm learning... I am using dotNet V2. Is there a better way
| available with V2?

{
public IList<T> RetrieveQuery<T>(string whereClause)
{
return ....
}
}


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

Back
Top