ResolveAll not working

Feb 3, 2012 at 5:29 AM

I have a code which look something like this.  When trying ResolveAll i dont get any instance from unity. Am i assuming or doing something wrong here.

 public class ResolveAllDemo
    {
        public ResolveAllDemo()
        {
            var container = UnityProvider.UnityInstanceProvider.NewUnityInstance;
 
            container.RegisterInstance<IUnityContainer>(container);
            container.RegisterType<IParserSuperParser>();
            container.RegisterType<IParserDefaultParser>();
            container.RegisterType<IParserBasicParser>();
            //   container.RegisterType<Crawler>();
            container.RegisterType<IParserFactoryUnityParserFactory>();
 
            foreach (var registeredMember in container.ResolveAll<IParser>())
            {
                LoggingUtility.LogerInstance.Logger.Write(registeredMember);
            }
        }
 
        #region Resovle Demo
 
        public interface IParserFactory
        {
            IEnumerable<IParser> BuildParsers();
        }
 
        public class UnityParserFactory : IParserFactory
        {
            private IUnityContainer _container;
 
            public UnityParserFactory(IUnityContainer container)
            {
                _container = container;
            }
 
            public IEnumerable<IParser> BuildParsers()
            {
                return _container.ResolveAll<IParser>();
            }
        }
 
        public class DefaultParser : IParser
        {
            public override string ToString()
            {
                return "DefaultParser";
            }
        }
 
        public class BasicParser : IParser
        {
            public override string ToString()
            {
                return "BasicParser";
            }
        }
 
        public class SuperParser : IParser
        {
            public override string ToString()
            {
                return "SuperParser";
            }
        }
 
        public interface IParser
        {
        }
 
        #endregion Resovle Demo
    }
Feb 3, 2012 at 5:43 AM

You are not registering 3 different implementations of IParser. You create one default mapping IParser -> SuperParser and then overwrite it with IParser -> DefaultParser and then you overwrite it again with IParser -> BasicParser.

If you want to register all three of them you have to provide named registrations like

container.RegisterType<IParser, SuperParser>("Super");
container.RegisterType<IParser, DefaultParser>("Default");
container.RegisterType<IParser, BasicParser>("Basic");

In addition ResolveAll does not include the default mapping (the one without a name). It only resolves those mappings of IParser that were registered as named mappings.

Feb 3, 2012 at 5:49 AM

I think @weberse touched on all the points.  Just for completeness you can check out the documentation for ResolveAll:

"This method is useful if you've registered multiple types with the same Type but different names.
Be aware that this method does NOT return an instance for the default (unnamed) registration."

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com