5

I have a method something like this:

public Something MyMethod()
{
    Setup();

    Do something useful...

    TearDown();

    return something;
}

The Setup and TearDown methods are in the base class.

The problem I'm having is that I have to write this type of method over and over again with the Setup() and TearDown() method calls.

EDIT: The tricky part of this method is that "Do something useful..." is specific to this method only. This part is different for every method I create.

Also, I can have MyMethod2, MyMethod3, in a single class. In all cases, I would like to run the setup and teardown

Is there an elegant way of doing this without having to write this every single time?

Perhaps I'm delusional, but is a way to add an attribute to the method and intercept the method call, so I can do stuff before and after the call?

Steven
  • 714
  • 2
  • 8
  • 21
  • 1
    This is a duplicate of plenty of questions. Did you try searching? You can use delegates, events, abstract methods, aspect-oriented programming and so on. For example [Run a method before all methods of a class](http://stackoverflow.com/questions/9192709/run-a-method-before-all-methods-of-a-class), [Call base function then inherited function](http://stackoverflow.com/questions/3747711/), [Execute code before the called function is executed](http://stackoverflow.com/questions/10398530), [Execute Method Before and After Code Block](http://stackoverflow.com/questions/26484206/)and so on. – CodeCaster Dec 23 '15 at 13:46
  • 2
    You want to use Aspect Oriented Programming. There are a lot of tools for that. – Yacoub Massad Dec 23 '15 at 13:46
  • 1
    Thanks CodeCaster, but none of the postings you mentioned help in my case because in my case I can have many of these "MyMethods" in a single class with different method names, different parameters, and different return values. – Steven Dec 23 '15 at 15:30
  • 1
    Use "@username" to notify someone of your reply. I have reopened your question. – CodeCaster Dec 23 '15 at 16:36
  • Anyway why don't the answers [here](http://stackoverflow.com/questions/26484206/execute-method-before-and-after-code-block) help? – CodeCaster Dec 23 '15 at 16:38
  • This requires refactoring, and not enough information is given to suggest the proper approach to use in the refactor. – Travis J Dec 29 '15 at 21:36

2 Answers2

8

Just implement this method in abstract base class like this:

public Something MyMethod()
{
    Setup();

    DoSomethingUsefull();

    TearDown();

    return something;
}

protected abstract DoSomethingUsefull();

Now you need to override only one method in inherited classes - DoSomethingUsefull()

This is Template Method pattern

Dzianis Yafimau
  • 2,034
  • 1
  • 27
  • 38
2

Use generics, lambdas and delegates like so:

public SomeThing MyMethod()
{
    return Execute(() =>
    {
        return new SomeThing();
    });
}


public T Execute<T>(Func<T> func)
{
    if (func == null)
        throw new ArgumentNullException("func");

    try
    {
        Setup();

        return func();
    }
    finally
    {
        TearDown();
    }
}
Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
Aaron Carlson
  • 5,522
  • 4
  • 31
  • 35
  • This is probably the closest to what I was thinking, but the main point was to remove having to write setup/teardown repeatedly. This code removes it but adds extra layer of code. – Steven Dec 29 '15 at 21:23
  • You should look at AOP frameworks if you want to use attributes to solve this problem. – Aaron Carlson Jan 03 '16 at 03:19