Bug: Booleans not supported?

Mar 25, 2008 at 6:41 PM
Edited Mar 25, 2008 at 6:46 PM
I am trying to get booleans to work with property injection from configuration.

I modified the Tests.Unity.ObjectWithTwoProperties to have the following property:
	public bool Testing
    	{
    		get { return testing; }
    		set { testing = value; }
    	}	bool testing;

I then set added this in the configuration:
<property name="Testing" propertyType="System.Boolean, mscorlib">
	<value value="true" />
</property>

I get the following exception when I run CanInjectMultipleProperties:

Test method Microsoft.Practices.Unity.Tests.InjectionConfigurationFixture.CanInjectMultipleProperties threw exception:  Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "ObjectWithTwoProperties", name = "". See the InnerException for more details. --->  Microsoft.Practices.ObjectBuilder2.BuildFailedException: The current build operation failed on strategy type Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy, index 2 for build key Build Key[Microsoft.Practices.Unity.Tests.TestObjects.ObjectWithTwoProperties, null] --->  System.InvalidCastException: Specified cast is not valid..
 
BuildUp_Microsoft.Practices.Unity.Tests.TestObjects.ObjectWithTwoProperties(IBuilderContext )
Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) in C:\Program Files\Microsoft Unity Application Block March 2008\UnitySource\UnitySource\Src\ObjectBuilder\Strategies\BuildPlan\DynamicMethod\DynamicMethodBuildPlan.cs: line 37
Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) in C:\Program Files\Microsoft Unity Application Block March 2008\UnitySource\UnitySource\Src\ObjectBuilder\Strategies\BuildPlan\BuildPlanStrategy.cs: line 40
Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) in C:\Program Files\Microsoft Unity Application Block March 2008\UnitySource\UnitySource\Src\ObjectBuilder\Strategies\StrategyChain.cs: line 92
Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) in C:\Program Files\Microsoft Unity Application Block March 2008\UnitySource\UnitySource\Src\ObjectBuilder\Strategies\StrategyChain.cs: line 109
Microsoft.Practices.ObjectBuilder2.Builder.BuildUp(IReadWriteLocator locator, ILifetimeContainer lifetime, IPolicyList policies, IStrategyChain strategies, Object buildKey, Object existing) in C:\Program Files\Microsoft Unity Application Block March 2008\UnitySource\UnitySource\Src\ObjectBuilder\Builder.cs: line 61
Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name) in C:\Program Files\Microsoft Unity Application Block March 2008\UnitySource\UnitySource\Src\Unity\UnityContainer.cs: line 449
Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name) in C:\Program Files\Microsoft Unity Application Block March 2008\UnitySource\UnitySource\Src\Unity\UnityContainer.cs: line 459
Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name) in C:\Program Files\Microsoft Unity Application Block March 2008\UnitySource\UnitySource\Src\Unity\UnityContainer.cs: line 442
Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name) in C:\Program Files\Microsoft Unity Application Block March 2008\UnitySource\UnitySource\Src\Unity\UnityContainer.cs: line 146
Microsoft.Practices.Unity.UnityContainerBase.Resolve(Type t) in C:\Program Files\Microsoft Unity Application Block March 2008\UnitySource\UnitySource\Src\Unity\UnityContainerBase.cs: line 416
Microsoft.Practices.Unity.UnityContainerBase.Resolve[T]() in C:\Program Files\Microsoft Unity Application Block March 2008\UnitySource\UnitySource\Src\Unity\UnityContainerBase.cs: line 394
Microsoft.Practices.Unity.Tests.InjectionConfigurationFixture.ResolveConfiguredObject[TObj](String containerName) in C:\Program Files\Microsoft Unity Application Block March 2008\UnitySource\UnitySource\Tests\Unity.Tests\InjectionConfigurationFixture.cs: line 98
Microsoft.Practices.Unity.Tests.InjectionConfigurationFixture.CanInjectMultipleProperties() in C:\Program Files\Microsoft Unity Application Block March 2008\UnitySource\UnitySource\Tests\Unity.Tests\InjectionConfigurationFixture.cs: line 77
 

... I also just tried this with an integer as well and got the same issue.
Mar 25, 2008 at 8:03 PM
You need to specify the type on the <value> element too. Otherwise it tries to stuff a string into a bool property and boom.
Mar 25, 2008 at 8:39 PM
Damn. Yes, in an ideal world, these type values should be figured out automagically. Maybe next version. :)

VERY impressed with what I've seen so far! This configuration is exactly what is needed. It just seems a little verbose, but I'm sure that will be fixed soon enough one way or another. ;)