在 StructureMap 中重置 ObjectFactory [英] Reset ObjectFactory in StructureMap

查看:24
本文介绍了在 StructureMap 中重置 ObjectFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些依赖于 StructureMap 的单元测试,所以我想在我的 [SetUp] 方法中完全重置 ObjectFactory.这就是我的 [SetUp] 方法现在的样子:

I'm writing some unit tests that rely on StructureMap so I want to completely reset the ObjectFactory in my [SetUp] method. This is what my [SetUp] method looks like right now:

[SetUp]
public void SetUp()
{
    ObjectFactory.Initialize(initializationExpression => {});
}

这似乎重置了配置,因为我可以执行 ObjectFactory.WhatDoIHave() 方法并且它不包含我的任何配置.但是,缓存的对象实例不会被移除,而是会在后续测试中返回.有没有办法完全重置 ObjectFactory?

This appears to reset the configuration because I can execute the ObjectFactory.WhatDoIHave() method and it does not contain any of my configuration. However, cached instances of objects are not removed and are returned in subsequent tests. Is there a way to completely reset the ObjectFactory?

我想我可能必须使用 ObjectFactory.EjectAllInstancesOf(),但这似乎没有帮助.

I thought I might have to use ObjectFactory.EjectAllInstancesOf(), but that doesn't appear to help.

我使用的是 2.5.3 版.

I'm using version 2.5.3.

这是一些人为的代码来显示我在说什么.我希望这个测试通过,但它没有.

Here is some contrived code to show what I'm talking about. I would expect this test to pass, but it doesn't.

[TestFixture]
public class TestingStructureMap
{
    [Test]
    public void FirstTestUsingCachedObjects()
    {
        ObjectFactory.Configure(configure =>
            configure.ForRequestedType<ISomeInterface>()
                .TheDefaultIsConcreteType<SomeImplementation>()
                .CacheBy(InstanceScope.ThreadLocal)
            );

        ISomeInterface firstSomeInterface = ObjectFactory.GetInstance<ISomeInterface>();
        Assert.AreEqual(1, firstSomeInterface.ID);

        ObjectFactory.Initialize(initializationExpression => { });
        ObjectFactory.EjectAllInstancesOf<ISomeInterface>();

        ObjectFactory.Configure(configure =>
            configure.ForRequestedType<ISomeInterface>()
                .TheDefaultIsConcreteType<SomeImplementation>()
                .CacheBy(InstanceScope.ThreadLocal)
            );

        ISomeInterface secondSomeInterface = ObjectFactory.GetInstance<ISomeInterface>();
        Assert.AreEqual(2, secondSomeInterface.ID);
    }

    public interface ISomeInterface
    {
        int ID { get; }
    }

    public class SomeImplementation : ISomeInterface
    {
        private static int NumberOfInstancesCreated;
        private readonly int id;

        public int ID
        {
            get { return id; }
        }

        public SomeImplementation()
        {
            id = ++NumberOfInstancesCreated;
        }
    }
}

推荐答案

我已经想通了.ObjectFactory.EjectAllInstancesOf() 实际上依赖于 T 的配置.在我的代码中,我通过首先清除所有配置来取消 ObjectFactory.EjectAllInstancesOf() 的有效性.如果我切换这两行代码,就可以了.

I've figured it out. ObjectFactory.EjectAllInstancesOf() is actually dependant on there being a configuration for T. In my code, I nullified the effectiveness of ObjectFactory.EjectAllInstancesOf() by first clearing all of the configuration. If I switch these two lines of code, it works.

这不起作用:

ObjectFactory.Initialize(initializationExpression => { });
ObjectFactory.EjectAllInstancesOf<ISomeInterface>();

这确实有效:

ObjectFactory.EjectAllInstancesOf<ISomeInterface>();
ObjectFactory.Initialize(initializationExpression => { });   

这篇关于在 StructureMap 中重置 ObjectFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆