在结构图中自动连接命名实例时出现问题

发布于 2024-09-18 15:33:52 字数 1183 浏览 1 评论 0原文

我是结构图新手,到目前为止运气很好。然而,有一个方面我遇到了一些问题。这是尝试使用命名实例和较新的 .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 技术交流群。

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

发布评论

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

评论(2

胡渣熟男 2024-09-25 15:33:52

当您指定多个实例时,请使用 Add() 而不是 Use()Use() 是旧 TheDefaultIs() 的同义词。

ObjectFactory.Configure(x => x.For<ITestClass>().Add(blueInstance.Object)
    .Named("Blue"));

When you are specifying multiple instances, use Add() instead of Use(). Use() is a synonym for the old TheDefaultIs().

ObjectFactory.Configure(x => x.For<ITestClass>().Add(blueInstance.Object)
    .Named("Blue"));
呢古 2024-09-25 15:33:52

事实证明我需要使用“With”来做我想做的事情。这最终是一个两步过程,但实现了我想要的。

var greenFromContainer = ObjectFactory.GetNamedInstance<ITestClass>("Green");
var actual = ObjectFactory.With(greenFromContainer).GetInstance<TestTarget>();

更复杂的场景似乎最好使用单独的容器/注册表或利用扩展的构造函数规则来处理。

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.

var greenFromContainer = ObjectFactory.GetNamedInstance<ITestClass>("Green");
var actual = ObjectFactory.With(greenFromContainer).GetInstance<TestTarget>();

More complicated scenarios would seem to be best handled with separate containers/registries or leveraging the extended constructor rules.

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