ResolveAll() gives more instances than registered via RegisterInstance()

Mar 19, 2009 at 3:10 PM
I am new to the Unity framework and I am puzzled on how the methods ResolveAll() and RegisterInstance() behave.

I have the following source code:

    class Program
    {
        static void Main(string[] args)
        {
            IUnityContainer container = new UnityContainer()
                .RegisterType<IPerson, JustAPerson>("justaperson")
                .RegisterType<IPerson, VeryImportantPerson>("veryimportantperson");

            Console.WriteLine("Here I start");

            IPerson person = container.Resolve<IPerson>("justaperson");
            person.SetName("Harry");
            Console.WriteLine(string.Format("Person {0} has new name {1}", person.GetHashCode(), person.Name));
            container.RegisterInstance<IPerson>(person.Name, person);

            person = container.Resolve<IPerson>("veryimportantperson");
            person.SetName("Sally");
            Console.WriteLine(string.Format("Person {0} has new name {1}", person.GetHashCode(), person.Name));
            container.RegisterInstance<IPerson>(person.Name, person);

            Console.WriteLine("I created all persons");

            ICollection<IPerson> persons = new List<IPerson>(container.ResolveAll<IPerson>());
            Console.WriteLine(string.Format("Persons: {0}", persons.Count));
            foreach (IPerson p in persons)
            {
                Console.WriteLine(string.Format("Person: {0} ({1}) is {2}", p.Name, p.GetHashCode(), p.GetType().Name));
            }

            person = container.Resolve<IPerson>("Sally");
            Console.WriteLine(string.Format("Is this person 'Sally'? {0} ({1})", person.Name, person.GetHashCode()));

            person = container.Resolve<IPerson>("Harry");
            Console.WriteLine(string.Format("Is this person 'Harry'? {0} ({1})", person.Name, person.GetHashCode()));

            Console.Write("Press ENTER");
            Console.ReadLine();
        }
    }

    public interface IPerson
    {
        string Name { get; }

        void SetName(string newName);
    }

    public class JustAPerson : IPerson
    {
        public JustAPerson()
        {
            Console.WriteLine("Person {0} of type {1} created ", this.GetHashCode(), this.GetType().Name);
            this.Name = "This is just a person";
        }

        public string Name { get; private set; }

        public void SetName(string newName)
        {
            this.Name = newName;
        }
    }

    public class VeryImportantPerson : IPerson
    {
        public VeryImportantPerson()
        {
            Console.WriteLine("Person {0} of type {1} created ", this.GetHashCode(), this.GetType().Name);
            this.Name = "This is a very important person";
        }

        public string Name { get; private set; }

        public void SetName(string newName)
        {
            this.Name = newName;
        }
    }

The output of my console:

---
Here I start
Person 54267293 of type JustAPerson created
Person 54267293 has new name Harry
Person 18643596 of type VeryImportantPerson created
Person 18643596 has new name Sally
I created all persons
Person 33574638 of type JustAPerson created
Person 33736294 of type VeryImportantPerson created
Persons: 4
Person: This is just a person (33574638) is JustAPerson
Person: This is a very important person (33736294) is VeryImportantPerson
Person: Harry (54267293) is JustAPerson
Person: Sally (18643596) is VeryImportantPerson
Is this person 'Sally'? Sally (18643596)
Is this person 'Harry'? Harry (54267293)
Press ENTER---

The console output is not what I expected.
There are 2 more person object created than I registered via RegisterInstance() (33574638 and 33736294).
Where do these person object come from and can I get rid of them?

Alex.

Mar 23, 2009 at 11:41 PM
The container is doing exactly what you told it to. You've got these four registrations:

                RegisterType<IPerson, JustAPerson>("justaperson")
                RegisterType<IPerson, VeryImportantPerson>("veryimportantperson")
                RegisterInstance<IPerson>("Harry", person)
                RegisterInstance<IPerson>("Sally", person)

In each case, the name is different, so you've got four registrations for IPerson. Why are you surprised to get four results back from ResolveAll?



Mar 25, 2009 at 11:28 AM
I 'm surprised because I registered 2 types via RegisterType<>(). I did not expect that 2 instances are created of these types.
Also what I did not expect that these 2 instances are created when I call ResolveAll<>().
I only expected the 2 instances that I registered via RegisterInstance<>(). Is there a way that I can retrieve all my registered instances?
Mar 26, 2009 at 9:37 PM
Edited Mar 26, 2009 at 9:38 PM
You called Resolve and expected to get instances of those types registered with RegisterType. Why would ResolveAll do anything different?

If you really want to separate out just the registered instances, I'd say use a separate instance of the container with just the specific instances in it.