c# - Best way to populate a list (or something) and make several methods use it? -
i have console application few methods that:
insert data1 (customers) db 1 db 2 update data1 db 1 db 2 insert data2 (contacts) db 1 db 2 insert data2 db 1 db 2
and data db 2 (accessed web services) db 1 (mysql), methods initialized on execution of application. these inserts , updates need compare field (country state) value in list web service. states have do:
getallrecord getallstates = new getallrecord(); getallstates.recordtype = getallrecordtype.state; getallstates.recordtypespecified = true; getallresult stateresult = _service.getall(getallstates); record[] staterecords = stateresult.recordlist;
and can loop through array , shortname/fullname with
if (stateresult.status.issuccess) { foreach (state state in staterecords) { if (addressstate.toupper() == state.fullname.toupper()) { addressstate = state.shortname; } } }
as have code above in methods takes lot of time fetch state data , have many times (about 40k records , web service let me 1k @ time have use "searchnext" method 39 times meaning query web service 40 times states in each method.
i guess try come i'm checking best praxis be? if create separate method or class how can access list values many times without having download them again?
edit: should this: getallrecord getallstates = new getallrecord(); getallstates.recordtype = getallrecordtype.state; getallstates.recordtypespecified = true; getallresult stateresult = _service.getall(getallstates); record[] staterecords = stateresult.recordlist; dictionary<string, string> allstates = new dictionary<string, string>(); foreach (state state in staterecords) { allstates.add(state.shortname, state.fullname); }
i not sure put though , how access methods.
one thing first, should add break code when match. no need continue looping foreach after have match.
addressstate = state.shortname; break;
40 thousand records isn´t in todays computers, , implement cache of fullnames <-> shortname.
if data don´t change approach.
create dictionary fullname key , shortname value. can lookup in methods needs translate full name short name. either store list static variable accessible other classes, or have in instance class pass other objects reference.
if data changes, refresh cache every often.
this way call web service 40 times data, , other lookups in memory.
code sample (not tested):
class mycache { public static dictionary<string,string> cache = new dictionary<string,string>(); public static void fillcache() { getallrecord getallstates = new getallrecord(); getallstates.recordtype = getallrecordtype.state; getallstates.recordtypespecified = true; getallresult stateresult = _service.getall(getallstates); record[] staterecords = stateresult.recordlist; if (stateresult.status.issuccess) { foreach (state state in staterecords) { cache[state.fullname.toupper()] = state.shortname; } } // , code rest of web service calls until have results. } } void main() { // initialize cache mycache.fillcache(); }
and in method using it
... string statename = "something"; string shortname = mycache.cache[statename.toupper()];
Comments
Post a Comment