StructureMap 存根注入
我正在使用 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 实现三次:
- 在容器初始化为使用常规实现 - 它返回常规实现 ->好的。
- 注入假实现后 ->它返回假实现 ->好的。
- 恢复默认后->应该返回常规实现(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:
- After the container has been initialized to use the regular implementation - it returns the regular implementation -> Ok.
- After injecting the fake implementation -> it returns the fake implementation -> Ok.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论