jeudi 6 janvier 2022

what pattern can be used here to preview this result?

I have a method to make an order,

public async Task<bool> Order(Request request)
{
    // Each step does different things.
    await Step1(request);
    await Step2(request);
    await Step3(request);
    await Step4(request);
    ...
    await StepN(request);
}

public async Task<bool> Step1(Request request)
{
    var amount1 = await changeSomething1(request);
    await Pay1(amount1);
}

public async Task<bool> Step2(Request request)
{
    var amount2 = await changeSomething2(request);
    await Pay2(amount2);
}

public async Task<bool> StepX(Request request)
{
    var amountX = await changeSomethingX(request);
    await PayX(amountX);
}

Now I need to preview the Order without any PayX(amount) is called. I don't want to add a boolen parameter to skip it, like the following code, which looks pretty ugly.

public async Task<bool> Order(Request request, bool preview = false)
{
    await Step1(request, preview);
    await Step2(request, preview);
    await Step3(request, preview);
    await Step4(request, preview);
    ....
    await StepN(request, preview);
}

public async Task<bool> StepX(Request request, bool preview = false)
{
    var amountX = await changeSomethingX(request);
    if(!preview) await PayX(amountX);
}

What pattern can be applied here? Thanks a lot.

Aucun commentaire:

Enregistrer un commentaire