Avoiding Duplication of Code

G

Guadala Harry

I have some logic that currently processes values retrieved by looping
through a HashTable/ListDictionary structure (a hash table that contains
ListDictionary objects as its values). Logically it's just a bunch of rows
and columns - just like in a DataTable or row set returned by a DataReader.
You can visualize this as 3 columns and N rows.

I need for the same logic to be able to process identical data - but by
processing DataRows of an ADO.NET DataReader.

I don't want to create two separate methods - one to process the DataReader
and another to process the hash table - as the logic is identical with the
exception being the structure being processed.

I'd appreciate suggestions for having one method that can process both a
DataReader and the hash table structure as described above.

The p-code for the logic currently implemented with a hash table structure
is this:
1. - initialize variables
2 - start loop through hash table
3 - populate 3 string variables from the hash table
4 - process the 3 string variables
5 - finish looping through hash table
6 - return one value to caller

Of course none of the processing logic really cares about the source of the
data (beyond syntax required to loop and to retrieve the 3 string values).

One option is to have the method only process *a* hash table structure. In
this case, the method would receive the hash table as a parameter - and when
null, would create a new hash table structure from scratch and populate it
from the db via a DataReader. Then it proceeds to process the hash table
with no knowledge of where the hash table came from.

Another method is to have the looping logic differentiate between what is
being looped through (hash table or DataReader return set). It would be
capable of looping through either the hash table or DataReader return set
based on some flag variable. I think this code might get kind of ugly
pretty quickly.

Both of these methods would work - but I'd appreciate some other ideas or
feedback on these two.

If you are wondering what's going on here (the bigger picture)... live user
interection is supported by the hash table structure (live user data exists
only in the hash table structure and only optionally saved to the db), while
automated processes work from the saved data only (from the db). I'm trying
to come up with one method that can be used by both live user processing as
well as the automated jobs.

Thanks!

-G
 
N

Nick Malik

Take a look at the Strategy pattern.

1) create an interface (IRowMover) for both objects that provide for
"movenext" and "get value(n)" where n is 0,1, or 2
2) inherit a component from that interface that implements the functionality
with a hash table
3) inherit another component from that interface that implements the
functionality against a data reader

now, in your controller class:
a) if the user's data is coming from a data set, instantiate the dataset
child object. If the data is coming from the hashtable, instantiate the
hashtable child object.
b) pass the new object, of type "IRowMover" to your class which does the
work.

That class will never know, or need to know, if it is spanning a hash table
or a data reader. All it knows is that it needs some data, and that it's
time to move to the next row.

Does this make sense?

--- Nick
 

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