tags:

views:

45

answers:

2

If I have the following, and I were to say ObjectFactory.GetInstance<Master>() is it possible to tell StructureMap to make the I_A instance to A_User the same instance as the I_A passed to Master?

public interface I_A { }
public interface I_B { }

public class A_User {
    public A_User(I_A A) { }
}

public class Master {
    public Master(I_A _, I_B __, A_User ___) { }
}
+1  A: 

UPDATED: As @Joshua Flanagan points out below, this is default SM behaviour. The following unit tests show that. The first tests uses the default behaviour. The second shows how you would get a unique instance if you wanted it:

using System;
using System.Collections.Generic;
using NUnit.Framework;
using StructureMap;
using StructureMap.Pipeline;

namespace SMTest
{
    [TestFixture]
    public class TestSOQuestion
    {

        class Foo : IFoo { }
        interface IFoo { }

        private interface IBar {
            IFoo Foo { get; set; }
        }
        class Bar : IBar
        {
            public IFoo Foo { get; set; }
            public Bar(IFoo foo)
            {
                Foo = foo;
            }
        }

        class UsesFooAndBar
        {
            public IBar Bar { get; set; }
            public IFoo Foo { get; set; }
            public UsesFooAndBar(IFoo foo, IBar bar)
            {
                Foo = foo;
                Bar = bar;
            }
        }

        [Test]
        public void TestOtherAnswer()
        {
            IContainer container = new Container(x =>
                                         {
                                             x.For<IFoo>().Use<Foo>();
                                             x.For<IBar>().Use<Bar>();
                                         });
            var usesFooAndBar = container.GetInstance<UsesFooAndBar>();
            Assert.AreSame(usesFooAndBar.Foo, usesFooAndBar.Bar.Foo);            
        }

        [Test]
        public void TestNonDefaultBehaviour()
        {
            IContainer container = new Container(x =>
                                         {
                                             x.For<IFoo>().AlwaysUnique().Use<Foo>();
                                             x.For<IBar>().Use<Bar>();
                                         });
            var usesFooAndBar = container.GetInstance<UsesFooAndBar>();
            Assert.AreNotSame(usesFooAndBar.Foo, usesFooAndBar.Bar.Foo); 
        }

    }
}
Tim Hoolihan
This should be completely unnecessary with StructureMap. If it is, there is a bug.
Joshua Flanagan
@Joshua Flanagan: Cool, I didn't know that. I wrote a unit test to check that out and put it in my post. Since you're a member of the team (and gave me a down-vote), I have to point out that this would be clearer if the documentation wasn't all based on deprecated and verbose methods :)
Tim Hoolihan
I agree, and point out that it is open source and we take contributions!
Joshua Flanagan
BTW, I only downvoted to try and get the asker to remove yours as the answer (since it is wrong, or misleading). I'll remove the downvote if you remove the unnecessary configuration section at the top and just state that StructureMap handles it by default.
Joshua Flanagan
@Joshua Flanagan: updated sample, and added a test that shows how do per request to differentiate the too. As for contributions, I'll consider.
Tim Hoolihan
+1  A: 

The default behavior of StructureMap will always give you the same instance within a "build session" (effectively, a single call to GetInstance). You should not have to configure anything extra to get the behavior you want.

If it is not working as you expect, please post more details, or mention it on the StructureMap mailing list.

Joshua Flanagan