【问题标题】:StructureMap default instance and additional with explicit argumentsStructureMap 默认实例和带有显式参数的附加实例
【发布时间】:2012-04-18 04:48:07
【问题描述】:

我有一个接口和一个实现该接口的类。该类有一个默认的静态实例,也可以显式构造(传递参数)。

如何配置 StrucutreMap 以使默认实例为静态实例,如果我请求一个带有参数的实例,则构造一个新实例?

这是失败的测试

[TestFixture]
public class StructureMapTests
{
    [Test]
    public void Test_same_interface_default_implemenation_and_with_parameter()
    {
        IMyInterface defaultImp = new MyImpl(0);

        ObjectFactory.Initialize(x =>
                                     {
                                         x.For<IMyInterface>().Use(defaultImp);
                                         x.For<IMyInterface>().Add<MyImpl>().Ctor<int>().IsTheDefault();                                             
                                     });

        Assert.AreEqual(defaultImp, ObjectFactory.GetInstance<IMyInterface>());

        var instance1 = ObjectFactory.With("value").EqualTo(1).GetInstance<IMyInterface>();
        Assert.AreEqual(true, instance1 != defaultImp); //<-- fails here
        Assert.AreEqual(1, instance1.GetValue());

        var instance2 = ObjectFactory.With("value").EqualTo(2).GetInstance<IMyInterface>();
        Assert.AreEqual(true, instance1 != defaultImp);
        Assert.AreEqual(2, instance2.GetValue());
    }

    public interface IMyInterface
    {
        int GetValue();
    }

    public class MyImpl : IMyInterface
    {
        private int _value;

        public MyImpl(int value)
        {
            _value = value;
        }

        public int GetValue()
        {
            return _value;
        }
    }
}

【问题讨论】:

    标签: c# dependency-injection ioc-container structuremap


    【解决方案1】:

    我认为您面临的问题是,当为同一个接口注册多个实现时,最后一个是 GetInstance 将要解决的那个。要解决此问题,您可以为配置命名。

    尝试以下方法:

    [TestFixture] 
    public class StructureMapTests 
    { 
        [Test] 
        public void Test_same_interface_default_implemenation_and_with_parameter() 
        { 
            IMyInterface defaultImp = new MyImpl(0);
    
            ObjectFactory.Initialize(x =>
                                         {
                                             x.For<IMyInterface>().Add<MyInterface>().Named("withArgument").Ctor<int>().IsTheDefault();                                        
                                             x.For<IMyInterface>().Use(defaultImp).Named("default");
                                         });
    
            Assert.AreEqual(defaultImp, ObjectFactory.GetInstance<IMyInterface>());
    
            var instance1 = ObjectFactory.With("value").EqualTo(1).GetInstance<IMyInterface>("withArgument");
            Assert.AreEqual(true, instance1 is MyInterface); 
            Assert.AreEqual(1, instance1.GetValue());
    
            var instance2 = ObjectFactory.With("value").EqualTo(2).GetInstance<IMyInterface>("withArgument");
            Assert.AreEqual(true, instance2 is MyInterface);
            Assert.AreEqual(2, instance2.GetValue());
        }
    
        public interface IMyInterface
        {
            int GetValue();
        }
    
        private class MyInterface : IMyInterface
        {
            private int _value;
    
            public MyInterface(int value)
            {
                _value = value;
            }
    
            public int GetValue()
            {
                return _value;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-21
      • 2013-05-23
      • 2012-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多