-3

I have a DBReader class which reads the data from multiple databases. I have a DBDataCache class which is meant to cache data for a specific database, and has member variables, like ArrayList arlCostCentres, DataTable dtPriceLists, etc.

The DBReader has methods like ReadListOfCostCentres, ReadPriceLists, etc, which takes database name as a parameter, reads data from the database and returns to the caller. Additionally, it should cache the results into the DBDataCache class.

The problem is in each method of DBReader, I need to write the same lines of code for the cache, with the only difference being the local variable name in which I have data, and the field name of the DBDataCache class into which I will like to store the local variable.

Example: in ReadListOfCostCentres:

dbData = (DBDataCache) mapDbDataCache[dbName.ToUpper()];
if (dbData == null)
{
    dbData = new DBDataCache();
}
dbData.arlCostCentres = arlCostCentres;

In ReadPriceLists:

dbData = (DBDataCache) mapDbDataCache[dbName.ToUpper()];
if (dbData == null)
{
    dbData = new DBDataCache();
}
dbData.dtPriceLists = dtPriceLists;

Is there a way to avoid this repetitive coding and make it more generic, or at least reduce the 4 lines of code into a single line, without degrading performance?

In DBDataCache, I wish to have individual fields for each important table. I do not wish to implement a generic SortedList with key = data name (like "Cost Centers", "Pricelists"), and value = the ArrayList or DataTable as the value.

I wish to move the repetitive code to a separate function CacheData, but the code to set the field in the DBDataCache requires a unique field name every time.

Is there a solution?

Thanks.

AllSolutions
  • 1,176
  • 5
  • 19
  • 40
  • Why are you using Hungarian notation? – Dai Apr 15 '17 at 01:39
  • Also, this is something that Entity Framework and/or Automapper would take care of - is there a reason you're not using an ORM? – Dai Apr 15 '17 at 01:40
  • I do not wish to learn ORM or Entity Framework. I am currently on .NET 2.0, but do not mind moving to .NET 4.5. – AllSolutions Apr 15 '17 at 01:41
  • The database I am connecting to is a proprietory, legacy database; non-SQL compliant database – AllSolutions Apr 15 '17 at 01:42
  • Use a code generator like T4, CodeSmith, … to maintain complete control over repetitive code. – Tom Blodget Apr 15 '17 at 01:44
  • @Dai, at this moment, I am already a little far ahead into the development, and I can not migrate the entire code to ORM, and do not have the luxury of timelines, as I need to deliver it soon. I am just looking to address this issue without rewriting a whole lot of code or changing the design. – AllSolutions Apr 15 '17 at 01:46
  • @TomBlodget, code generation tools will still have the repetitive code. Only instead of me writing it, the tool will do it for me. I wish to avoid the repetitive code itself and have a cleaner easy-to-maintain code. – AllSolutions Apr 15 '17 at 01:48
  • @Dai, BTW, what is the problem in using Hungarian notation? Why did you point this out specifically? – AllSolutions Apr 15 '17 at 01:59
  • Hungarian notation, at one time Holy Script at Microsoft, is now heretical. Hint: See Dai's bio. – radarbob Apr 15 '17 at 03:38
  • Besides that, Hungarian.is.bad. Except if it were truly hungarian you'll leave out all the vowels. Class-type prefixes add absolutely nothing to understanding what the code is about. And it definitely hurts understanding. It really gets in the way when reading lots of code. You should be writing Object Oriented not .net framework class library oriented. – radarbob Apr 15 '17 at 03:43

1 Answers1

0
dbData = (DBDataCache) mapDbDataCache[dbName.ToUpper()];
dbData = dbData ?? new DBDataCache();
dbData.arlCostCentres = arlCostCentres;

I wish to move the repetitive code to a separate function CacheData, but the code to set the field in the DBDataCache requires a unique field name every time.

Perhaps I don't fully understand but passing a parameter to this separate function seems straight forward.

radarbob
  • 4,964
  • 2
  • 23
  • 36