StructureMap 存根注入

发布于 2024-12-04 10:36:08 字数 1756 浏览 1 评论 0原文

我正在使用 StructureMap 并尝试注册接口(存根)的临时实现。

阅读这篇文章后,我提出了以下设置:

接口的定义

public interface IConsoleWriter
{
    void Write(string message);
}

:常规实现:

public class ConsoleWriter : IConsoleWriter
{
    public void Write(string message)
    {
        Console.WriteLine("That's the message: '{0}'", message);
    }
}

假实现:

public class FakeConsoleWriter : IConsoleWriter
{
    public void Write(string message)
    {
        Console.WriteLine("That's a fake writer who does not care about your message.");
    }
}

现在,定义了所有这些后,我使用以下场景:

    static void TestTemporaryStub()
    {
        // register the regular implementation as default
        ObjectFactory.Initialize(x => x.For<IConsoleWriter>().Use<ConsoleWriter>());

        var writer1 = ObjectFactory.GetInstance<IConsoleWriter>();
        writer1.Write("abc");

        // temporarily inject a fake implementation
        ObjectFactory.Inject(typeof(IConsoleWriter), new FakeConsoleWriter());

        var writer2 = ObjectFactory.GetInstance<IConsoleWriter>();
        writer2.Write("abc");

        // attempt to reset the settings to default
        ObjectFactory.ResetDefaults();

        var writer3 = ObjectFactory.GetInstance<IConsoleWriter>();
        writer3.Write("abc");
    }

在上面的代码中,我要求 StructureMap 容器检索 IConsoleWriter 实现三次:

  1. 在容器初始化为使用常规实现 - 它返回常规实现 ->好的。
  2. 注入假实现后 ->它返回假实现 ->好的。
  3. 恢复默认后->应该返回常规实现(ConsoleWriter),但它仍然返回假 ->不行。

我在这里遗漏了什么吗?

I'm using StructureMap and trying to register a temporary implementation for an interface (a stub).

After reading this article I came up with the following setup:

The definition of the interface:

public interface IConsoleWriter
{
    void Write(string message);
}

The regular implementation:

public class ConsoleWriter : IConsoleWriter
{
    public void Write(string message)
    {
        Console.WriteLine("That's the message: '{0}'", message);
    }
}

The fake implementation:

public class FakeConsoleWriter : IConsoleWriter
{
    public void Write(string message)
    {
        Console.WriteLine("That's a fake writer who does not care about your message.");
    }
}

Now, having all these defined, I use the following scenario:

    static void TestTemporaryStub()
    {
        // register the regular implementation as default
        ObjectFactory.Initialize(x => x.For<IConsoleWriter>().Use<ConsoleWriter>());

        var writer1 = ObjectFactory.GetInstance<IConsoleWriter>();
        writer1.Write("abc");

        // temporarily inject a fake implementation
        ObjectFactory.Inject(typeof(IConsoleWriter), new FakeConsoleWriter());

        var writer2 = ObjectFactory.GetInstance<IConsoleWriter>();
        writer2.Write("abc");

        // attempt to reset the settings to default
        ObjectFactory.ResetDefaults();

        var writer3 = ObjectFactory.GetInstance<IConsoleWriter>();
        writer3.Write("abc");
    }

In the code above, I ask the StructureMap container to retrieve an IConsoleWriter imnplementation three times:

  1. After the container has been initialized to use the regular implementation - it returns the regular implementation -> Ok.
  2. After injecting the fake implementation -> it returns the fake implementation -> Ok.
  3. After reverting back to the defaults -> should return the regular implementation (ConsoleWriter), but it still returns the fake -> Not Ok.

Am I missing something in here?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文