How to determine does some object exist in U-container already or not

Apr 9, 2008 at 3:57 PM
Edited Apr 9, 2008 at 4:11 PM
Try to describe my usecase and issues with Unity I run into.

I have some objects which describe Application abstraction. All of them implement IApplication interface.
Each Application-object is configurable. I use System.Configuration and EntLib v.3's extensions for it (IConfigurationSource and difference types from EntLib's Common assembly).
Configuration (actualy .config-file, but it can be any IConfigurationSource-implementation) of all Applications "deserialized" into one object - MyConfigurationManager. This object actualy just contains the collection of all configurated Applications found in 'configuration source'.
And I have MyConfigurationManagerFactory which parameterized by IConfigurationSource. MyConfigurationManagerFactory uses EnterpriseLibraryFactory to build up MyConfigurationManager's instance.

So broadly the code looks like this:
	Microsoft.Practices.EnterpriseLibrary.Common.Configuration.IConfigurationSource configSource = new SystemConfigurationSource();
	MyConfigurationManagerFactory configFactory = new MyConfigurationManagerFactory(configSource);
	MyConfigurationManager config = configFactory.Create();
	IApplication app = config.GetDefaultAppConfig();
MyConfigurationManagerFactory's Create method is pretty simple:
		public MyConfigurationManager Create()
			return EnterpriseLibraryFactory.BuildUp<MyConfigurationManager>(configurationSource, Policies);
where 'configurationSource' is IConfigurationSource and 'Policies' is some additional policies for ObjectBuilder (v.1).

Then I've decided to use Unity. I thought about similar things that guys talked about here:
So I created singleton instance of Unity-container in AppDomain - UnityHelper.Contrainer (see link from above). Then I wanted to use it in my WCF-service. And wanted to store default Application's instance for WCF-service.
In UnitTests I manually registered Application's instance for service in singleton Unity-container. Then the turn went for real application.
In my WCF-service code I wrote such code:
	IApplication app = UnityHelper.Container.Resolve<IApplication>();
in the case I haven't registered IApplication's implementation in the contrainer (as I did in UnitTests) I get an exception.
That code leads to attempt to create instance of IApplication and causes such exception:
"The type IApplication does not have an accessible constructor"

So probably I should configure the Unity-container in configuration. But how? I have to specify:
- IConfigurationSource implementation and parameters for concrete implementation. If it's FileConfigurationSource for instance than I have to specify filename (it's not problem);
- create MyConfigurationManagerFactory's instance which gets in its ctor IConfigurationSource-implementation;
- call MyConfigurationManagerFactory's Create method to get MyConfigurationManager's instance (fully configurated based on specified configuration source);
- and only then ask MyConfigurationManager's instance for some (default) Application's instance.

The problem is that it's not enough to specify implementation for IApplication. Actualy I shouldn't do this at all because real Application's type is specified in 'configuration source', which I pass to MyConfigurationManagerFactory.
I feel as I'm trying to interbreed grass-snake with hedgehog :( (I'm not sure you have such proverb)

As for discussion's subject:
it'd be helpful if I can ask Unity-container does it has any instance for some interface. In my case I'd write such code in my service:
	IApplication app = UnityHelper.Container.TryResolve<IApplication>();
	if (app == null)
		IConfigurationSource configSource = UnityHelper.Container.Resolve<IConfigurationSource>();
		MyConfigurationManagerFactory configFactory = new MyConfigurationManagerFactory(configSource);
		MyConfigurationManager config = configFactory.Create();
		IApplication app = config.GetDefaultAppConfig();
So if that were possible I would have to specify in Unity's configuration only implementation type for IConfigurationSource. That's such easier.

So I'll be appreciate for any thoughts about.

And I'm looking forward very much when you integrate Unity with new EntLib.
Apr 11, 2008 at 10:44 AM
I think the only way to figure out if the object you need already exists in a container is just try to resolve it and check if for null result.
To get rid of nulls I think you can go this way - create a default container and register there Null Object instances (like String.Empty) of all types you need and then create a child container and configure it to use the concrete types. Everywhere in code ask only the child container to resolve types and in case it cann't it will return a null object for that requested type. In such a case you never have nulls returned from the container.

Thanks, Leonid
Apr 11, 2008 at 3:46 PM
Thanks for your suggestion, Leonid.
But asking Unity with Resolve method you won't get null you'll get an exception like "The type IApplication does not have an accessible constructor" (in the case interface resolving).
Apr 11, 2008 at 9:01 PM
It has already been discussed and I showed a code sample on how to do so:
Apr 14, 2008 at 8:37 AM
Thank you a lot, that's nice way to solve my issue.