lundi 25 novembre 2019

Calculate value in API call and remember it across the whole request

I am looking for the best design to implement the following. I have ClassA and ClassB A method called MethodA in ClassA calls MethodB1 in ClassB. MethodB1 is something like

public bool MethodB1()
{
    var x = ClassC.CalculateX();
    if(x)
    {
        return false;
    }

    //do something else;
    return true;
}

MethodA calls MethodB1 only once.

There are other methods in ClassB that call MethodB1 multiple times in a loop.

Typically we have two scenarios:

  1. API endpoint => MethodA => MethodB1 (here we know the value of x that never changes in a single call) => MethodB2 => MethodB1 (called multiple times in a loop)

  2. API endpoint => MethodB2 => MethodB1 (called multiple times in a loop and we will get the value of x that is not going to change in a single call)

Getting the value of x is expensive operation, I want to do it once in the first scenario and remember it in the current call to be used in other places in ClassA and ClassB. Any suggestions are welcome.

Aucun commentaire:

Enregistrer un commentaire