Is there a design pattern that can help me avoid repeating DoThisStepFirst() in many methods?
class Program
{
static void Main(string[] args)
{
Method1();
Method2();
MethodN();
}
private static void DoThisStepFirst()
{
// Implementation
}
private static void Method1()
{
DoThisStepFirst();
// Implementation
}
private static void Method2()
{
DoThisStepFirst();
// Implementation
}
// {...}
private static void MethodN()
{
DoThisStepFirst();
// Implementation
}
}
EDIT 1: Suffice it to say, this is a contrived example.
My actual implementation includes method signatures with parameters and non-trivial operations in each method.
EDIT 2:
@Charlie Martin wanted to know more about the program. So here's what I'm actually trying to do.
DoThisStepFirst() method, I'd like to initialize SessionState.If it's a Test Harness, then your Unit Test framework should have a TestFixtureSetup method, it'll run first before your tests are run:
For NUnit:
[TestFixtureSetUp]
public void Setup()
{
}
For MSTest:
[TestInitialize()]
public void Startup()
{
//Do setup here
}
I guess you could have a method that always calls DoThisStepFirst() then performs the passed action:
private static void DoSomething(Action doSomethingElse)
{
DoThisStepFirst();
doSomethingElse();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With