在结构图中自动连接命名实例时出现问题
我是结构图新手,到目前为止运气很好。然而,有一个方面我遇到了一些问题。这是尝试使用命名实例和较新的 .For().Use() 语法自动连接。
无论我做什么,当我尝试自动连接以创建新对象时,我都会得到为给定类型配置的最后一个项目。当我只是尝试从容器中获取该类型时,我可以使用命名实例毫无问题地获取正确的项目。
为了简单起见,下面显示了 ObjectFactory,但我的实际代码使用的是 IContainer。两者对我来说都有相同的结果。
//arrange
var expectedGreen = "Green";
var notExpectedBlue = "Blue";
var greenInstance = new Mock<ITestClass>();
greenInstance.SetupGet(m => m.Data).Returns(expectedGreen);
ObjectFactory.Configure(x => x.For<ITestClass>().Use(greenInstance.Object).Named("Green"));
var blueInstance = new Mock<ITestClass>();
blueInstance.SetupGet(m => m.Data).Returns(notExpectedBlue);
ObjectFactory.Configure(x => x.For<ITestClass>().Use(blueInstance.Object).Named("Blue"));
//act
var actual = ObjectFactory.GetNamedInstance<TestTarget>("Green");
//assert
Assert.AreEqual(expectedGreen, actual.TestClass.Data);
TestTarget 类有一个采用 ITestClass 作为参数的构造函数。我的期望是,通过请求 TestTarget 的实例,我会得到一个构造的 TestTarget 返回,它将引用名为“Green”的 ITestClass。
最终,我的目标是模拟这样一种情况:我有两个相同类型的对象,可以在容器中访问它们(例如使用两个数据库的应用程序)。解决此类问题的替代方法也是一个很好的答案。但是,我想坚持使用 2.5+ 方法来执行此操作。
I'm new to structure map and so far having good luck. However, there's one area I'm having a bit of a problem with. This is trying to auto-wireup using named instances and the newer .For().Use() syntax.
No matter what I do, I'm getting the LAST item I've configured for a given type when I'm trying to auto-wire to create a new object. I can get the correct item using named instances without a problem when I'm just trying to get that type from the container.
For simplicity's sake, below shows ObjectFactory, but my real code is using an IContainer. Both have same result for me.
//arrange
var expectedGreen = "Green";
var notExpectedBlue = "Blue";
var greenInstance = new Mock<ITestClass>();
greenInstance.SetupGet(m => m.Data).Returns(expectedGreen);
ObjectFactory.Configure(x => x.For<ITestClass>().Use(greenInstance.Object).Named("Green"));
var blueInstance = new Mock<ITestClass>();
blueInstance.SetupGet(m => m.Data).Returns(notExpectedBlue);
ObjectFactory.Configure(x => x.For<ITestClass>().Use(blueInstance.Object).Named("Blue"));
//act
var actual = ObjectFactory.GetNamedInstance<TestTarget>("Green");
//assert
Assert.AreEqual(expectedGreen, actual.TestClass.Data);
The class TestTarget has a constructor that takes an ITestClass as a parameter. My expectation is that by asking for an instance of TestTarget, I'd get a constructed TestTarget back that would reference the ITestClass named "Green".
Ultimately, my goal is to simulate a case where I have two objects of the same type that would be accessed in the container (such as an app that worked with two databases). An alternative approach for solving that type of problem would be a good answer too. However, I want to stick to the 2.5+ methods for doing this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您指定多个实例时,请使用
Add()
而不是Use()
。Use()
是旧TheDefaultIs()
的同义词。When you are specifying multiple instances, use
Add()
instead ofUse()
.Use()
is a synonym for the oldTheDefaultIs()
.事实证明我需要使用“With”来做我想做的事情。这最终是一个两步过程,但实现了我想要的。
更复杂的场景似乎最好使用单独的容器/注册表或利用扩展的构造函数规则来处理。
Turns out I needed to use "With" to do what I was trying to do. This ultimately is a two step process, but accomplishes what I wanted.
More complicated scenarios would seem to be best handled with separate containers/registries or leveraging the extended constructor rules.