jeudi 6 avril 2017

How to sequentially check various states, ensuring the slowest is done last

I have an api which checks the status of a user

There are 5 possible states. 3 of them are fast to retrieve. 2 of them are v. slow. So I only want to check the slow states if I've ruled out they are not one of the fast states

I originally had a list of List<Tuple<bool, SomeStateEnum>> with 5 entries. and a linq query to find the single one that is true. (the thing in the first item of the Tuple is a property in the class , which contains method calls for various lookups).

But I have just realised this means all values get evaluated. Which is obviously not what I want

So I could change this to a bunch of 5 if statements that check the fastest thing first and the slowest thing last

Could also have a chain of responsibility where the last thing in the chain is the slowest thing to check

but I get the feeling there may be a smarter way or more appropriate pattern. Kind of like lazy-loading. Only do the slow lookup if you really need to. I did wrap the slow stuff in a Func in the property getter, but by putting them all in the List<Tuple<bool, SomeStateEnum>> they still get evaluated any ideas?

Aucun commentaire:

Enregistrer un commentaire