开发者

Best way to populate a list (or something) and make several methods use it?

I have a console application with a few methods that:

insert data1 (customers) from db 1 to db 2
update data1 from db 1 to db 2
insert data2 (contacts) from db 1 to db 2
insert data2 from db 1 to db 2

and then some data from db 2 (accessed by web services) to db 1 (MySql), the methods are initialized on execution of the application. With these inserts and updates I need to compare a field (country state) with a value in a list I get from a web service. To get the states I have to do:

GetAllRecord getAllStates = new GetAllRecord();
getAllStates.recordType = GetAllRecordType.state;
getAllStates.recordTypeSpecified = true;
GetAllResult stateResult = _service.getAll(getAllStates);
Record[] stateRecords = stateResult.recordList; 

and I can then loop through the array and look for shortname/fullname with

if (stateResult.status.isSuccess)
{
    foreach (State state in stateRecords)
        {
            if (addressState.ToUpper() == state.fullName.ToUpper())
                            {
                                addressState = state.shortname;
                            }
                }
}

As it is now I have the code above in all my methods but it takes a lot of time to fetch the state data and I have to do it many times (about 40k records and the web service only let me get 1k at a time so I have to use a "searchNext" method 39 times meaning that I query the web开发者_如何转开发 service 40 times for the states in each method.

I guess I could try to come up with something but I'm just checking what best praxis would be? If I create a separate method or class how can I access this list with all its values many times without having to download them again?

Edit: should I do something like 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 am not sure where to put it though and how to access it from my methods.


One thing first, you should add a break to your code when you get a match. No need to continue looping the foreach after you have a match.

addressState = state.shortname;
break;

40 thousand records isn´t necessarily that much in with todays computers, and I would definitely implement a cache of all the fullnames <-> shortname.

If the data don´t change very often this is a perfectly good approach.

Create a Dictionary with fullName as the key and shortName as the value. Then you can just do a lookup in the methods which needs to translate the full name to the short name. You could either store this list as a static variable accessible from other classes, or have it in an instance class which you pass to your other objects as a reference.

If the data changes, you could refresh your cache every so often.

This way you only call the web service 40 times to get all the data, and all other lookups are 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;
        }
    }
    // and some code to do the rest of the web service calls until you have all results.
 }
}

void Main()
{
   // initialize the cache
   MyCache.FillCache();
}

and in some method using it

...
string stateName = "something";
string shortName = MyCache.Cache[stateName.ToUpper()];


An easy way would be (and you really should) to cache the data locally. If I understand you correctly you do the webservice check everytime something changes which is likely unneccessary.

An easy implementation (if you can't or don't want to change your original data structures) would be to use a Dictionary somewhat like:

Dictionary<String, String> cache;
cache[addressState] = state.shortname;

BTW: You REALLY should not be using ToUpper for case insensitive compares. Use String.Compare (a, b, StringComparison.OrdinalIgnoreCase) instead.


From what I gather all the first bit of code is in some form of loop, and because of which the following line (which internally does the call to the web service) is being called 40 times:

GetAllResult stateResult = _service.getAll(getAllStates);

Perhaps you should try moving the stateResult variable to a class level scope: make it a private variable or something. So at least it will be there for the life time of the object. In the constructor of the class or in some method, make a call to the method on the object which interfaces with the ws. If you've gone with writing a method, make sure you've called the method once before you execute your loop-logic.

Hence you wouldn't have to call the ws all the time, just once.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜