How to judge a type of instance registered

Nov 24, 2008 at 1:01 AM
I have a block of code like this (Container is unity container, SingletonDelegate is a delegate return new instance of type T)
   public Singleton<T>(SingletonDelegate<T> singletonDelete) 
if (Container.ResolveAll<T>().Count() == 0)
        return Container.Resolve<T>();
it works good in unity 1.1, but after i install the latest 1.2 version, ResolveAll() will always return 0 after any registeration.

I must add a instance name both in RegisterInstance<T> and Resolve<T>, so i don't know what has changed and it's very hard to understand the register and resolve logic here ( seems to be much more easy, for no registeration will return null in resolve, but unity just throw exception in Resolve<Interface> and return a new instance in Resolve<Class>)
Nov 24, 2008 at 2:41 AM

The ResolveAll method overloads only return types that you registered with a specified name. They do not return instances for any default (un-named) registration mappings.

See this :

For more info refer to the documentation.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
Nov 24, 2008 at 1:46 PM
Is there a particular reason why you are not using the ContainerControlledLifetimeManager in Unity to create and manage singletons for you? Seems like it would simplify the code for you a great deal.

However, using your code above, just register and resolve everything using each type's name as opposed to a default instance. Something like:

Container.RegisterInstance<T>(typeof(T).Name, singleonDelegate());

and resolve using:


This should allow your ResolveAll<T> to work since all your types will be registered using named instances.


Patterns & Practices Guidance