1

Currently I have code that looks like this

Status("Start Step 1");
result = Step1();
Status("End Step 1", result);

Status("Start Step 2");
result = Step2();
Status("End Step 2", result);

Status("Start Step 3");
result = Step3();
Status("End Step 3", result);

Would it be possible to somehow refactor this code and get rid of the status lines. -- however it is very important to update the status at the start and end of each step.

I thought about moving the status lines into the Step1,2,3 calls but that just clutters up the methods.

1
  • 1
    Create a function that accepts a lambda (containing Step1, 2, 3...). Call that functon with appropriate lambda; have it increment a local step number, and print "Start <step>", do the lambda, print "End Step". Commented Dec 6, 2011 at 4:30

5 Answers 5

5
public void ExecuteAndLog(Func<T> func, string startMessage, string endMessage) {
    Status(startMessage);
    var result = func;
    Status(endMessage, result);
}

with the obvious invocations

You can refine as needed. For example, building on the previous:

public void ExecuteAndLog(Func<T> func, string message) {
    ExecuteAndLog(func, "Start " + message, "End " + message);
}

making the invocations slightly simpler and even

public void ExecuteAndLog(Func<T> func, int stepNumber) {
    ExecuteAndLog(func, String.Format("Step {0}", stepNumber));
}

building on the previous. So now you can say

ExecuteAndLog(Step1, 1);
ExecuteAndLog(Step2, 2);
ExecuteAndLog(Step3, 3);
Sign up to request clarification or add additional context in comments.

1 Comment

+1, but recommend placing func last. The call would then be ExecuteAndLog("Start step 1", "End step 1", ()=>Step1())
1

Slight improvement over @Jason

public object ExecuteAndLog(Func<T> func, int number) {
    Status(string.Format("Start Step {0}", number));
    var result = func;
    Status(string.Format("End Step {0}", number), result);
    return result;
}

// and used like:

var result = ExecuteAndLog(Step1, 1);
result = ExecuteAndLog(Step2, 2);
result = ExecuteAndLog(Step3, 3);

Comments

1

Maybe something like this:

public void RunStep<T>(string stepName, Func<T> stepFunc)
    {
        Status("Start Step " + stepName);
        var result = stepFunc();
        Status("End Step " + stepName, result);
    }

Which would be called like:

RunStep("1", Step1);
RunStep("2", Step2);
RunStep("3", Step3);

Comments

0

if they all return the same result type you can do that like

    Runner(Step1, Step2, Step3);


    public void Runner<T>(params Func<T>[] stepList)
    {
        foreach (var act in stepList)
        {
            Status(act.Method.Name + "Started");
            var result=act.Invoke();
            Status(act.Method.Name + "Ended", result);  
        }


    }

Comments

0

You can use Spring.Net logger to avoid calling Status again and again.

Thanks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.