设计模式:许多方法共享相同的第一步

发布于 2024-10-19 18:22:11 字数 1166 浏览 1 评论 0原文

是否有一种设计模式可以帮助我避免在许多方法中重复 DoThisStepFirst()

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
    }
}

编辑1:可以说,这是一个人为的示例。
我的实际实现包括带有参数的方法签名以及每个方法中的重要操作。

编辑2:

  • @Marc Gravell 建议面向方面编程。是的,这可能对我有帮助。
  • 我还认为责任链模式也可能对我有帮助。

@Charlie Martin 想了解有关该计划的更多信息。 这就是我实际上想做的事情。

  • 我正在尝试设置一个测试工具来针对“m”控制器和“n”控制器方法的 ASP.NET MVC 2 应用程序运行。
  • MVC 应用程序与 SessionState 紧密耦合。由于各种原因,我无法模拟 SessionState。
    • 因此,我想在 DoThisStepFirst() 方法内部初始化 SessionState。
    • 初始化 SessionState 后,我想继续执行指定的方法(这就是为什么我怀疑我可能正在寻求“责任链”设计模式)。

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:

  • @Marc Gravell suggested Aspect Oriented Programming. Yes, that might help me here.
  • I'm also thinking that the Chain-of-Responsibility pattern might help me here as well.

@Charlie Martin wanted to know more about the program.
So here's what I'm actually trying to do.

  • I'm trying to set up a test harness to run against an ASP.NET MVC 2 application for 'm' controllers and 'n' controller methods.
  • The MVC application is tightly coupled with SessionState. For various reasons, I can't mock SessionState.
    • So inside of the DoThisStepFirst() method, I'd like to initialize SessionState.
    • And after initializing SessionState, I'd like to proceed to a specified method (which is why I suspect that I might be seeking the 'Chain of Responsibility' design pattern).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

谁与争疯 2024-10-26 18:22:11

如果它是测试工具,那么您的单元测试框架应该有一个 TestFixtureSetup 方法,它将在您的测试运行之前首先运行:

对于 NUnit:

[TestFixtureSetUp]
public void Setup()
{

}

对于 MSTest:

[TestInitialize()]
public void Startup()
{
    //Do setup here
}

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
}
月棠 2024-10-26 18:22:11

我想您可能有一个始终调用 DoThisStepFirst() 然后执行传递的操作的方法:

    private static void DoSomething(Action doSomethingElse)
    {
        DoThisStepFirst();
        doSomethingElse();
    }

I guess you could have a method that always calls DoThisStepFirst() then performs the passed action:

    private static void DoSomething(Action doSomethingElse)
    {
        DoThisStepFirst();
        doSomethingElse();
    }
牛↙奶布丁 2024-10-26 18:22:11
private static void MethodCommon(Func f)
{
    DoThisStepFirst();
    f();
}

private static void Method1()
{
    MethodCommon(() => 
        doSomething();
    );
}
private static void MethodCommon(Func f)
{
    DoThisStepFirst();
    f();
}

private static void Method1()
{
    MethodCommon(() => 
        doSomething();
    );
}
清晰传感 2024-10-26 18:22:11

老实说,如果不了解更多有关该程序的信息,就无法回答这个问题。看起来“DoThisStepFirst”是某种初始化;很容易想到

  DoThisStepFirst();
  Method1(); // and so on

等等,但似乎每个方法都需要初始化。那么问题是“为什么每个方法都处于状态中,所以你需要不断重新初始化?”

从另一个方向来看,您可以想象程序中您确实必须在每个步骤之前进行一些初始化,并且这是相同的初始化,在这种情况下您会陷入困境。

To be honest, there's no way to answer this without knowing more about the pgram. It looks like "DoThisStepFirst" is some kind of initialization; it's tempting to think of

  DoThisStepFirst();
  Method1(); // and so on

and so on, but it appears each method needs the initialization. So then the question is "why is each method piddling in the state so you need to keep reinitializing?"

Going the other direction, you can imagine programs in which you really must do some initialization befre each step, and it's the same initialization, in which case you're pretty well stuck.

剧终人散尽 2024-10-26 18:22:11

将代码放在构造函数中的 DoThisStepFirst() 中怎么样?如果他们所有人都需要它运行才能在那里初始化,那么它很有可能会进入那里。除非它重置了在您的方法执行任何操作之前需要重置的状态/行为。

How about put the code in DoThisStepFirst() it in the constructor. If all of them need it to run in order to init there, operations there's a good chance it could go in there. Unless it resets states/behaviors that need to be reset before your methods execute anything.

秉烛思 2024-10-26 18:22:11

不,一般没有。也许您可以提供一些有关您想要实现的目标的详细信息?

No, there generally is not. Maybe you could provide some details about what you are trying to accomplish?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文