Possible Bug

Aug 22, 2012 at 2:10 AM

I may be misunderstanding how it should be used but here's a failing test case:

 

        [TestMethod]
        public void ResolveByKeyGetsCorrectInstances()
        {
            var foo1 = new Foo();
            var foo2 = new Foo();

            var iocContainer = new MetroContainer()
                .RegisterInstance<IFoo>(foo1, "theKey1")
                .RegisterInstance<IFoo>(foo2, "theKey2");

            var instance1 = iocContainer.Resolve<IFoo>("theKey1");
            var instance2 = iocContainer.Resolve<IFoo>("theKey2");

            Assert.AreSame(instance1, foo1);
            Assert.AreSame(instance2, foo2);
        }

-Michael O. Schoneman 

Aug 22, 2012 at 2:46 PM

Here's a very simple Singleton replacement that works the way I expect it to:

    public class Singleton : IRegistration
    {
        private object theInstance;
        public void Register(Type type, Func<LifetimeScope,object> resolverFunc)
        {
        }

        internal void Register(Type type, object instance)
        {
            theInstance = instance;
        }

        public object Resolve(Type type, Func<LifetimeScope, object> resolverFunc, LifetimeScope resolver)
        {
            if (theInstance == null)
            {
                theInstance = resolverFunc(resolver);
            }
            return theInstance;
        }
    }

-Michael O. Schoneman 

Coordinator
Aug 29, 2012 at 8:57 PM

Hi Michael,

 

Thanks for providing this failing test - it was certainly a bug!

 

The key associated with the registration was not being used in the Singleton IRegistration; I have changed this to respect the Type and the Key, so the test passes, and everything else stays green :)

 

Cheers,

Ian