leoinlios
leoinlios

Reputation: 831

Calling methods at beginning and end - is there a better way to do this in C#?

I have several methods that have the folling structure:

    public void doWhatever()
    {
     methodToPrepareEnvironment();
     // do something
     methodToResumeEnvironment();
    }

All my "doWhatever"-type methods call these prepare/resume methods and there is only 1 prepare and 1 resume method called by all these doWhatever methods. Is there a better way to achieve this without having to put a call to the prepare and resume methods at the beginning and end of all my doWhatever methods? I have the feeling I am missing something extremely basic here. Please note, the doWhatever methods cannot be combined, they each need to be independent and they each need to prepare the environment, do some tasks and then resume the environment.

Thank you.

Upvotes: 2

Views: 308

Answers (4)

Phil Gan
Phil Gan

Reputation: 2863

With strategic use of access modifiers you can ensure external calls are always preceded and followed by method calls with little repetition like so:

public void DoWhatever()
{
    DoAction(DoingWhatever);
}

private void DoingWhatever()
{
    // ...
}

private void DoAction(Action action)
{
    methodToPrepareEnvironment();

    action();

    methodToResumeEnvironment();
}

An Action is a delegate that represents a method with no return value.

Upvotes: 5

sybkar
sybkar

Reputation: 386

A slightly different option (though not as "clean" as these options, and certainly not as intuitive to someone trying to maintain the code would be to mark the functions with an attribute, and then have a build step that injects method calls before and after:

[WrapWithEnvironmentCalls()]
public void MyFunction()
{
    // Code goes here
}

I believe the CTP Rosalyn has some functionality to approach this.

CAVEAT: I would never do this in my code, it wouldn't be maintainable, intuitive, and would likely get torn to shreds in a code review, but I'm just providing it as an alternate option for those more adventurous.

Upvotes: 1

Sleiman Jneidi
Sleiman Jneidi

Reputation: 23329

This how I would do it , Introduce a delegate that has the signature of the function to be called between methodToPrepareEnvironment() and methodToResumeEnvironment() and then attach the funcion to the delegate and call doWhatever in this way

doWhatever(Fn); 

//////

public delegate void ToDO();
class Program
{
    public void Fn()
    {

        Console.WriteLine("This is Fn");

    }

    public void doWhatever(ToDO t)
    {
        methodToPrepareEnvironment();
        t();
        methodToResumeEnvironment();
    }
}

Upvotes: 1

Oded
Oded

Reputation: 498992

Is there a better way to achieve this without having to put a call to the prepare and resume methods at the beginning and end of all my doWhatever methods?

Pass in a delegate (two built in ones are Action and Func):

public void doWhatever(Action anAction )
{
 methodToPrepareEnvironment();

 anAction();

 methodToResumeEnvironment();
} 

In regards of ensuring the Prepare and Resume are always called - the above does not cater for that, as the anAction can be called by any code that has access to it.

You can investigate AOP - a popular tool is PostSharp.

Upvotes: 3

Related Questions