views:

669

answers:

1

Can you override the constructor arguments for a named instance, it seems you can only do it for a default instance.

I would like to do:

ObjectFactory.With("name").EqualTo("Matt").GetNamedInstance<IActivity>("soccer");
+1  A: 

GetInstance behaves like GetNamedInstance when used after .With

    


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)));
        }

    }
}

jasoni
So glad you fixed that :-)
Simon Farrow