【问题标题】:StructureMap - Override constructor arguments for a named instanceStructureMap - 覆盖命名实例的构造函数参数
【发布时间】:2009-01-13 14:40:53
【问题描述】:

您能否覆盖命名实例的构造函数参数,看来您只能为默认实例执行此操作。

我想做:

ObjectFactory.With("name").EqualTo("Matt").GetNamedInstance<IActivity>("soccer"); 

【问题讨论】:

  • 我也有同样的问题....有人吗?
  • 调用这个会发生什么? (假设其语法有效)
  • 有人得到这个答案吗?

标签: inversion-of-control structuremap


【解决方案1】:

在 .With 之后使用时,GetInstance 的行为类似于 GetNamedInstance

    


using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using StructureMap;

namespace StructureMapWith
{
    [TestFixture]
    public class Class1
    {

        public interface IFooParent
        {
            IFoo Foo { get; set; }
        }
        public interface IFoo
        {

        }

        public class FooParentLeft : IFooParent
        {
            public IFoo Foo { get; set; }

            public FooParentLeft(IFoo foo)
            {
                Foo = foo;
            }
        }

        public class FooParentRight : IFooParent
        {
            public IFoo Foo { get; set; }

            public FooParentRight()
            {

            }
            public FooParentRight(IFoo foo)
            {
                Foo = foo;
            }
        }

        public class Left : IFoo { }
        public class Right : IFoo { }

        [Test]
        public void See_what_with_does_more()
        {
            ObjectFactory.Initialize(c =>
                                        {
                                            c.ForRequestedType()
                                                .AddInstances(i =>
                                                                  {
                                                                      i.OfConcreteType().WithName("Left");
                                                                      i.OfConcreteType().WithName("Right");
                                                                  });
                                            c.ForRequestedType()
                                                .AddInstances(i =>
                                                                  {
                                                                      i.OfConcreteType().WithName("Left");
                                                                      i.OfConcreteType().WithName(
                                                                          "Right");
                                                                  });
                                        });

            var returned = ObjectFactory.With(typeof (IFoo), new Right())
                .With(typeof (IFooParent), new FooParentRight())
                .GetInstance("Right");
            Assert.That(returned, Is.TypeOf(typeof(FooParentRight)));
            Assert.That(returned.Foo, Is.TypeOf(typeof (Right)));
        }

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 2015-01-02
    相关资源
    最近更新 更多