vendredi 14 avril 2017

Avoid repetitive coding to transfer local variable to an object member variable

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 member 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 members 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.

Is there a solution?

Thanks.





Aucun commentaire:

Enregistrer un commentaire